Skip to content

Commit d8bab17

Browse files
authored
Fix tag migrations (#5028)
1 parent a3456ca commit d8bab17

File tree

2 files changed

+93
-6
lines changed

2 files changed

+93
-6
lines changed

bims/migrations/0493_copy_tags_to_tenants.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,29 @@ def copy_tags_to_tenants(apps, schema_editor):
6363
table_exists = cursor.fetchone()[0]
6464

6565
if not table_exists:
66-
print(f" Skipping {schema_name}: taggit_tag table doesn't exist yet. Run migrate_schemas first.")
67-
continue
66+
print(f" Creating taggit tables in {schema_name} by copying from public schema...")
67+
68+
# Copy taggit_tag table structure (including constraints and indexes)
69+
cursor.execute(f"""
70+
CREATE TABLE {schema_name}.taggit_tag
71+
(LIKE public.taggit_tag INCLUDING ALL)
72+
""")
73+
74+
# Copy taggit_taggeditem table structure
75+
cursor.execute(f"""
76+
CREATE TABLE {schema_name}.taggit_taggeditem
77+
(LIKE public.taggit_taggeditem INCLUDING ALL)
78+
""")
79+
80+
# Recreate foreign key constraint to point to tenant schema
81+
cursor.execute(f"""
82+
ALTER TABLE {schema_name}.taggit_taggeditem
83+
ADD CONSTRAINT taggit_taggeditem_tag_id_fkey
84+
FOREIGN KEY (tag_id) REFERENCES {schema_name}.taggit_tag(id)
85+
ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
86+
""")
87+
88+
print(f" ✓ Created taggit tables in {schema_name}")
6889

6990
# Check if tags have already been migrated to this tenant
7091
cursor.execute(f"""
@@ -128,10 +149,10 @@ def copy_tags_to_tenants(apps, schema_editor):
128149
if cursor.fetchone():
129150
items_skipped += 1
130151
else:
131-
# Check if the content_type and object still exist in this tenant
152+
# Check if the content_type exists in public schema
132153
cursor.execute(f"""
133154
SELECT EXISTS (
134-
SELECT 1 FROM {schema_name}.django_content_type
155+
SELECT 1 FROM public.django_content_type
135156
WHERE id = %s
136157
)
137158
""", [content_type_id])

bims/migrations/0495_rename_taxontag_id_to_tag_id.py

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,80 @@ def rename_column_in_all_schemas(apps, schema_editor):
5858
has_tag_id = cursor.fetchone()[0]
5959

6060
if has_taxontag_id and not has_tag_id:
61-
print(f" Renaming column in {schema_name}...")
61+
print(f" Updating {schema_name}...")
62+
63+
# Drop old foreign key constraint to bims_taxontag if it exists
64+
cursor.execute(f"""
65+
SELECT constraint_name
66+
FROM information_schema.table_constraints
67+
WHERE table_schema = '{schema_name}'
68+
AND table_name = 'bims_taggroup_tags'
69+
AND constraint_type = 'FOREIGN KEY'
70+
AND constraint_name LIKE '%taxontag%'
71+
""")
72+
old_constraint = cursor.fetchone()
73+
if old_constraint:
74+
constraint_name = old_constraint[0]
75+
print(f" Dropping old FK constraint: {constraint_name}")
76+
cursor.execute(f"""
77+
ALTER TABLE {schema_name}.bims_taggroup_tags
78+
DROP CONSTRAINT {constraint_name}
79+
""")
80+
81+
# Rename column
6282
cursor.execute(f"""
6383
ALTER TABLE {schema_name}.bims_taggroup_tags
6484
RENAME COLUMN taxontag_id TO tag_id
6585
""")
66-
print(f" ✓ Renamed taxontag_id to tag_id in {schema_name}")
86+
87+
# Add new foreign key constraint to taggit_tag
88+
print(f" Adding new FK constraint to taggit_tag")
89+
cursor.execute(f"""
90+
ALTER TABLE {schema_name}.bims_taggroup_tags
91+
ADD CONSTRAINT bims_taggroup_tags_tag_id_fkey
92+
FOREIGN KEY (tag_id) REFERENCES {schema_name}.taggit_tag(id)
93+
ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
94+
""")
95+
96+
print(f" ✓ Updated {schema_name}")
6797
elif has_tag_id:
6898
print(f" Skipping {schema_name}: tag_id column already exists")
99+
# Check if the correct FK constraint exists
100+
cursor.execute(f"""
101+
SELECT constraint_name
102+
FROM information_schema.table_constraints
103+
WHERE table_schema = '{schema_name}'
104+
AND table_name = 'bims_taggroup_tags'
105+
AND constraint_type = 'FOREIGN KEY'
106+
AND constraint_name LIKE '%tag_id%'
107+
""")
108+
fk_exists = cursor.fetchone()
109+
110+
if not fk_exists:
111+
# Need to add the FK constraint even though column exists
112+
print(f" Adding missing FK constraint to taggit_tag")
113+
# First drop any old constraint
114+
cursor.execute(f"""
115+
SELECT constraint_name
116+
FROM information_schema.table_constraints
117+
WHERE table_schema = '{schema_name}'
118+
AND table_name = 'bims_taggroup_tags'
119+
AND constraint_type = 'FOREIGN KEY'
120+
AND constraint_name LIKE '%taxontag%'
121+
""")
122+
old_constraint = cursor.fetchone()
123+
if old_constraint:
124+
cursor.execute(f"""
125+
ALTER TABLE {schema_name}.bims_taggroup_tags
126+
DROP CONSTRAINT {old_constraint[0]}
127+
""")
128+
129+
cursor.execute(f"""
130+
ALTER TABLE {schema_name}.bims_taggroup_tags
131+
ADD CONSTRAINT bims_taggroup_tags_tag_id_fkey
132+
FOREIGN KEY (tag_id) REFERENCES {schema_name}.taggit_tag(id)
133+
ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED
134+
""")
69135
else:
70136
print(f" Skipping {schema_name}: taxontag_id column doesn't exist")
71137

0 commit comments

Comments
 (0)