add image flow
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
from typing import Generator
|
||||
|
||||
from sqlalchemy import create_engine, event
|
||||
from sqlalchemy import create_engine, event, text
|
||||
from sqlalchemy.engine import make_url
|
||||
from sqlalchemy.orm import DeclarativeBase, Session, sessionmaker
|
||||
|
||||
@@ -68,3 +68,41 @@ def init_db(database_url: str | None = None) -> None:
|
||||
configure_database(database_url)
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
_sync_sqlite_image_columns()
|
||||
|
||||
|
||||
def _sync_sqlite_image_columns() -> None:
|
||||
if engine is None or engine.dialect.name != "sqlite":
|
||||
return
|
||||
|
||||
image_columns = {
|
||||
"boxes": {
|
||||
"image_blob": "BLOB",
|
||||
"image_mime_type": "VARCHAR(50)",
|
||||
"image_width": "INTEGER",
|
||||
"image_height": "INTEGER",
|
||||
},
|
||||
"items": {
|
||||
"image_blob": "BLOB",
|
||||
"image_mime_type": "VARCHAR(50)",
|
||||
"image_width": "INTEGER",
|
||||
"image_height": "INTEGER",
|
||||
},
|
||||
"subitems": {
|
||||
"image_blob": "BLOB",
|
||||
"image_mime_type": "VARCHAR(50)",
|
||||
"image_width": "INTEGER",
|
||||
"image_height": "INTEGER",
|
||||
},
|
||||
}
|
||||
|
||||
with engine.begin() as connection:
|
||||
for table_name, columns in image_columns.items():
|
||||
existing_columns = {
|
||||
row[1] for row in connection.execute(text(f"PRAGMA table_info({table_name})"))
|
||||
}
|
||||
for column_name, column_type in columns.items():
|
||||
if column_name not in existing_columns:
|
||||
connection.execute(
|
||||
text(f"ALTER TABLE {table_name} ADD COLUMN {column_name} {column_type}")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user