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

allow per-instrument database table reset #1031

Merged
merged 12 commits into from Sep 26, 2022
17 changes: 17 additions & 0 deletions jwql/database/database_interface.py
Expand Up @@ -447,5 +447,22 @@ def set_read_permissions():
FGSReadnoiseQueryHistory = monitor_orm_factory('fgs_readnoise_query_history')
FGSReadnoiseStats = monitor_orm_factory('fgs_readnoise_stats')

INSTRUMENT_TABLES = {
'nircam': [NIRCamDarkQueryHistory, NIRCamDarkPixelStats, NIRCamDarkDarkCurrent,
NIRCamBiasQueryHistory, NIRCamBiasStats, NIRCamBadPixelQueryHistory,
NIRCamBadPixelStats, NIRCamReadnoiseQueryHistory, NIRCamReadnoiseStats],
'niriss': [NIRISSDarkQueryHistory, NIRISSDarkPixelStats, NIRISSDarkDarkCurrent,
NIRISSBiasQueryHistory, NIRISSBiasStats, NIRISSBadPixelQueryHistory,
NIRISSBadPixelStats, NIRISSReadnoiseQueryHistory, NIRISSReadnoiseStats],
'miri': [MIRIDarkQueryHistory, MIRIDarkPixelStats, MIRIDarkDarkCurrent,
MIRIBadPixelQueryHistory, MIRIBadPixelStats, MIRIReadnoiseQueryHistory,
MIRIReadnoiseStats],
'nirspec': [NIRSpecDarkQueryHistory, NIRSpecDarkPixelStats, NIRSpecDarkDarkCurrent,
NIRSpecBiasQueryHistory, NIRSpecBiasStats, NIRSpecBadPixelQueryHistory,
NIRSpecBadPixelStats, NIRSpecReadnoiseQueryHistory, NIRSpecReadnoiseStats],
'fgs': [FGSDarkQueryHistory, FGSDarkPixelStats, FGSDarkDarkCurrent,
FGSBadPixelQueryHistory, FGSBadPixelStats, FGSReadnoiseQueryHistory,
FGSReadnoiseStats]}

if __name__ == '__main__':
base.metadata.create_all(engine)
27 changes: 22 additions & 5 deletions jwql/database/reset_database.py
Expand Up @@ -24,23 +24,40 @@
``postgresql+psycopg2://user:password@host:port/database``.
"""

from jwql.database.database_interface import base, set_read_permissions
import argparse

from jwql.database.database_interface import base, set_read_permissions, INSTRUMENT_TABLES
from jwql.utils.utils import get_config


if __name__ == '__main__':

parser = argparse.ArgumentParser(description='Reset JWQL database tables')
parser.add_argument('instrument', metavar='INSTRUMENT', type=str,
help='instrument tables to reset ("all" for all)',
default='all')
args = parser.parse_args()

instrument = args.instrument

connection_string = get_config()['connection_string']
server_type = connection_string.split('@')[-1][0]

assert server_type != 'p', 'Cannot reset production database!'

prompt = ('About to reset all tables for database instance {}. Do you '
'wish to proceed? (y/n)\n'.format(connection_string))
prompt = ('About to reset {} tables for database instance {}. Do you '
'wish to proceed? (y/n)\n'.format(instrument, connection_string))
response = input(prompt)

if response.lower() == 'y':
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add something here for n and user input that aren't y/n?

if response.lower() == 'y':
    delete tables
elif response.lower() == 'n'
    print('selected n, exiting')
else:
    print("{} is not valid input, exiting".format(input)) 

Might be overkill, but besides that, this LGTM!

base.metadata.drop_all()
base.metadata.create_all()
if instrument.lower() == 'all':
base.metadata.drop_all()
base.metadata.create_all()
elif instrument.lower() in ['nircam', 'nirspec', 'niriss', 'miri', 'fgs']:
tables = [x.__table__ for x in INSTRUMENT_TABLES[instrument]]
base.metadata.drop_all(tables=tables)
base.metadata.create_all(tables=tables)
else:
raise ValueError("Unknown instrument {}".format(instrument))
set_read_permissions()
print('\nDatabase instance {} has been reset'.format(connection_string))