am-freescan/scripts/apply_fixture_trajectory.py

27 lines
1.8 KiB
Python
Raw Normal View History

"""Create the additive fixture association table in the designated dev database only."""
from pathlib import Path
import json,sys
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT/'scripts'))
from bootstrap_db import validate_target
def apply():
import pymysql
runtime=json.loads((ROOT/'.local/runtime.json').read_text(encoding='utf-8-sig'))['mysql'];validate_target(runtime)
sql=(ROOT/'am-backend/sql/freescan/015_fixture_trajectory.sql').read_text(encoding='utf-8')
statements=['\n'.join(line for line in part.splitlines() if not line.lstrip().startswith('--')).strip() for part in sql.split(';')]
if any(not s.upper().startswith('CREATE TABLE IF NOT EXISTS LAB_FIXTURE_TRAJECTORY ') for s in statements if s):raise ValueError('Unexpected migration')
db=pymysql.connect(host=runtime['host'],port=int(runtime.get('port',3306)),user=runtime['user'],password=runtime['password'],database=runtime['database'],charset='utf8mb4',autocommit=True)
with db.cursor() as cursor:
cursor.execute('SELECT DATABASE()');assert cursor.fetchone()[0]=='am-freescan'
before={}
for table in ['lab_fixture','lab_fixture_framework','lab_test_task','lab_execution','lab_resource_occupation']:
cursor.execute('SELECT COUNT(*) FROM `'+table+'`');before[table]=cursor.fetchone()[0]
for statement in statements:
if statement:cursor.execute(statement)
for table,count in before.items():
cursor.execute('SELECT COUNT(*) FROM `'+table+'`');assert cursor.fetchone()[0]==count,table
cursor.execute('SELECT COUNT(*) FROM lab_fixture_trajectory');associations=cursor.fetchone()[0]
(ROOT/'.local/fixture-trajectory-migration.json').write_text(json.dumps({'baselineCounts':before,'associationRows':associations,'customerCountsPreserved':True},indent=2),encoding='utf-8')
db.close();print('Fixture trajectory table ready; existing rows preserved.')
if __name__=='__main__':apply()