This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrator.py
42 lines (35 loc) · 1.45 KB
/
migrator.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
import os, sys, sqlite3
from settings import DB_FILE, PINGPANTHER_ROOT
MIGRATION_DIR = os.path.join(PINGPANTHER_ROOT, 'migrations')
sys.path.append(MIGRATION_DIR)
def get_all_migrations():
"""Returns a list of all the migrations inside the MIGRATION_DIR"""
migrations = os.listdir(MIGRATION_DIR)
m_list = []
for m in migrations:
number = m.split(".")[0]
file_type = m.split(".")[1]
if file_type == "py":
migration_module = __import__(number)
migration_class = migration_module.Migration
m_list.append((number, migration_class))
return sorted(m_list, key=lambda m: m[0])
def run_migrations():
"""Execute each migration's forwards migration"""
# connect to the database
db = sqlite3.connect(DB_FILE, detect_types = sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
m_list = get_all_migrations()
for (number, m_c) in m_list:
# check if migration has already been migrated
entry = db.execute("SELECT id FROM migration WHERE id = ? AND migrated = ?",
(number, 1)).fetchone()
if not entry: # migrate forward
# print "Migrating %s" % number
migration = m_c(db)
migration.forwards()
db.execute(
"INSERT INTO migration (id, migrated) VALUES (?, ?)",
(number, 1))
# else:
# print "Skipping %s, already migrated." % number
db.commit()