Skip to content

Commit 29872bb

Browse files
authored
Improved Migrations (#628)
If running many migrations at once, do each in a separate transaction instead of all in one transaction. Reduces contention when migrating an active database.
1 parent 13155e7 commit 29872bb

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

dbos/_migration.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,23 @@ def run_dbos_migrations(
4545
engine: sa.Engine, schema: str, use_listen_notify: bool
4646
) -> None:
4747
"""Run DBOS-managed migrations by executing each SQL command in dbos_migrations."""
48+
# Get current migration version
4849
with engine.begin() as conn:
49-
# Get current migration version
5050
result = conn.execute(
5151
sa.text(f'SELECT version FROM "{schema}".dbos_migrations')
5252
)
5353
current_version = result.fetchone()
5454
last_applied = current_version[0] if current_version else 0
5555

56-
# Apply migrations starting from the next version
57-
migrations = get_dbos_migrations(schema, use_listen_notify)
58-
for i, migration_sql in enumerate(migrations, 1):
59-
if i <= last_applied:
60-
continue
56+
# Apply each migration in its own transaction
57+
migrations = get_dbos_migrations(schema, use_listen_notify)
58+
for i, migration_sql in enumerate(migrations, 1):
59+
if i <= last_applied:
60+
continue
6161

62-
# Execute the migration
63-
dbos_logger.info(f"Applying DBOS system database schema migration {i}")
62+
dbos_logger.info(f"Applying DBOS system database schema migration {i}")
6463

64+
with engine.begin() as conn:
6565
# Migration 10 adds a primary key to the notifications table.
6666
# Skip it if the table already has one.
6767
if (

dbos/cli/cli.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
load_config,
2525
)
2626
from .._docker_pg_helper import start_docker_pg, stop_docker_pg
27-
from .._logger import dbos_logger
27+
from .._logger import dbos_logger, init_logger
2828
from .._sys_db import SystemDatabase
2929
from .._utils import GlobalParams
3030
from ..cli._github_init import create_template_from_github
@@ -286,7 +286,9 @@ def migrate(
286286
system_database_url=system_database_url,
287287
application_database_url=application_database_url,
288288
)
289-
289+
# Emit INFO logs from migrations
290+
init_logger()
291+
dbos_logger.setLevel(logging.INFO)
290292
typer.echo(f"Starting DBOS migrations")
291293
if application_database_url:
292294
typer.echo(f"Application database: {sa.make_url(application_database_url)}")

0 commit comments

Comments
 (0)