Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pragma statements automatically if sqlite #9169

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/prefect/server/database/migrations/env.py
Original file line number Diff line number Diff line change
@@ -91,8 +91,8 @@ def dry_run_migrations() -> None:
render_as_batch=dialect.name == "sqlite",
# Each migration is its own transaction
transaction_per_migration=True,
template_args={"dialect": dialect.name},
)

with context.begin_transaction():
context.run_migrations()

@@ -104,7 +104,6 @@ def do_run_migrations(connection: AsyncEngine) -> None:
Args:
connection: a database engine.
"""

context.configure(
connection=connection,
target_metadata=target_metadata,
@@ -123,6 +122,7 @@ def do_run_migrations(connection: AsyncEngine) -> None:
render_as_batch=dialect.name == "sqlite",
# Each migration is its own transaction
transaction_per_migration=True,
template_args={"dialect": dialect.name},
)

with context.begin_transaction():
10 changes: 9 additions & 1 deletion src/prefect/server/database/migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -16,10 +16,18 @@ down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}

<%
sqlite = dialect == "sqlite"
postgresql = dialect == "postgresql"
%>


def upgrade():
${'op.execute("PRAGMA foreign_keys=OFF")' if sqlite else ""}
${upgrades if upgrades else "pass"}

${'op.execute("PRAGMA foreign_keys=ON")' if sqlite else ""}

def downgrade():
${'op.execute("PRAGMA foreign_keys=OFF")' if sqlite else ""}
${downgrades if downgrades else "pass"}
${'op.execute("PRAGMA foreign_keys=ON")' if sqlite else ""}