Spaces:
Sleeping
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 +5 -2
- backend/alembic/versions/2025_12_10_0255-ai_cache_manual_v1_add_cache.py +13 -21
- backend/alembic/versions/2025_12_12_0627-5204c9a5178c_add_verification_fields_to_user.py +19 -12
- backend/alembic/versions/2025_12_14_1000-add_announcements_table.py +13 -11
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.
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 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 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 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 |
|