|
28 | 28 | from cloudbot.util.mapping import KeyFoldDict |
29 | 29 |
|
30 | 30 | if TYPE_CHECKING: |
31 | | - from sqlalchemy import Table |
32 | 31 | from sqlalchemy.engine import Engine |
33 | | - from sqlalchemy.orm import Session |
34 | 32 |
|
35 | 33 | from cloudbot.client import Client |
36 | 34 |
|
@@ -200,8 +198,8 @@ def __init__( |
200 | 198 | max_workers=self.config.get("thread_count") |
201 | 199 | ) |
202 | 200 |
|
203 | | - self.old_db = self.config.get("old_database") |
204 | | - self.do_db_migrate = self.config.get("migrate_db") |
| 201 | + self.old_db: str | None = self.config.get("old_database") |
| 202 | + self.do_db_migrate: bool = self.config.get("migrate_db", False) |
205 | 203 |
|
206 | 204 | # set values for reloading |
207 | 205 | reloading_conf = self.config.get("reloading", {}) |
@@ -354,7 +352,7 @@ async def _init_routine(self) -> None: |
354 | 352 | await self.plugin_manager.load_all(self.plugin_dir) |
355 | 353 |
|
356 | 354 | if self.old_db and self.do_db_migrate: |
357 | | - self.migrate_db() |
| 355 | + self.migrate_db(old_db_url=self.old_db) |
358 | 356 | if self.stopped_future: |
359 | 357 | self.stopped_future.set_result(False) |
360 | 358 |
|
@@ -548,24 +546,33 @@ async def reload_config(self) -> None: |
548 | 546 |
|
549 | 547 | await asyncio.gather(*tasks) |
550 | 548 |
|
551 | | - def migrate_db(self) -> None: |
| 549 | + def migrate_db(self, *, old_db_url: str) -> None: |
552 | 550 | logger.info("Migrating database") |
553 | | - engine: Engine = create_engine(self.old_db, future=True) |
554 | | - old_session: Session = scoped_session(sessionmaker(bind=engine))() |
555 | | - new_session: Session = database.Session() |
556 | | - table: Table |
557 | | - inspector = sa_inspect(engine) |
558 | | - for table in database.metadata.tables.values(): |
559 | | - logger.info("Migrating table %s", table.name) |
560 | | - if not inspector.has_table(table.name): |
561 | | - continue |
| 551 | + engine: Engine = create_engine(old_db_url, future=True) |
| 552 | + with ( |
| 553 | + scoped_session( |
| 554 | + sessionmaker(bind=engine, future=True) |
| 555 | + )() as old_session, |
| 556 | + database.Session() as new_session, |
| 557 | + ): |
| 558 | + inspector = sa_inspect(engine) |
| 559 | + for table in database.metadata.tables.values(): |
| 560 | + logger.info("Migrating table %s", table.name) |
| 561 | + if not inspector.has_table(table.name): |
| 562 | + continue |
562 | 563 |
|
563 | | - old_data = old_session.execute(table.select()).mappings().fetchall() |
564 | | - if not old_data: |
565 | | - continue |
| 564 | + old_data = ( |
| 565 | + old_session.execute(table.select()).mappings().fetchall() |
| 566 | + ) |
| 567 | + if not old_data: |
| 568 | + continue |
| 569 | + |
| 570 | + table.create(bind=self.db_engine, checkfirst=True) |
| 571 | + new_session.execute( |
| 572 | + table.insert(), [dict(row) for row in old_data] |
| 573 | + ) |
| 574 | + new_session.commit() |
| 575 | + old_session.execute(table.delete()) |
| 576 | + old_session.commit() |
566 | 577 |
|
567 | | - table.create(bind=self.db_engine, checkfirst=True) |
568 | | - new_session.execute(table.insert(), [dict(row) for row in old_data]) |
569 | | - new_session.commit() |
570 | | - old_session.execute(table.delete()) |
571 | | - old_session.commit() |
| 578 | + engine.dispose() |
0 commit comments