Files
stl-repo/app/migrate.py
Consultoria AS 14b307110d feat: implementar 12 mejoras, tests, docs y optimizaciones
- Fase A: license templates, search history, cost estimator
- Fase B: import URL, bulk ZIP, batch download
- Fase C: comparison mode, mesh validation, measurement tool
- Fase D: cross-section clipping, overhang heatmap, layer animation
- Refactor Pydantic/SQLAlchemy warnings
- 24 tests pytest
- README actualizado
- WebP thumbnails, lazy loading, cache headers
2026-04-27 09:14:58 +00:00

60 lines
2.5 KiB
Python

import os
from sqlalchemy import text
from app.database import engine, SessionLocal
from app.models import Base, Tag, ModelFile
def run_migrations():
"""Run manual migrations for SQLite."""
Base.metadata.create_all(bind=engine)
db = SessionLocal()
try:
# Check if we need to migrate old tags (CSV string) to new tag structure
result = db.execute(text("SELECT name FROM sqlite_master WHERE type='table' AND name='tags'"))
if result.fetchone():
# Check if there are models with old-style tags (comma separated) and no model_tags entries
from app.models import Model3D
models = db.query(Model3D).all()
for model in models:
# Migrate tags if model has tags string but no tag relationships
if not model.tags and model.__dict__.get('tags_col'):
tag_names = [t.strip().lower() for t in model.tags_col.split(',') if t.strip()]
for name in tag_names:
tag = db.query(Tag).filter(Tag.name == name).first()
if not tag:
tag = Tag(name=name)
db.add(tag)
db.flush()
if tag not in model.tags:
model.tags.append(tag)
# Check if we need to migrate files to model_files
result = db.execute(text("SELECT name FROM sqlite_master WHERE type='table' AND name='model_files'"))
if result.fetchone():
from app.models import Model3D
models = db.query(Model3D).all()
for model in models:
# Check if model already has file records
if not model.files:
file_path = os.path.join("uploads", model.filename)
if os.path.exists(file_path):
mf = ModelFile(
model_id=model.id,
filename=model.filename,
file_path=file_path,
file_type='stl',
is_primary=True,
file_size=model.file_size,
file_hash=model.file_hash,
)
db.add(mf)
db.commit()
print("Migrations completed successfully")
except Exception as e:
print(f"Migration error: {e}")
db.rollback()
finally:
db.close()