acupedia-api/alembic/versions/0001_baseline_schema.py

175 lines
4.4 KiB
Python

from __future__ import annotations
from collections.abc import Sequence
from pathlib import Path
import re
from alembic import op
revision: str = "0001_baseline_schema"
down_revision: str | None = None
branch_labels: Sequence[str] | None = None
depends_on: Sequence[str] | None = None
def _split_sql(text: str) -> list[str]:
statements = []
buffer = []
i = 0
dollar_tag = None
in_single = False
in_double = False
in_line_comment = False
in_block_comment = False
block_depth = 0
while i < len(text):
ch = text[i]
nxt = text[i + 1] if i + 1 < len(text) else ""
if in_line_comment:
buffer.append(ch)
if ch == "\n":
in_line_comment = False
i += 1
continue
if in_block_comment:
buffer.append(ch)
if ch == "/" and nxt == "*":
block_depth += 1
buffer.append(nxt)
i += 2
continue
if ch == "*" and nxt == "/":
if block_depth == 0:
buffer.append(nxt)
i += 2
in_block_comment = False
continue
block_depth -= 1
buffer.append(nxt)
i += 2
continue
i += 1
continue
if dollar_tag is not None:
if text.startswith(dollar_tag, i):
buffer.append(dollar_tag)
i += len(dollar_tag)
dollar_tag = None
else:
buffer.append(ch)
i += 1
continue
if in_single:
buffer.append(ch)
if ch == "'":
if nxt == "'":
buffer.append(nxt)
i += 2
continue
in_single = False
i += 1
continue
if in_double:
buffer.append(ch)
if ch == '"':
if nxt == '"':
buffer.append(nxt)
i += 2
continue
in_double = False
i += 1
continue
if ch == "-" and nxt == "-":
buffer.extend([ch, nxt])
i += 2
in_line_comment = True
continue
if ch == "/" and nxt == "*":
buffer.extend([ch, nxt])
i += 2
in_block_comment = True
block_depth = 0
continue
if ch == "'":
buffer.append(ch)
i += 1
in_single = True
continue
if ch == '"':
buffer.append(ch)
i += 1
in_double = True
continue
if ch == "$":
match = re.match(r"\$[A-Za-z_][A-Za-z0-9_]*\$|\$\$", text[i:])
if match:
tag = match.group(0)
buffer.append(tag)
i += len(tag)
dollar_tag = tag
continue
if ch == ";":
statement = "".join(buffer).strip()
if statement:
statements.append(statement)
buffer = []
i += 1
continue
buffer.append(ch)
i += 1
statement = "".join(buffer).strip()
if statement:
statements.append(statement)
return statements
def _is_transaction_statement(statement: str) -> bool:
lines = [
line.strip()
for line in statement.splitlines()
if line.strip() and not line.strip().startswith("--")
]
if not lines:
return True
value = "\n".join(lines).strip().lower()
return value in {
"begin",
"begin transaction",
"commit",
"commit transaction",
}
def upgrade() -> None:
path = Path(__file__).parent / "sql" / "acupedia_schema_v5.sql"
sql = path.read_text(encoding="utf-8")
connection = op.get_bind()
for statement in _split_sql(sql):
if not _is_transaction_statement(statement):
connection.exec_driver_sql(statement)
def downgrade() -> None:
connection = op.get_bind()
connection.exec_driver_sql("DROP SCHEMA IF EXISTS platform CASCADE")
connection.exec_driver_sql("DROP SCHEMA IF EXISTS commerce CASCADE")
connection.exec_driver_sql("DROP SCHEMA IF EXISTS content CASCADE")
connection.exec_driver_sql("DROP SCHEMA IF EXISTS auth CASCADE")