def db(self) -> Engine: if not self._db: db = engine_finder() if not db: raise Exception("Unable to find the engine") self._db = db return self._db
def setUp(self) -> None: engine: t.Optional[Engine] = engine_finder() class Schema(Table, db=engine): """ Only for raw query execution """ pass Schema.raw("CREATE SCHEMA IF NOT EXISTS schema1").run_sync() Schema.raw("CREATE SCHEMA IF NOT EXISTS schema2").run_sync() Publication.create_table().run_sync() Writer.create_table().run_sync() Book.create_table().run_sync()
async def load_json_string(json_string: str): """ Parses the JSON string, and inserts the parsed data into the database. """ # We have to deserialise the JSON to find out which apps and tables it # contains, so we can create a Pydantic model. # Then we let Pydantic do the proper deserialisation, as it does a much # better job of deserialising dates, datetimes, bytes etc. deserialised_contents = load_json(json_string) app_names = deserialised_contents.keys() fixture_configs = [ FixtureConfig( app_name=app_name, table_class_names=[ i for i in deserialised_contents[app_name].keys() ], ) for app_name in app_names ] pydantic_model_class = create_pydantic_fixture_model( fixture_configs=fixture_configs) fixture_pydantic_model = pydantic_model_class.parse_raw(json_string) finder = Finder() engine = engine_finder() if not engine: raise Exception("Unable to find the engine.") async with engine.transaction(): for app_name in app_names: app_model = getattr(fixture_pydantic_model, app_name) for ( table_class_name, model_instance_list, ) in app_model.__dict__.items(): table_class = finder.get_table_with_name( app_name, table_class_name) await table_class.insert(*[ table_class.from_dict(row.__dict__) for row in model_instance_list ]).run()
async def run_backwards(self): print("Reversing MigrationManager ...") engine = engine_finder() if not engine: raise Exception("Can't find engine") async with engine.transaction(): for raw in self.raw_backwards: if inspect.iscoroutinefunction(raw): await raw() else: raw() await self._run_add_columns(backwards=True) await self._run_add_tables(backwards=True) await self._run_drop_tables(backwards=True) await self._run_rename_tables(backwards=True) await self._run_drop_columns(backwards=True) await self._run_rename_columns(backwards=True) await self._run_alter_columns(backwards=True)
async def run(self): print(f" - {self.migration_id} [forwards]... ", end="") engine = engine_finder() if not engine: raise Exception("Can't find engine") async with engine.transaction(): for raw in self.raw: if inspect.iscoroutinefunction(raw): await raw() else: raw() await self._run_add_tables() await self._run_rename_tables() await self._run_add_columns() await self._run_drop_columns() await self._run_drop_tables() await self._run_rename_columns() await self._run_alter_columns()
async def close_database_connection_pool(): engine = engine_finder() await engine.close_connnection_pool()
async def open_database_connection_pool(): engine = engine_finder() await engine.start_connnection_pool()
def refresh_db(self): self.db = engine_finder()
async def close_database_connection_pool(): try: engine = engine_finder() await engine.close_connection_pool() except Exception: print("Unable to connect to the database")