forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrations.py
More file actions
41 lines (32 loc) · 1.02 KB
/
migrations.py
File metadata and controls
41 lines (32 loc) · 1.02 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
import os
from typing import List
import asyncpg
import psycopg2.extensions
def apply_migrations(db: psycopg2.extensions.connection, paths: List[str]):
files = _find_sql_files(paths)
for file in files:
with open(file, "r") as fd:
blob = fd.read()
cur = db.cursor()
cur.execute(blob)
cur.close()
db.commit()
async def apply_migrations_async(db: asyncpg.Connection, paths: List[str]):
files = _find_sql_files(paths)
for file in files:
with open(file, "r") as fd:
blob = fd.read()
await db.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