"""Add numeric price without overwriting legacy sample notes/location.""" import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) if '--local' in sys.argv: import test_wsgi from services.db import get_db def migrate(): conn = get_db() try: with conn.cursor() as cur: cur.execute('SHOW COLUMNS FROM spot_confirmations LIKE %s', ('product_unit_price',)) column = cur.fetchone() if not column: cur.execute('ALTER TABLE spot_confirmations ADD COLUMN product_unit_price VARCHAR(1000) NULL') # Only unambiguous numeric notes can be treated as an old price. cur.execute("""UPDATE spot_confirmations SET product_unit_price=TRIM(sample_remark) WHERE product_unit_price IS NULL AND TRIM(sample_remark) REGEXP '^[0-9]{1,14}([.][0-9]{1,4})?$'""") else: cur.execute('ALTER TABLE spot_confirmations MODIFY COLUMN product_unit_price VARCHAR(1000) NULL') conn.commit() print('spot_confirmations.product_unit_price ready') finally: conn.close() if __name__ == '__main__': migrate()