|
27 | 27 |
|
28 | 28 | def upgrade() -> None:
|
29 | 29 | """Add comprehensive metadata columns to all entity tables for audit tracking."""
|
| 30 | + bind = op.get_bind() |
| 31 | + inspector = sa.inspect(bind) |
| 32 | + |
| 33 | + # Check if this is a fresh database without existing tables |
| 34 | + if not inspector.has_table("gateways"): |
| 35 | + print("Fresh database detected. Skipping metadata migration.") |
| 36 | + return |
| 37 | + |
30 | 38 | tables = ["tools", "resources", "prompts", "servers", "gateways"]
|
31 | 39 |
|
| 40 | + # Define metadata columns to add |
| 41 | + metadata_columns = [ |
| 42 | + ("created_by", sa.String(), True), |
| 43 | + ("created_from_ip", sa.String(), True), |
| 44 | + ("created_via", sa.String(), True), |
| 45 | + ("created_user_agent", sa.Text(), True), |
| 46 | + ("modified_by", sa.String(), True), |
| 47 | + ("modified_from_ip", sa.String(), True), |
| 48 | + ("modified_via", sa.String(), True), |
| 49 | + ("modified_user_agent", sa.Text(), True), |
| 50 | + ("import_batch_id", sa.String(), True), |
| 51 | + ("federation_source", sa.String(), True), |
| 52 | + ("version", sa.Integer(), False, "1"), # Not nullable, with default |
| 53 | + ] |
| 54 | + |
| 55 | + # Add columns to each table if they don't exist |
| 56 | + for table in tables: |
| 57 | + if inspector.has_table(table): |
| 58 | + columns = [col["name"] for col in inspector.get_columns(table)] |
| 59 | + |
| 60 | + for col_name, col_type, nullable, *default in metadata_columns: |
| 61 | + if col_name not in columns: |
| 62 | + try: |
| 63 | + if default: |
| 64 | + op.add_column(table, sa.Column(col_name, col_type, nullable=nullable, server_default=default[0])) |
| 65 | + else: |
| 66 | + op.add_column(table, sa.Column(col_name, col_type, nullable=nullable)) |
| 67 | + print(f"Added column {col_name} to {table}") |
| 68 | + except Exception as e: |
| 69 | + print(f"Warning: Could not add column {col_name} to {table}: {e}") |
| 70 | + |
| 71 | + # Create indexes for query performance (safe B-tree indexes) |
| 72 | + # Note: modified_at column doesn't exist in schema, so we skip it |
| 73 | + index_definitions = [ |
| 74 | + ("created_by", ["created_by"]), |
| 75 | + ("created_at", ["created_at"]), |
| 76 | + ("created_via", ["created_via"]), |
| 77 | + ] |
| 78 | + |
32 | 79 | for table in tables:
|
33 |
| - # Creation metadata (nullable=True for backwards compatibility) |
34 |
| - op.add_column(table, sa.Column("created_by", sa.String(), nullable=True)) |
35 |
| - op.add_column(table, sa.Column("created_from_ip", sa.String(), nullable=True)) |
36 |
| - op.add_column(table, sa.Column("created_via", sa.String(), nullable=True)) |
37 |
| - op.add_column(table, sa.Column("created_user_agent", sa.Text(), nullable=True)) |
38 |
| - |
39 |
| - # Modification metadata (nullable=True for backwards compatibility) |
40 |
| - op.add_column(table, sa.Column("modified_by", sa.String(), nullable=True)) |
41 |
| - op.add_column(table, sa.Column("modified_from_ip", sa.String(), nullable=True)) |
42 |
| - op.add_column(table, sa.Column("modified_via", sa.String(), nullable=True)) |
43 |
| - op.add_column(table, sa.Column("modified_user_agent", sa.Text(), nullable=True)) |
44 |
| - |
45 |
| - # Source tracking (nullable=True for backwards compatibility) |
46 |
| - op.add_column(table, sa.Column("import_batch_id", sa.String(), nullable=True)) |
47 |
| - op.add_column(table, sa.Column("federation_source", sa.String(), nullable=True)) |
48 |
| - op.add_column(table, sa.Column("version", sa.Integer(), nullable=False, server_default="1")) |
49 |
| - |
50 |
| - # Create indexes for query performance (PostgreSQL compatible, SQLite ignores) |
51 |
| - try: |
52 |
| - op.create_index(f"idx_{table}_created_by", table, ["created_by"]) |
53 |
| - op.create_index(f"idx_{table}_created_at", table, ["created_at"]) |
54 |
| - op.create_index(f"idx_{table}_modified_at", table, ["modified_at"]) |
55 |
| - op.create_index(f"idx_{table}_created_via", table, ["created_via"]) |
56 |
| - except Exception: # nosec B110 - database compatibility |
57 |
| - # SQLite doesn't support all index types, skip silently |
58 |
| - pass |
| 80 | + if inspector.has_table(table): |
| 81 | + try: |
| 82 | + existing_indexes = [idx["name"] for idx in inspector.get_indexes(table)] |
| 83 | + except Exception as e: |
| 84 | + print(f"Warning: Could not get indexes for {table}: {e}") |
| 85 | + continue |
| 86 | + |
| 87 | + for index_suffix, columns in index_definitions: |
| 88 | + index_name = f"idx_{table}_{index_suffix}" |
| 89 | + if index_name not in existing_indexes: |
| 90 | + # Check if the column exists before creating index |
| 91 | + table_columns = [col["name"] for col in inspector.get_columns(table)] |
| 92 | + if all(col in table_columns for col in columns): |
| 93 | + try: |
| 94 | + op.create_index(index_name, table, columns) |
| 95 | + print(f"Created index {index_name}") |
| 96 | + except Exception as e: |
| 97 | + print(f"Warning: Could not create index {index_name}: {e}") |
| 98 | + else: |
| 99 | + print(f"Skipping index {index_name} - required columns {columns} not found in {table}") |
59 | 100 |
|
60 | 101 |
|
61 | 102 | def downgrade() -> None:
|
62 | 103 | """Remove comprehensive metadata columns from all entity tables."""
|
| 104 | + bind = op.get_bind() |
| 105 | + inspector = sa.inspect(bind) |
| 106 | + |
63 | 107 | tables = ["tools", "resources", "prompts", "servers", "gateways"]
|
64 | 108 |
|
| 109 | + # Index names to drop (modified_at doesn't exist, so skip it) |
| 110 | + index_suffixes = ["created_by", "created_at", "created_via"] |
| 111 | + |
| 112 | + # Drop indexes first (if they exist) |
65 | 113 | for table in tables:
|
66 |
| - # Drop indexes first (if they exist) |
67 |
| - try: |
68 |
| - op.drop_index(f"idx_{table}_created_by", table) |
69 |
| - op.drop_index(f"idx_{table}_created_at", table) |
70 |
| - op.drop_index(f"idx_{table}_modified_at", table) |
71 |
| - op.drop_index(f"idx_{table}_created_via", table) |
72 |
| - except Exception: # nosec B110 - database compatibility |
73 |
| - # Indexes might not exist on SQLite |
74 |
| - pass |
75 |
| - |
76 |
| - # Drop metadata columns |
77 |
| - op.drop_column(table, "version") |
78 |
| - op.drop_column(table, "federation_source") |
79 |
| - op.drop_column(table, "import_batch_id") |
80 |
| - op.drop_column(table, "modified_user_agent") |
81 |
| - op.drop_column(table, "modified_via") |
82 |
| - op.drop_column(table, "modified_from_ip") |
83 |
| - op.drop_column(table, "modified_by") |
84 |
| - op.drop_column(table, "created_user_agent") |
85 |
| - op.drop_column(table, "created_via") |
86 |
| - op.drop_column(table, "created_from_ip") |
87 |
| - op.drop_column(table, "created_by") |
| 114 | + if inspector.has_table(table): |
| 115 | + try: |
| 116 | + existing_indexes = [idx["name"] for idx in inspector.get_indexes(table)] |
| 117 | + except Exception as e: |
| 118 | + print(f"Warning: Could not get indexes for {table}: {e}") |
| 119 | + continue |
| 120 | + |
| 121 | + for suffix in index_suffixes: |
| 122 | + index_name = f"idx_{table}_{suffix}" |
| 123 | + if index_name in existing_indexes: |
| 124 | + try: |
| 125 | + op.drop_index(index_name, table) |
| 126 | + print(f"Dropped index {index_name}") |
| 127 | + except Exception as e: |
| 128 | + print(f"Warning: Could not drop index {index_name}: {e}") |
| 129 | + |
| 130 | + # Metadata columns to drop (in reverse order for safety) |
| 131 | + metadata_columns = [ |
| 132 | + "version", |
| 133 | + "federation_source", |
| 134 | + "import_batch_id", |
| 135 | + "modified_user_agent", |
| 136 | + "modified_via", |
| 137 | + "modified_from_ip", |
| 138 | + "modified_by", |
| 139 | + "created_user_agent", |
| 140 | + "created_via", |
| 141 | + "created_from_ip", |
| 142 | + "created_by", |
| 143 | + ] |
| 144 | + |
| 145 | + # Drop metadata columns (if they exist) |
| 146 | + for table in reversed(tables): # Reverse order for safety |
| 147 | + if inspector.has_table(table): |
| 148 | + columns = [col["name"] for col in inspector.get_columns(table)] |
| 149 | + |
| 150 | + for col_name in metadata_columns: |
| 151 | + if col_name in columns: |
| 152 | + try: |
| 153 | + op.drop_column(table, col_name) |
| 154 | + print(f"Dropped column {col_name} from {table}") |
| 155 | + except Exception as e: |
| 156 | + print(f"Warning: Could not drop column {col_name} from {table}: {e}") |
0 commit comments