|
4 | 4 | from typing import Any, Dict, Optional |
5 | 5 |
|
6 | 6 | from tortoise import Tortoise |
7 | | -from tortoise.transactions import in_transaction |
| 7 | +from tortoise.transactions import get_connection |
8 | 8 |
|
9 | 9 | from dipdup.config import ContractConfig, DipDupConfig, IndexConfig, IndexTemplateConfig, PostgresDatabaseConfig |
10 | 10 | from dipdup.datasources.datasource import Datasource |
@@ -46,24 +46,30 @@ async def restart(self) -> None: |
46 | 46 |
|
47 | 47 | async def reindex(self) -> None: |
48 | 48 | """Drop all tables or whole database and restart with the same CLI arguments""" |
49 | | - if isinstance(self.config.database, PostgresDatabaseConfig): |
50 | | - exclude_expression = '' |
51 | | - if self.config.database.immune_tables: |
52 | | - immune_tables = [f"'{t}'" for t in self.config.database.immune_tables] |
53 | | - exclude_expression = f' AND tablename NOT IN ({",".join(immune_tables)})' |
54 | | - |
55 | | - async with in_transaction() as conn: |
56 | | - await conn.execute_script( |
57 | | - f''' |
58 | | - DO $$ DECLARE |
59 | | - r RECORD; |
60 | | - BEGIN |
61 | | - FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema(){exclude_expression}) LOOP |
62 | | - EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE'; |
63 | | - END LOOP; |
64 | | - END $$; |
65 | | - ''' |
66 | | - ) |
| 49 | + |
| 50 | + async def _recreate_schema(conn, name: str) -> None: |
| 51 | + await conn.execute_script(f'DROP SCHEMA IF EXISTS {name} CASCADE') |
| 52 | + await conn.execute_script(f'CREATE SCHEMA {name}') |
| 53 | + |
| 54 | + async def _move_table(conn, name: str, schema: str, new_schema: str) -> None: |
| 55 | + await conn.execute_script(f'ALTER TABLE {schema}.{name} SET SCHEMA {new_schema}') |
| 56 | + |
| 57 | + database_config = self.config.database |
| 58 | + if isinstance(database_config, PostgresDatabaseConfig): |
| 59 | + conn = get_connection(None) |
| 60 | + immune_schema_name = f'{database_config.schema_name}_immune' |
| 61 | + |
| 62 | + if database_config.immune_tables: |
| 63 | + await _recreate_schema(conn, immune_schema_name) |
| 64 | + |
| 65 | + for table in database_config.immune_tables: |
| 66 | + await _move_table(conn, table, database_config.schema_name, immune_schema_name) |
| 67 | + |
| 68 | + await _recreate_schema(conn, database_config.schema_name) |
| 69 | + |
| 70 | + for table in database_config.immune_tables: |
| 71 | + await _move_table(conn, table, immune_schema_name, database_config.schema_name) |
| 72 | + |
67 | 73 | else: |
68 | 74 | await Tortoise._drop_databases() |
69 | 75 | await self.restart() |
|
0 commit comments