-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathmigrations.py
More file actions
43 lines (34 loc) · 1.28 KB
/
migrations.py
File metadata and controls
43 lines (34 loc) · 1.28 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
import os
from typing import List
import sqlalchemy
import sqlalchemy.ext.asyncio
def apply_migrations(conn: sqlalchemy.engine.Connection, paths: List[str]):
files = _find_sql_files(paths)
for file in files:
with open(file, "r") as fd:
blob = fd.read()
stmts = blob.split(";")
for stmt in stmts:
if stmt.strip():
conn.execute(sqlalchemy.text(stmt))
async def apply_migrations_async(conn: sqlalchemy.ext.asyncio.AsyncConnection, paths: List[str]):
files = _find_sql_files(paths)
for file in files:
with open(file, "r") as fd:
blob = fd.read()
raw_conn = await conn.get_raw_connection()
# The asyncpg sqlalchemy adapter uses a prepared statement cache which can't handle the migration statements
await raw_conn._connection.execute(blob)
def _find_sql_files(paths: List[str]) -> List[str]:
files = []
for path in paths:
if not os.path.exists(path):
raise FileNotFoundError(f"{path} does not exist")
if os.path.isdir(path):
for file in os.listdir(path):
if file.endswith(".sql"):
files.append(os.path.join(path, file))
else:
files.append(path)
files.sort()
return files