add new ui
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import sys
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy import create_engine, text, inspect
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from backend.database import SessionLocal as NewSession
|
||||
from backend.database import SessionLocal as NewSession, engine as new_engine
|
||||
from backend import models
|
||||
from pathlib import Path
|
||||
|
||||
OLD_DB_URL = "sqlite:///./backend/old_app.db"
|
||||
|
||||
OLD_DB_URL = "sqlite:///./instance/project.-10-11-25.db"
|
||||
old_engine = create_engine(OLD_DB_URL, connect_args={"check_same_thread": False})
|
||||
OldSession = sessionmaker(bind=old_engine)
|
||||
old_db = OldSession()
|
||||
@@ -14,7 +16,25 @@ new_db = NewSession()
|
||||
def log(msg: str):
|
||||
print(f"[INFO] {msg}", file=sys.stderr)
|
||||
|
||||
|
||||
def ensure_schema():
|
||||
"""Ensure new DB has required tables/columns (owners table and oboruds.owner_id)."""
|
||||
# Create any missing tables defined in models (e.g., owners)
|
||||
models.Base.metadata.create_all(bind=new_engine)
|
||||
|
||||
# If oboruds.owner_id is missing (older DB), add the column (SQLite allows simple ALTER)
|
||||
try:
|
||||
inspector = inspect(new_engine)
|
||||
cols = [c["name"] for c in inspector.get_columns("oboruds")]
|
||||
if "owner_id" not in cols:
|
||||
with new_engine.begin() as conn:
|
||||
conn.execute(text("ALTER TABLE oboruds ADD COLUMN owner_id INTEGER"))
|
||||
log("Добавлен столбец oboruds.owner_id")
|
||||
except Exception as e:
|
||||
log(f"Предупреждение: проверка/добавление owner_id не выполнена: {e}")
|
||||
|
||||
def migrate():
|
||||
ensure_schema()
|
||||
log("Запуск переноса данных из old_app.db → app.db")
|
||||
|
||||
# Тип оборудования по умолчанию
|
||||
|
||||
Reference in New Issue
Block a user