-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdb-migration.py
42 lines (34 loc) · 1.13 KB
/
db-migration.py
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
"""Simple db migrations script to apply all pending yoyo migrations
In case you need, you can also use the command line. Eg to list migrations:
```bash
yoyo -c yoyo.ini --database postgres://you_database_url list
```
This can be usefull to mark a migration as already applied without actually applying it.
eg:
```bash
yoyo -c yoyo.ini --database postgres://you_database_url mark
```
"""
import os
import time
from yoyo import read_migrations, get_backend
from folksonomy.settings import POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_HOST
AUTH_DATA = ""
if POSTGRES_USER:
AUTH_DATA += POSTGRES_USER
if POSTGRES_PASSWORD:
AUTH_DATA += ":" + POSTGRES_PASSWORD
if AUTH_DATA:
AUTH_DATA += "@"
url = "postgres://{}{}/folksonomy".format(AUTH_DATA, POSTGRES_HOST or "")
backend = get_backend(url)
# Add steps
migrations = read_migrations('./db/migrations')
# Apply any outstanding migrations
to_apply = backend.to_apply(migrations)
print(f"Found {len(to_apply)} migrations to apply")
if to_apply:
start = time.monotonic()
backend.apply_migrations(to_apply)
end = time.monotonic()
print(f"Done in {end - start:.2} seconds")