chore: 移除不必要的迁移脚本文件
This commit is contained in:
@ -1,79 +0,0 @@
|
|||||||
"""打通采购申请与采购入库的按单入库链路
|
|
||||||
|
|
||||||
Revision ID: a1b2c3d4e5f6
|
|
||||||
Revises: None (或填当前最新 revision)
|
|
||||||
Create Date: 2026-07-14
|
|
||||||
|
|
||||||
变更内容:
|
|
||||||
1. purchase_request 表新增 base_id (FK → material_base.id)
|
|
||||||
2. stock_buy 表新增 request_id (FK → purchase_request.id)
|
|
||||||
"""
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
|
|
||||||
|
|
||||||
# revision identifiers, used by Alembic.
|
|
||||||
revision = 'a1b2c3d4e5f6'
|
|
||||||
down_revision = None # ← 替换为当前最新 revision ID
|
|
||||||
branch_labels = None
|
|
||||||
depends_on = None
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade():
|
|
||||||
# ============================================================
|
|
||||||
# 1. purchase_request 新增 base_id
|
|
||||||
# ============================================================
|
|
||||||
with op.batch_alter_table('purchase_request', schema=None) as batch_op:
|
|
||||||
batch_op.add_column(
|
|
||||||
sa.Column('base_id', sa.Integer(), nullable=True,
|
|
||||||
comment='关联基础物料ID')
|
|
||||||
)
|
|
||||||
batch_op.create_foreign_key(
|
|
||||||
'fk_purchase_request_base_id',
|
|
||||||
'material_base',
|
|
||||||
['base_id'],
|
|
||||||
['id'],
|
|
||||||
ondelete='SET NULL'
|
|
||||||
)
|
|
||||||
batch_op.create_index(
|
|
||||||
'ix_purchase_request_base_id',
|
|
||||||
['base_id']
|
|
||||||
)
|
|
||||||
|
|
||||||
# ============================================================
|
|
||||||
# 2. stock_buy 新增 request_id
|
|
||||||
# ============================================================
|
|
||||||
with op.batch_alter_table('stock_buy', schema=None) as batch_op:
|
|
||||||
batch_op.add_column(
|
|
||||||
sa.Column('request_id', sa.Integer(), nullable=True,
|
|
||||||
comment='关联采购申请单ID')
|
|
||||||
)
|
|
||||||
batch_op.create_foreign_key(
|
|
||||||
'fk_stock_buy_request_id',
|
|
||||||
'purchase_request',
|
|
||||||
['request_id'],
|
|
||||||
['id'],
|
|
||||||
ondelete='SET NULL'
|
|
||||||
)
|
|
||||||
batch_op.create_index(
|
|
||||||
'ix_stock_buy_request_id',
|
|
||||||
['request_id']
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade():
|
|
||||||
# ============================================================
|
|
||||||
# 回滚:stock_buy 删除 request_id
|
|
||||||
# ============================================================
|
|
||||||
with op.batch_alter_table('stock_buy', schema=None) as batch_op:
|
|
||||||
batch_op.drop_constraint('fk_stock_buy_request_id', type_='foreignkey')
|
|
||||||
batch_op.drop_index('ix_stock_buy_request_id')
|
|
||||||
batch_op.drop_column('request_id')
|
|
||||||
|
|
||||||
# ============================================================
|
|
||||||
# 回滚:purchase_request 删除 base_id
|
|
||||||
# ============================================================
|
|
||||||
with op.batch_alter_table('purchase_request', schema=None) as batch_op:
|
|
||||||
batch_op.drop_constraint('fk_purchase_request_base_id', type_='foreignkey')
|
|
||||||
batch_op.drop_index('ix_purchase_request_base_id')
|
|
||||||
batch_op.drop_column('base_id')
|
|
||||||
@ -1,93 +0,0 @@
|
|||||||
-- ============================================================
|
|
||||||
-- migrations_add_purchase_stockbuy_link.sql
|
|
||||||
-- 打通「采购申请」与「采购入库」的按单入库链路
|
|
||||||
--
|
|
||||||
-- 变更内容:
|
|
||||||
-- 1. purchase_request 表新增 base_id 列(FK → material_base.id)
|
|
||||||
-- 2. stock_buy 表新增 request_id 列(FK → purchase_request.id)
|
|
||||||
--
|
|
||||||
-- 执行方式(任选一种):
|
|
||||||
-- psql -h <host> -U postgres -d inventory_system -f migrations_add_purchase_stockbuy_link.sql
|
|
||||||
-- 或在 pgAdmin / Navicat / DBeaver 中打开执行
|
|
||||||
-- ============================================================
|
|
||||||
|
|
||||||
BEGIN;
|
|
||||||
|
|
||||||
-- ============================================================
|
|
||||||
-- 1. purchase_request 新增 base_id
|
|
||||||
-- 硬关联基础物料表,使采购申请能精确锁定物料
|
|
||||||
-- ============================================================
|
|
||||||
ALTER TABLE purchase_request
|
|
||||||
ADD COLUMN IF NOT EXISTS base_id INTEGER;
|
|
||||||
|
|
||||||
-- 添加外键约束(如果列已存在但无约束,会补充)
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF NOT EXISTS (
|
|
||||||
SELECT 1 FROM information_schema.table_constraints
|
|
||||||
WHERE constraint_name = 'fk_purchase_request_base_id'
|
|
||||||
AND table_name = 'purchase_request'
|
|
||||||
) THEN
|
|
||||||
ALTER TABLE purchase_request
|
|
||||||
ADD CONSTRAINT fk_purchase_request_base_id
|
|
||||||
FOREIGN KEY (base_id) REFERENCES material_base(id)
|
|
||||||
ON DELETE SET NULL;
|
|
||||||
END IF;
|
|
||||||
END;
|
|
||||||
$$;
|
|
||||||
|
|
||||||
-- 为 base_id 添加索引(支持按物料筛选采购申请)
|
|
||||||
CREATE INDEX IF NOT EXISTS ix_purchase_request_base_id
|
|
||||||
ON purchase_request (base_id);
|
|
||||||
|
|
||||||
COMMENT ON COLUMN purchase_request.base_id IS '关联基础物料ID';
|
|
||||||
|
|
||||||
|
|
||||||
-- ============================================================
|
|
||||||
-- 2. stock_buy 新增 request_id
|
|
||||||
-- 关联采购申请单,使入库记录可追溯到来源采购单
|
|
||||||
-- ============================================================
|
|
||||||
ALTER TABLE stock_buy
|
|
||||||
ADD COLUMN IF NOT EXISTS request_id INTEGER;
|
|
||||||
|
|
||||||
-- 添加外键约束
|
|
||||||
DO $$
|
|
||||||
BEGIN
|
|
||||||
IF NOT EXISTS (
|
|
||||||
SELECT 1 FROM information_schema.table_constraints
|
|
||||||
WHERE constraint_name = 'fk_stock_buy_request_id'
|
|
||||||
AND table_name = 'stock_buy'
|
|
||||||
) THEN
|
|
||||||
ALTER TABLE stock_buy
|
|
||||||
ADD CONSTRAINT fk_stock_buy_request_id
|
|
||||||
FOREIGN KEY (request_id) REFERENCES purchase_request(id)
|
|
||||||
ON DELETE SET NULL;
|
|
||||||
END IF;
|
|
||||||
END;
|
|
||||||
$$;
|
|
||||||
|
|
||||||
-- 为 request_id 添加索引(支持"按单查入库"的追溯查询)
|
|
||||||
CREATE INDEX IF NOT EXISTS ix_stock_buy_request_id
|
|
||||||
ON stock_buy (request_id);
|
|
||||||
|
|
||||||
COMMENT ON COLUMN stock_buy.request_id IS '关联采购申请单ID';
|
|
||||||
|
|
||||||
|
|
||||||
-- ============================================================
|
|
||||||
-- 3. 验证结果
|
|
||||||
-- ============================================================
|
|
||||||
SELECT
|
|
||||||
column_name,
|
|
||||||
data_type,
|
|
||||||
is_nullable,
|
|
||||||
pg_catalog.col_description(
|
|
||||||
(SELECT c.oid FROM pg_catalog.pg_class c
|
|
||||||
WHERE c.relname = table_name),
|
|
||||||
ordinal_position
|
|
||||||
) AS comment
|
|
||||||
FROM information_schema.columns
|
|
||||||
WHERE table_name IN ('purchase_request', 'stock_buy')
|
|
||||||
AND column_name IN ('base_id', 'request_id')
|
|
||||||
ORDER BY table_name, column_name;
|
|
||||||
|
|
||||||
COMMIT;
|
|
||||||
Reference in New Issue
Block a user