Skip to content

Commit

Permalink
Update checkdb subcommand to work with sqlalchemy-2.0
Browse files Browse the repository at this point in the history
Use a raw connection cursor to send the "create database" and "drop
database" commands during checkdb

Pass engine explicitly to metadata create_all/drop_all functions
  • Loading branch information
sde1000 committed Feb 5, 2025
1 parent 119177e commit 51a9f09
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions quicktill/dbutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,19 +271,19 @@ def run(args):
return 1
if args.createdb:
engine = sqlalchemy.create_engine("postgresql+psycopg2:///postgres")
conn = engine.connect()
conn.execute('commit')
conn.execute(f'create database "{args.tempdb}"')
conn.close()
raw_connection = engine.raw_connection()
with raw_connection.cursor() as cursor:
cursor.execute('commit')
cursor.execute(f'create database "{args.tempdb}"')
raw_connection.close()
try:
engine = sqlalchemy.create_engine(
f"postgresql+psycopg2:///{args.tempdb}")
models.metadata.bind = engine
try:
models.metadata.create_all()
models.metadata.create_all(engine)
pristine_schema = subprocess.check_output(
["pg_dump", "-s", "-O", args.tempdb])
models.metadata.drop_all()
models.metadata.drop_all(engine)
finally:
# If we don't explicitly close the connection to the
# database here, we won't be able to drop it
Expand All @@ -292,10 +292,11 @@ def run(args):
if args.createdb:
engine = sqlalchemy.create_engine(
"postgresql+psycopg2:///postgres")
conn = engine.connect()
conn.execute('commit')
conn.execute(f'drop database "{args.tempdb}"')
conn.close()
raw_connection = engine.raw_connection()
with raw_connection.cursor() as cursor:
cursor.execute('commit')
cursor.execute(f'drop database "{args.tempdb}"')
raw_connection.close()
current = tempfile.NamedTemporaryFile(delete=False)
current.write(current_schema)
current.close()
Expand Down

0 comments on commit 51a9f09

Please sign in to comment.