Spaces:
Sleeping

axeronis commited on
Commit
4a58976
·
1 Parent(s): 35ac97a

fix: Make migrations idempotent with IF NOT EXISTS

Browse files
backend/alembic/versions/2025_11_24_0333-395c1f43a20e_add_manager_monthly_salary_to_tenants.py CHANGED
@@ -20,8 +20,11 @@ depends_on: Union[str, Sequence[str], None] = None
20
 
21
 
22
  def upgrade() -> None:
23
- # Add manager_monthly_salary column to tenants table
24
- op.add_column('tenants', sa.Column('manager_monthly_salary', sa.Integer(), nullable=False, server_default='1000'))
 
 
 
25
 
26
 
27
  def downgrade() -> None:
 
20
 
21
 
22
  def upgrade() -> None:
23
+ # Add manager_monthly_salary column to tenants table (idempotent)
24
+ op.execute("""
25
+ ALTER TABLE tenants
26
+ ADD COLUMN IF NOT EXISTS manager_monthly_salary INTEGER NOT NULL DEFAULT 1000;
27
+ """)
28
 
29
 
30
  def downgrade() -> None:
backend/alembic/versions/2025_12_10_0255-ai_cache_manual_v1_add_cache.py CHANGED
@@ -18,27 +18,19 @@ depends_on: Union[str, Sequence[str], None] = None
18
 
19
 
20
  def upgrade() -> None:
21
- # 1. Add AI Cache columns to Tenants
22
- op.add_column('tenants', sa.Column('ai_cache_key', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
23
- op.add_column('tenants', sa.Column('ai_cache_expiry', sa.DateTime(), nullable=True))
24
-
25
- # 2. Cleanup legacy tables (as discussed)
26
- # Use if_exists logic or try/except block usually in raw SQL, but here we assume Alembic knows.
27
- # We will try to drop, but ignore if fails (safest approach is explicit drop).
28
-
29
- # Drop Quick Reply Templates
30
- # Check if table exists is hard in alembic op directly without inspection,
31
- # but we can just run drop_table. If it fails, users can manually fix or we retry.
32
- # Given the autogen saw it, it exists.
33
- try:
34
- op.drop_table('quick_reply_templates')
35
- except Exception:
36
- pass
37
-
38
- try:
39
- op.drop_table('lead_notes')
40
- except Exception:
41
- pass
42
 
43
 
44
  def downgrade() -> None:
 
18
 
19
 
20
  def upgrade() -> None:
21
+ # 1. Add AI Cache columns to Tenants (idempotent)
22
+ op.execute("""
23
+ ALTER TABLE tenants
24
+ ADD COLUMN IF NOT EXISTS ai_cache_key VARCHAR;
25
+ """)
26
+ op.execute("""
27
+ ALTER TABLE tenants
28
+ ADD COLUMN IF NOT EXISTS ai_cache_expiry TIMESTAMP;
29
+ """)
30
+
31
+ # 2. Cleanup legacy tables (idempotent)
32
+ op.execute("DROP TABLE IF EXISTS quick_reply_templates")
33
+ op.execute("DROP TABLE IF EXISTS lead_notes")
 
 
 
 
 
 
 
 
34
 
35
 
36
  def downgrade() -> None:
backend/alembic/versions/2025_12_12_0627-5204c9a5178c_add_verification_fields_to_user.py CHANGED
@@ -23,18 +23,25 @@ depends_on: Union[str, Sequence[str], None] = None
23
  def upgrade() -> None:
24
  # ### commands auto generated by Alembic - please adjust! ###
25
 
26
- # 1. Add columns to users table
27
- # We use server_default to handle existing rows for non-nullable boolean
28
- op.add_column('users', sa.Column('is_verified', sa.Boolean(), nullable=False, server_default=sa.text('false')))
29
- op.add_column('users', sa.Column('verification_code', sqlmodel.sql.sqltypes.AutoString(), nullable=True))
30
- op.add_column('users', sa.Column('verification_code_expires_at', sa.DateTime(), nullable=True))
31
-
32
- # 2. Ensure role is not null (as per original migration attempt)
33
- # This might already be the case, but good to ensure
34
- op.alter_column('users', 'role',
35
- existing_type=sa.VARCHAR(length=20),
36
- nullable=False,
37
- existing_server_default=sa.text("'OWNER'::character varying"))
 
 
 
 
 
 
 
38
 
39
  # ### end Alembic commands ###
40
 
 
23
  def upgrade() -> None:
24
  # ### commands auto generated by Alembic - please adjust! ###
25
 
26
+ # 1. Add columns to users table (idempotent)
27
+ op.execute("""
28
+ ALTER TABLE users
29
+ ADD COLUMN IF NOT EXISTS is_verified BOOLEAN NOT NULL DEFAULT false;
30
+ """)
31
+ op.execute("""
32
+ ALTER TABLE users
33
+ ADD COLUMN IF NOT EXISTS verification_code VARCHAR;
34
+ """)
35
+ op.execute("""
36
+ ALTER TABLE users
37
+ ADD COLUMN IF NOT EXISTS verification_code_expires_at TIMESTAMP;
38
+ """)
39
+
40
+ # 2. Ensure role is not null (idempotent - safe to run multiple times)
41
+ op.execute("""
42
+ ALTER TABLE users
43
+ ALTER COLUMN role SET NOT NULL;
44
+ """)
45
 
46
  # ### end Alembic commands ###
47
 
backend/alembic/versions/2025_12_14_1000-add_announcements_table.py CHANGED
@@ -22,17 +22,19 @@ depends_on: Union[str, Sequence[str], None] = None
22
 
23
  def upgrade() -> None:
24
  # ### commands auto generated by Alembic - adjusted manually ###
25
- op.create_table('system_announcements',
26
- sa.Column('id', sa.UUID(), nullable=False),
27
- sa.Column('title', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
28
- sa.Column('message', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
29
- sa.Column('type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
30
- sa.Column('is_active', sa.Boolean(), nullable=False),
31
- sa.Column('created_at', sa.DateTime(), nullable=False),
32
- sa.Column('expires_at', sa.DateTime(), nullable=True),
33
- sa.Column('created_by', sa.UUID(), nullable=True),
34
- sa.PrimaryKeyConstraint('id')
35
- )
 
 
36
  # ### end Alembic commands ###
37
 
38
 
 
22
 
23
  def upgrade() -> None:
24
  # ### commands auto generated by Alembic - adjusted manually ###
25
+ # Idempotent: create table only if not exists
26
+ op.execute("""
27
+ CREATE TABLE IF NOT EXISTS system_announcements (
28
+ id UUID NOT NULL PRIMARY KEY,
29
+ title VARCHAR NOT NULL,
30
+ message VARCHAR NOT NULL,
31
+ type VARCHAR NOT NULL,
32
+ is_active BOOLEAN NOT NULL,
33
+ created_at TIMESTAMP NOT NULL,
34
+ expires_at TIMESTAMP,
35
+ created_by UUID
36
+ );
37
+ """)
38
  # ### end Alembic commands ###
39
 
40