Skip to content

Commit dc9cb97

Browse files
committed
Fix update command so it works with SQLite (and possibly other relational DBs)
1 parent 00f9ead commit dc9cb97

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

python/rcdb/cli/db.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,21 @@ def db(ctx):
2929
print("Schema version: {} - '{}'".format(schema_version.version, schema_version.comment))
3030

3131

32-
# add a command to the 'db' group
3332
@db.command()
3433
@pass_rcdb_context
3534
def update(context):
3635
provider = RCDBProvider(context.connection_str, check_version=False)
3736

3837
# Check something exists
3938
if not sqlalchemy.inspect(provider.engine).has_table(SchemaVersion.__tablename__):
40-
print('The schema version table does not exists. It looks like RCDB v1.')
39+
print('The schema version table does not exist. It looks like RCDB v1.')
4140
current_version = 1
4241
else:
4342
print('Found schema version table')
4443
# Check schema version
4544
current_version = provider.get_schema_version()
4645

47-
if current_version !=1:
46+
if current_version != 1:
4847
print(f"Can't update schema version. Current version is: {current_version.version}. This command can update:")
4948
print(f" DB v1 --> v2")
5049
return
@@ -62,8 +61,6 @@ def update(context):
6261
if not click.confirm('Do you really want to continue?'):
6362
return
6463

65-
# TODO move next it to provider, create schema_update_v1_v2 function !!!
66-
6764
# That we will need for DB
6865
metadata = rcdb.model.Base.metadata
6966
provider = RCDBProvider(context.connection_str, check_version=False)
@@ -74,21 +71,31 @@ def update(context):
7471
# Create run periods table
7572
RunPeriod.__table__.create(provider.engine)
7673

77-
with provider.engine.connect() as conn:
78-
conn.execute("""
79-
DROP TABLE IF EXISTS `trigger_thresholds` ;
80-
DROP TABLE IF EXISTS `trigger_masks` ;
81-
DROP TABLE IF EXISTS `readout_thresholds` ;
82-
DROP TABLE IF EXISTS `readout_masks` ;
83-
DROP TABLE IF EXISTS `dac_presets` ;
84-
DROP TABLE IF EXISTS `crates` ;
85-
DROP TABLE IF EXISTS `boards` ;
86-
DROP TABLE IF EXISTS `board_installations_have_runs` ;
87-
DROP TABLE IF EXISTS `board_installations` ;
88-
DROP TABLE IF EXISTS `board_configurations_have_runs` ;
89-
DROP TABLE IF EXISTS `board_configurations` ;
90-
DROP TABLE IF EXISTS `alembic_version` ;
91-
""")
74+
# List of old tables to drop
75+
tables_to_drop = [
76+
'trigger_thresholds',
77+
'trigger_masks',
78+
'readout_thresholds',
79+
'readout_masks',
80+
'dac_presets',
81+
'crates',
82+
'boards',
83+
'board_installations_have_runs',
84+
'board_installations',
85+
'board_configurations_have_runs',
86+
'board_configurations',
87+
'alembic_version'
88+
]
89+
90+
with provider.engine.begin() as conn:
91+
inspector = sqlalchemy.inspect(conn)
92+
existing_tables = inspector.get_table_names()
93+
94+
# Drop each table if it is present
95+
for t in tables_to_drop:
96+
if t in existing_tables:
97+
print(f"Dropping table '{t}'")
98+
conn.execute(sqlalchemy.text(f"DROP TABLE {t}"))
9299

93100
# Set correct version
94101
version = stamp_schema_version(provider)

0 commit comments

Comments
 (0)