import json from sqlite3 import Connection, OperationalError, connect def upgrade_tables( schema: str, db: Connection ) -> None: tabs = json.load( open( schema ) ) for tab, cols in tabs.items(): cdesc = ', '.join( f'{c} {t}' for c, t in cols.items() ) cmd = f'create table if not exists {tab} ( {cdesc} )' db.execute( cmd ) for c, t in cols.items(): cmd = f'alter table {tab} add column {c} {t}' try: db.execute( cmd ) except OperationalError as e: if not str( e ).startswith( 'duplicate column' ): raise import run_tests