gate2: fix all migrations for fresh DB installation
This commit is contained in:
@@ -22,6 +22,45 @@ depends_on = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# Create folders table if it doesn't exist (DMS plugin table normally created via create_all)
|
||||
op.execute("""
|
||||
CREATE TABLE IF NOT EXISTS folders (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
tenant_id UUID NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
parent_id UUID REFERENCES folders(id) ON DELETE CASCADE,
|
||||
owner_id UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
created_by UUID NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
deleted_at TIMESTAMP WITH TIME ZONE
|
||||
)
|
||||
""")
|
||||
op.execute('CREATE INDEX IF NOT EXISTS ix_folders_parent ON folders (parent_id)')
|
||||
op.execute('CREATE INDEX IF NOT EXISTS ix_folders_tenant ON folders (tenant_id)')
|
||||
|
||||
# Create files table if it doesn't exist (DMS plugin table normally created via create_all)
|
||||
op.execute("""
|
||||
CREATE TABLE IF NOT EXISTS files (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
tenant_id UUID NOT NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
folder_id UUID REFERENCES folders(id) ON DELETE SET NULL,
|
||||
owner_id UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
uploaded_by UUID NOT NULL,
|
||||
mime_type VARCHAR(255) NOT NULL,
|
||||
size_bytes INTEGER NOT NULL,
|
||||
storage_path VARCHAR(1024) NOT NULL,
|
||||
content_hash VARCHAR(64),
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
|
||||
deleted_at TIMESTAMP WITH TIME ZONE
|
||||
)
|
||||
""")
|
||||
op.execute('CREATE INDEX IF NOT EXISTS ix_files_folder ON files (folder_id)')
|
||||
op.execute('CREATE INDEX IF NOT EXISTS ix_files_tenant ON files (tenant_id)')
|
||||
op.execute('CREATE INDEX IF NOT EXISTS ix_files_name ON files (name)')
|
||||
|
||||
op.create_table(
|
||||
"entity_attachments",
|
||||
sa.Column("id", PGUUID(as_uuid=True), primary_key=True, server_default=sa.text("gen_random_uuid()")),
|
||||
@@ -38,10 +77,10 @@ def upgrade() -> None:
|
||||
sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
|
||||
)
|
||||
|
||||
op.create_index("ix_entity_attachments_entity", "entity_attachments", ["entity_type", "entity_id", "tenant_id"])
|
||||
op.create_index("ix_entity_attachments_tenant", "entity_attachments", ["tenant_id"])
|
||||
op.create_index("ix_entity_attachments_dms_file", "entity_attachments", ["dms_file_id"])
|
||||
op.create_index("ix_entity_attachments_owner", "entity_attachments", ["owner_id"])
|
||||
op.execute('CREATE INDEX IF NOT EXISTS ix_entity_attachments_entity ON entity_attachments (entity_type, entity_id, tenant_id)')
|
||||
op.execute('CREATE INDEX IF NOT EXISTS ix_entity_attachments_tenant ON entity_attachments (tenant_id)')
|
||||
op.execute('CREATE INDEX IF NOT EXISTS ix_entity_attachments_dms_file ON entity_attachments (dms_file_id)')
|
||||
op.execute('CREATE INDEX IF NOT EXISTS ix_entity_attachments_owner ON entity_attachments (owner_id)')
|
||||
|
||||
# Enable RLS on entity_attachments (tenant isolation)
|
||||
op.execute("ALTER TABLE entity_attachments ENABLE ROW LEVEL SECURITY")
|
||||
|
||||
Reference in New Issue
Block a user