30 lines
940 B
Python
30 lines
940 B
Python
from .db import get_db
|
|
from pathlib import Path
|
|
|
|
import colorama
|
|
|
|
|
|
def apply_migrations():
|
|
migrations_dir = Path("./migrations")
|
|
if not migrations_dir.exists():
|
|
raise Exception("migrations directory was not found")
|
|
|
|
db = get_db()
|
|
already_run_migrations = set([])
|
|
try:
|
|
cur = db.execute("SELECT applied FROM migrations")
|
|
for (applied,) in cur.fetchall():
|
|
already_run_migrations.add(applied)
|
|
except:
|
|
pass
|
|
|
|
for migration in sorted(migrations_dir.iterdir()):
|
|
if migration.name not in already_run_migrations:
|
|
print(
|
|
f"{colorama.Fore.LIGHTBLUE_EX}[*]{colorama.Fore.RESET} "
|
|
f"Applied migration: {colorama.Fore.GREEN}{migration.name}{colorama.Fore.RESET}"
|
|
)
|
|
db.executescript(migration.read_text())
|
|
db.execute("INSERT INTO migrations VALUES (?)", [migration.name])
|
|
db.commit()
|