-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathj9k0l1m2n3o4_audit_log_details_json.py
More file actions
55 lines (43 loc) · 1.4 KB
/
Copy pathj9k0l1m2n3o4_audit_log_details_json.py
File metadata and controls
55 lines (43 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""store audit_log.details as JSONB on PostgreSQL
Revision ID: j9k0l1m2n3o4
Revises: i8j9k0l1m2n3
Create Date: 2026-05-08 00:00:00.000000
"""
from typing import Sequence, Union
import sqlalchemy as sa
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "j9k0l1m2n3o4"
down_revision: Union[str, None] = "i8j9k0l1m2n3"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
bind = op.get_bind()
if bind.dialect.name == "postgresql":
op.execute(
"""
ALTER TABLE audit_log
ALTER COLUMN details TYPE JSONB
USING CASE
WHEN details IS NULL OR btrim(details) = '' THEN NULL
ELSE details::jsonb
END
"""
)
return
op.alter_column("audit_log", "details", existing_type=sa.Text(), type_=sa.JSON(), existing_nullable=True)
def downgrade() -> None:
bind = op.get_bind()
if bind.dialect.name == "postgresql":
op.execute(
"""
ALTER TABLE audit_log
ALTER COLUMN details TYPE TEXT
USING CASE
WHEN details IS NULL THEN NULL
ELSE details::text
END
"""
)
return
op.alter_column("audit_log", "details", existing_type=sa.JSON(), type_=sa.Text(), existing_nullable=True)