140 lines
5.8 KiB
Python
140 lines
5.8 KiB
Python
"""Purchase schema corrections
|
|
|
|
Revision ID: a1b2c3d4e5f6
|
|
Revises: 612bd62057c2
|
|
Create Date: 2026-10-07 10:00:00.000000
|
|
|
|
Changes:
|
|
1. Resource.invoice_id → NOT NULL
|
|
2. PurchaseItem → UNIQUE(purchase_id, module_id)
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
# revision identifiers
|
|
revision: str = 'a1b2c3d4e5f6'
|
|
down_revision: Union[str, None] = '612bd62057c2'
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
"""Apply schema corrections for purchase items and resources."""
|
|
|
|
# 1. Добавить UNIQUE constraint на (purchase_id, module_id) для PurchaseItem
|
|
# Используем batch_op для SQLite
|
|
|
|
with op.batch_alter_table('purchase_items') as batch_op:
|
|
batch_op.create_unique_constraint(
|
|
'uq_purchase_item_purchase_module',
|
|
['purchase_id', 'module_id']
|
|
)
|
|
|
|
# 2. Сделать Resource.invoice_id NOT NULL
|
|
# Для SQLite нужно создать временную таблицу, перенести данные и переименовать
|
|
|
|
# Создаем временную таблицу с новыми constraints
|
|
op.create_table(
|
|
'resources_temp',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('project_id', sa.Integer(), nullable=False),
|
|
sa.Column('module_id', sa.Integer(), nullable=False),
|
|
sa.Column('quantity', sa.Integer(), nullable=False),
|
|
sa.Column('invoice_number', sa.String(length=100), nullable=True),
|
|
sa.Column('invoice_date', sa.DateTime(), nullable=True),
|
|
sa.Column('specification_number', sa.String(length=100), nullable=True),
|
|
sa.Column('specification_id', sa.Integer(), nullable=True),
|
|
sa.Column('invoice_id', sa.Integer(), nullable=False), # Изменено на NOT NULL
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['module_id'], ['modules.id'], ondelete='RESTRICT'),
|
|
sa.ForeignKeyConstraint(['specification_id'], ['specifications.id'], ondelete='SET NULL'),
|
|
sa.ForeignKeyConstraint(['invoice_id'], ['invoices.id'], ondelete='CASCADE')
|
|
)
|
|
|
|
# Переносим только записи с invoice_id IS NOT NULL
|
|
op.execute("""
|
|
INSERT INTO resources_temp (
|
|
id, project_id, module_id, quantity, invoice_number,
|
|
invoice_date, specification_number, specification_id,
|
|
invoice_id, description, created_at, updated_at
|
|
)
|
|
SELECT
|
|
id, project_id, module_id, quantity, invoice_number,
|
|
invoice_date, specification_number, specification_id,
|
|
invoice_id, description, created_at, updated_at
|
|
FROM resources
|
|
WHERE invoice_id IS NOT NULL
|
|
""")
|
|
|
|
# Удаляем записи без invoice_id
|
|
op.execute("DELETE FROM resources WHERE invoice_id IS NULL")
|
|
|
|
# Удаляем старую таблицу
|
|
op.execute("DROP TABLE resources")
|
|
|
|
# Переименовываем временную таблицу
|
|
op.rename_table('resources_temp', 'resources')
|
|
|
|
|
|
def downgrade() -> None:
|
|
"""Rollback schema corrections."""
|
|
|
|
# 1. Удаляем UNIQUE constraint
|
|
with op.batch_alter_table('purchase_items') as batch_op:
|
|
try:
|
|
batch_op.drop_constraint(
|
|
'uq_purchase_item_purchase_module',
|
|
type_='unique'
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
# 2. Возвращаем Resource.invoice_id как nullable
|
|
# Создаем временную таблицу с old schema
|
|
op.create_table(
|
|
'resources_temp_old',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('project_id', sa.Integer(), nullable=False),
|
|
sa.Column('module_id', sa.Integer(), nullable=False),
|
|
sa.Column('quantity', sa.Integer(), nullable=False),
|
|
sa.Column('invoice_number', sa.String(length=100), nullable=True),
|
|
sa.Column('invoice_date', sa.DateTime(), nullable=True),
|
|
sa.Column('specification_number', sa.String(length=100), nullable=True),
|
|
sa.Column('specification_id', sa.Integer(), nullable=True),
|
|
sa.Column('invoice_id', sa.Integer(), nullable=True), # Возвращаем nullable
|
|
sa.Column('description', sa.Text(), nullable=True),
|
|
sa.Column('created_at', sa.DateTime(), nullable=True),
|
|
sa.Column('updated_at', sa.DateTime(), nullable=True),
|
|
sa.PrimaryKeyConstraint('id'),
|
|
sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ondelete='CASCADE'),
|
|
sa.ForeignKeyConstraint(['module_id'], ['modules.id'], ondelete='RESTRICT'),
|
|
sa.ForeignKeyConstraint(['specification_id'], ['specifications.id'], ondelete='SET NULL'),
|
|
sa.ForeignKeyConstraint(['invoice_id'], ['invoices.id'], ondelete='SET NULL')
|
|
)
|
|
|
|
# Переносим данные
|
|
op.execute("""
|
|
INSERT INTO resources_temp_old (
|
|
id, project_id, module_id, quantity, invoice_number,
|
|
invoice_date, specification_number, specification_id,
|
|
invoice_id, description, created_at, updated_at
|
|
)
|
|
SELECT
|
|
id, project_id, module_id, quantity, invoice_number,
|
|
invoice_date, specification_number, specification_id,
|
|
invoice_id, description, created_at, updated_at
|
|
FROM resources
|
|
""")
|
|
|
|
# Удаляем новую таблицу
|
|
op.execute("DROP TABLE resources")
|
|
|
|
# Переименовываем
|
|
op.rename_table('resources_temp_old', 'resources')
|