Skip to content

Commit f75f927

Browse files
committed
🐛 Fix migration for SQLite compatibility in CI/CD
The migration was failing in CI/CD because SQLite doesn't support: - ALTER COLUMN ... DROP NOT NULL syntax - ON CONFLICT (postgres) vs INSERT OR IGNORE (sqlite) Fixed by: - Checking dialect.name and skipping ALTER COLUMN for SQLite - Using INSERT OR IGNORE for SQLite, ON CONFLICT for PostgreSQL - Using 1 instead of true for SQLite boolean values This allows the migration to work in both production (PostgreSQL) and testing (SQLite) environments. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent eefa8ab commit f75f927

1 file changed

Lines changed: 23 additions & 8 deletions

File tree

migrations/versions/c8c8f157177a_enhanced_property_model_with_.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,21 +151,36 @@ def upgrade():
151151
# Phase 5: Migrate existing contact_id relationships to PropertyContact table
152152
# This preserves existing relationships during the transition
153153
connection = op.get_bind()
154-
result = connection.execute(sa.text("""
155-
INSERT INTO property_contact (property_id, contact_id, relationship_type, is_primary, created_at)
156-
SELECT id, contact_id, 'owner', true, CURRENT_TIMESTAMP
157-
FROM property
158-
WHERE contact_id IS NOT NULL
159-
ON CONFLICT (property_id, contact_id) DO NOTHING
160-
"""))
154+
from alembic import context
155+
156+
# SQLite uses different syntax for INSERT ... ON CONFLICT
157+
if context.get_context().dialect.name == 'sqlite':
158+
result = connection.execute(sa.text("""
159+
INSERT OR IGNORE INTO property_contact (property_id, contact_id, relationship_type, is_primary, created_at)
160+
SELECT id, contact_id, 'owner', 1, CURRENT_TIMESTAMP
161+
FROM property
162+
WHERE contact_id IS NOT NULL
163+
"""))
164+
else:
165+
# PostgreSQL syntax
166+
result = connection.execute(sa.text("""
167+
INSERT INTO property_contact (property_id, contact_id, relationship_type, is_primary, created_at)
168+
SELECT id, contact_id, 'owner', true, CURRENT_TIMESTAMP
169+
FROM property
170+
WHERE contact_id IS NOT NULL
171+
ON CONFLICT (property_id, contact_id) DO NOTHING
172+
"""))
161173

162174
# Log migration progress
163175
if result.rowcount > 0:
164176
print(f"Migrated {result.rowcount} existing property-contact relationships")
165177

166178
# Phase 6: Make contact_id nullable for backward compatibility
167179
# This allows properties to be created without requiring a contact_id
168-
op.alter_column('property', 'contact_id', nullable=True)
180+
# Note: SQLite doesn't support ALTER COLUMN, so we need to check the database type
181+
from alembic import context
182+
if context.get_context().dialect.name != 'sqlite':
183+
op.alter_column('property', 'contact_id', nullable=True)
169184

170185
# Note: We're keeping the contact_id column for now to ensure backward compatibility
171186
# It can be removed in a future migration after all code is updated

0 commit comments

Comments
 (0)