Files
lego-instructions-manager/migrate_add_part_labels.py

57 lines
1.8 KiB
Python

"""
Migration: parts drawer labelling.
Creates part_categories and part_labels, makes uploads/parts, and seeds the
starter taxonomy. Safe to run more than once - every step checks first.
python migrate_add_part_labels.py
You do not strictly need this: app/services/schema_upgrade.py creates both
tables at boot once the wiring patch is applied. It exists so you can run the
change deliberately, and see what it did, before restarting the service.
Created by: Jess Rogerson (yelling commands at Claude.AI)
"""
import os
import sys
from sqlalchemy import inspect
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import create_app, db # noqa: E402
from app.models.part_label import PartCategory, PartLabel # noqa: E402
from app.services.part_catalog import seed_categories # noqa: E402
def main():
app = create_app(os.environ.get('FLASK_CONFIG', 'default'))
with app.app_context():
inspector = inspect(db.engine)
existing = set(inspector.get_table_names())
for model in (PartCategory, PartLabel):
if model.__tablename__ in existing:
print(f' = table {model.__tablename__} already present')
else:
model.__table__.create(db.engine, checkfirst=True)
print(f' + created table {model.__tablename__}')
parts_folder = os.path.join(app.config['UPLOAD_FOLDER'], 'parts')
os.makedirs(parts_folder, exist_ok=True)
print(f' = part images folder: {parts_folder}')
created = seed_categories(force=True)
if created:
print(f' + seeded {created} categories')
else:
print(' = categories already seeded (nothing changed)')
print('\nDone. Parts Labels lives at /admin/labels (admins only).')
if __name__ == '__main__':
main()