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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
"""
Version 2 schema.
Compatible with yoyo-migrations >= 6.0
"""
from datetime import datetime
from yoyo.migrations import get_migration_hash
def upgrade(backend):
create_log_table(backend)
create_version_table(backend)
cursor = backend.execute(
"SELECT id, ctime FROM {}".format(backend.migration_table_quoted)
)
migration_id = "" # type: str
created_at = datetime(1970, 1, 1) # type: datetime
for migration_id, created_at in iter(cursor.fetchone, None): # type: ignore
migration_hash = get_migration_hash(migration_id)
log_data = dict(
backend.get_log_data(),
operation="apply",
comment=(
"this log entry created automatically by an internal schema upgrade"
),
created_at_utc=created_at,
migration_hash=migration_hash,
migration_id=migration_id,
)
backend.execute(
"INSERT INTO {0.log_table_quoted} "
"(id, migration_hash, migration_id, operation, created_at_utc, "
"username, hostname, comment) "
"VALUES "
"(:id, :migration_hash, :migration_id, 'apply', :created_at_utc, "
":username, :hostname, :comment)".format(backend),
log_data,
)
backend.execute("DROP TABLE {0.migration_table_quoted}".format(backend))
create_migration_table(backend)
backend.execute(
"INSERT INTO {0.migration_table_quoted} "
"SELECT migration_hash, migration_id, created_at_utc "
"FROM {0.log_table_quoted}".format(backend)
)
def create_migration_table(backend):
backend.execute(
"CREATE TABLE {0.migration_table_quoted} ( "
# sha256 hash of the migration id
"migration_hash VARCHAR(64), "
# The migration id (ie path basename without extension)
"migration_id VARCHAR(255), "
# When this id was applied
"applied_at_utc TIMESTAMP, "
"PRIMARY KEY (migration_hash))".format(backend)
)
def create_log_table(backend):
backend.execute(
"CREATE TABLE {0.log_table_quoted} ( "
"id VARCHAR(36), "
"migration_hash VARCHAR(64), "
"migration_id VARCHAR(255), "
"operation VARCHAR(10), "
"username VARCHAR(255), "
"hostname VARCHAR(255), "
"comment VARCHAR(255), "
"created_at_utc TIMESTAMP, "
"PRIMARY KEY (id))".format(backend)
)
def create_version_table(backend):
backend.execute(
"CREATE TABLE {0.version_table_quoted} ("
"version INT NOT NULL PRIMARY KEY, "
"installed_at_utc TIMESTAMP)".format(backend)
)
|