Initial commit

This commit is contained in:
Alex
2026-10-07 19:57:07 +00:00
commit 6e6035c789
131 changed files with 23101 additions and 0 deletions
Binary file not shown.
+56
View File
@@ -0,0 +1,56 @@
# Alembic Configuration
# This file is generated by alembic to configure the migration environment
from logging.config import fileConfig
from sqlalchemy import engine_from_config, pool
from alembic import context
# This is the Alembic Config object
config = context.config
# Interpret the config file for Python logging
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# Import your models here
from app.models import Base
# Add your model's MetaData object here for 'autogenerate' support
target_metadata = Base.metadata
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode."""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
render_as_batch=True, # Required for SQLite FK support
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode."""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
render_as_batch=True, # Required for SQLite FK support
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
@@ -0,0 +1,366 @@
"""Initial V1 schema - consistent domain model
Revision ID: 612bd62057c2
Revises:
Create Date: 2026-10-06 16:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = '612bd62057c2'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table('customers',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
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')
)
op.create_table('projects',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
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')
)
op.create_table('softwares',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
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')
)
op.create_table('modules',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=100), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('software_id', sa.Integer(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('name'),
sa.ForeignKeyConstraint(['software_id'], ['softwares.id'], ondelete='SET NULL')
)
op.create_table('vendors',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('contact_person', sa.String(length=255), nullable=True),
sa.Column('phone', sa.String(length=50), nullable=True),
sa.Column('email', sa.String(length=255), nullable=True),
sa.Column('address', sa.Text(), nullable=True),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_table('contracts',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('project_id', sa.Integer(), nullable=False),
sa.Column('contract_number', sa.String(length=100), nullable=False),
sa.Column('contract_date', sa.DateTime(), nullable=True),
sa.Column('start_date', sa.DateTime(), nullable=True),
sa.Column('end_date', sa.DateTime(), nullable=True),
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.UniqueConstraint('project_id', 'contract_number', name='uq_contract_project_number'),
sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ondelete='CASCADE')
)
op.create_table('documents',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('project_id', sa.Integer(), nullable=True),
sa.Column('document_type', sa.String(length=50), nullable=True),
sa.Column('document_number', sa.String(length=100), nullable=False),
sa.Column('document_date', sa.DateTime(), nullable=True),
sa.Column('title', sa.String(length=255), nullable=True),
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='SET NULL')
)
op.create_table('legal_entities',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('inn', sa.String(length=50), nullable=True),
sa.Column('kpp', sa.String(length=50), nullable=True),
sa.Column('address', sa.Text(), nullable=True),
sa.Column('customer_id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ondelete='RESTRICT')
)
op.create_table('systems',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('project_id', sa.Integer(), nullable=False),
sa.Column('customer_id', sa.Integer(), nullable=False),
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(['customer_id'], ['customers.id'], ondelete='RESTRICT')
)
op.create_table('guardians',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('name', sa.String(length=255), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.Column('serial_number', sa.String(length=100), nullable=True),
sa.Column('location', sa.String(length=255), nullable=True),
sa.Column('system_id', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.UniqueConstraint('system_id', 'serial_number', name='uq_guardian_system_serial'),
sa.ForeignKeyConstraint(['system_id'], ['systems.id'], ondelete='CASCADE')
)
op.create_table('project_customers',
sa.Column('project_id', sa.Integer(), nullable=False),
sa.Column('customer_id', sa.Integer(), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('project_id', 'customer_id'),
sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['customer_id'], ['customers.id'], ondelete='CASCADE')
)
op.create_table('project_legal_entities',
sa.Column('project_id', sa.Integer(), nullable=False),
sa.Column('legal_entity_id', sa.Integer(), nullable=False),
sa.Column('description', sa.Text(), nullable=True),
sa.PrimaryKeyConstraint('project_id', 'legal_entity_id'),
sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['legal_entity_id'], ['legal_entities.id'], ondelete='CASCADE')
)
op.create_table('purchase_items',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('purchase_id', sa.Integer(), nullable=False),
sa.Column('module_id', sa.Integer(), nullable=False),
sa.Column('quantity', sa.Integer(), nullable=False),
sa.Column('unit_price', sa.Numeric(precision=12, scale=2), nullable=True),
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(['purchase_id'], ['purchases.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['module_id'], ['modules.id'], ondelete='RESTRICT')
)
op.create_table('purchase_specifications',
sa.Column('purchase_id', sa.Integer(), nullable=False),
sa.Column('specification_id', sa.Integer(), nullable=False),
sa.PrimaryKeyConstraint('purchase_id', 'specification_id'),
sa.ForeignKeyConstraint(['purchase_id'], ['purchases.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['specification_id'], ['specifications.id'], ondelete='CASCADE')
)
op.create_table('purchases',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('project_id', sa.Integer(), nullable=False),
sa.Column('vendor_id', sa.Integer(), nullable=False),
sa.Column('purchase_number', sa.String(length=100), nullable=False),
sa.Column('purchase_date', sa.DateTime(), nullable=True),
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(['vendor_id'], ['vendors.id'], ondelete='RESTRICT')
)
op.create_table('resource_allocations',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('resource_id', sa.Integer(), nullable=False),
sa.Column('system_id', sa.Integer(), nullable=False),
sa.Column('guardian_id', sa.Integer(), nullable=True),
sa.Column('quantity', sa.Integer(), nullable=False),
sa.Column('allocation_date', sa.DateTime(), nullable=True),
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(['resource_id'], ['resources.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['system_id'], ['systems.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['guardian_id'], ['guardians.id'], ondelete='SET NULL')
)
op.create_table('resources',
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),
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.create_table('specification_items',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('specification_id', sa.Integer(), nullable=False),
sa.Column('module_id', sa.Integer(), nullable=False),
sa.Column('quantity', sa.Integer(), nullable=False),
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(['specification_id'], ['specifications.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['module_id'], ['modules.id'], ondelete='RESTRICT')
)
op.create_table('specifications',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('project_id', sa.Integer(), nullable=False),
sa.Column('contract_id', sa.Integer(), nullable=False),
sa.Column('specification_number', sa.String(length=100), nullable=False),
sa.Column('specification_date', sa.DateTime(), nullable=True),
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.UniqueConstraint('contract_id', 'specification_number', name='uq_specification_contract_number'),
sa.ForeignKeyConstraint(['project_id'], ['projects.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['contract_id'], ['contracts.id'], ondelete='CASCADE')
)
op.create_table('system_modules',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('system_id', sa.Integer(), nullable=False),
sa.Column('guardian_id', sa.Integer(), nullable=True),
sa.Column('module_id', sa.Integer(), nullable=False),
sa.Column('quantity', sa.Integer(), nullable=False),
sa.Column('created_at', sa.DateTime(), nullable=True),
sa.Column('updated_at', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id'),
sa.ForeignKeyConstraint(['system_id'], ['systems.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['guardian_id'], ['guardians.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['module_id'], ['modules.id'], ondelete='CASCADE')
)
op.create_table('contract_addendums',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('contract_id', sa.Integer(), nullable=False),
sa.Column('addendum_number', sa.String(length=100), nullable=False),
sa.Column('addendum_date', sa.DateTime(), nullable=True),
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(['contract_id'], ['contracts.id'], ondelete='CASCADE')
)
op.create_table('document_dispatches',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('document_id', sa.Integer(), nullable=False),
sa.Column('dispatch_date', sa.DateTime(), nullable=True),
sa.Column('recipient', sa.String(length=255), nullable=True),
sa.Column('package', sa.String(length=255), nullable=True),
sa.Column('channel', sa.String(length=50), nullable=True),
sa.Column('status', sa.String(length=50), nullable=True),
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(['document_id'], ['documents.id'], ondelete='CASCADE')
)
op.create_table('document_files',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('document_id', sa.Integer(), nullable=False),
sa.Column('file_path', sa.String(length=500), nullable=False),
sa.Column('file_name', sa.String(length=255), nullable=False),
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(['document_id'], ['documents.id'], ondelete='CASCADE')
)
op.create_table('invoices',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('purchase_id', sa.Integer(), nullable=False),
sa.Column('vendor_id', sa.Integer(), nullable=False),
sa.Column('invoice_number', sa.String(length=100), nullable=False),
sa.Column('invoice_date', sa.DateTime(), nullable=True),
sa.Column('amount', sa.Numeric(precision=12, scale=2), nullable=True),
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(['purchase_id'], ['purchases.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['vendor_id'], ['vendors.id'], ondelete='RESTRICT')
)
op.create_table('shipments',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('purchase_id', sa.Integer(), nullable=False),
sa.Column('shipment_number', sa.String(length=100), nullable=True),
sa.Column('shipment_date', sa.DateTime(), nullable=True),
sa.Column('tracking_number', sa.String(length=100), nullable=True),
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(['purchase_id'], ['purchases.id'], ondelete='CASCADE')
)
def downgrade() -> None:
op.drop_table('shipments')
op.drop_table('invoices')
op.drop_table('document_files')
op.drop_table('document_dispatches')
op.drop_table('contract_addendums')
op.drop_table('system_modules')
op.drop_table('specifications')
op.drop_table('specification_items')
op.drop_table('resources')
op.drop_table('purchase_specifications')
op.drop_table('purchases')
op.drop_table('purchase_items')
op.drop_table('project_legal_entities')
op.drop_table('project_customers')
op.drop_table('guardians')
op.drop_table('systems')
op.drop_table('legal_entities')
op.drop_table('documents')
op.drop_table('contracts')
op.drop_table('vendors')
op.drop_table('modules')
op.drop_table('softwares')
op.drop_table('projects')
op.drop_table('customers')
View File
@@ -0,0 +1,139 @@
"""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')
@@ -0,0 +1,114 @@
"""Correct Resource.invoice_id FK behavior
Revision ID: b2c3d4e5f6a7
Revises: a1b2c3d4e5f6
Create Date: 2026-10-07 11:00:00.000000
Changes:
1. Resource.invoice_id FK: ON DELETE CASCADE → RESTRICT
2. NOT NULL test corrected
3. Invoice deletion restriction test added
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers
revision: str = 'b2c3d4e5f6a7'
down_revision: Union[str, None] = 'a1b2c3d4e5f6'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Change Resource.invoice_id FK from CASCADE to RESTRICT."""
# Для SQLite нужно пересоздать таблицу с новым FK behavior
# Сначала создаем временную таблицу без FK constraint на invoice_id
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),
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='RESTRICT')
)
# Переносим данные
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
""")
# Удаляем старую таблицу
op.execute("DROP TABLE resources")
# Переименовываем
op.rename_table('resources_temp', 'resources')
def downgrade() -> None:
"""Rollback FK behavior to CASCADE."""
# Пересоздаем таблицу с CASCADE
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=False),
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')
)
# Переносим данные
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')
@@ -0,0 +1,113 @@
"""Correct Resource.invoice_id FK and NOT NULL in model
Revision ID: c3d4e5f6a7b8
Revises: b2c3d4e5f6a7
Create Date: 2026-10-07 12:00:00.000000
Changes:
1. Update Resource.invoice_id: SET NULL/NULLABLE → RESTRICT/NOT NULL in SQLAlchemy model
2. Database schema updated to match
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers
revision: str = 'c3d4e5f6a7b8'
down_revision: Union[str, None] = 'b2c3d4e5f6a7'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Update Resource.invoice_id FK and NOT NULL."""
# Для SQLite нужно пересоздать таблицу с новыми constraints
# Создаем временную таблицу с новыми 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),
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='RESTRICT')
)
# Переносим данные
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
""")
# Удаляем старую таблицу
op.execute("DROP TABLE resources")
# Переименовываем
op.rename_table('resources_temp', 'resources')
def downgrade() -> None:
"""Rollback Resource.invoice_id FK and NOT NULL."""
# Пересоздаем таблицу с SET NULL/nullable
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),
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')