33 lines
841 B
Python
33 lines
841 B
Python
from pathlib import Path
|
|
import sys
|
|
|
|
from sqlalchemy import text
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from app.database import engine # noqa: E402
|
|
|
|
|
|
def column_exists(conn, column_name: str) -> bool:
|
|
return bool(conn.execute(text("""
|
|
SELECT COUNT(1)
|
|
FROM information_schema.columns
|
|
WHERE table_schema = DATABASE()
|
|
AND table_name = 'products'
|
|
AND column_name = :column_name
|
|
"""), {"column_name": column_name}).scalar())
|
|
|
|
|
|
def main() -> None:
|
|
with engine.begin() as conn:
|
|
if not column_exists(conn, "supplier"):
|
|
conn.execute(text(
|
|
"ALTER TABLE products ADD COLUMN supplier VARCHAR(255) NULL AFTER material_name"
|
|
))
|
|
print("product supplier field migrated")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|