39 lines
1.1 KiB
Python
39 lines
1.1 KiB
Python
"""HartOMat standard materials — add hartomat_code column and seed 35 materials
|
|
|
|
Revision ID: 019
|
|
Revises: 018
|
|
Create Date: 2026-03-02
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
revision = "019"
|
|
down_revision = "018"
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
op.add_column("materials", sa.Column("hartomat_code", sa.Integer(), nullable=True))
|
|
|
|
from app.data.hartomat_materials import HARTOMAT_MATERIALS
|
|
|
|
conn = op.get_bind()
|
|
now = datetime.utcnow().isoformat()
|
|
for mat in HARTOMAT_MATERIALS:
|
|
desc = mat["description"].replace("'", "''")
|
|
name = mat["name"].replace("'", "''")
|
|
conn.execute(sa.text(
|
|
f"INSERT INTO materials (id, name, description, source, hartomat_code, created_at, updated_at) "
|
|
f"VALUES ('{uuid.uuid4()}', '{name}', '{desc}', '{mat['source']}', "
|
|
f"{mat['hartomat_code']}, '{now}', '{now}') "
|
|
f"ON CONFLICT (name) DO NOTHING"
|
|
))
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.execute("DELETE FROM materials WHERE source = 'hartomat_standard'")
|
|
op.drop_column("materials", "hartomat_code")
|