-
Notifications
You must be signed in to change notification settings - Fork 23
/
migrations.py
136 lines (126 loc) · 4.05 KB
/
migrations.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
#############################################################################
#
# Copyright (C) 2018 Fabian Schweinfurth
# Contact: autoshift <at> derfabbi.de
#
# This file is part of autoshift
#
# autoshift is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# autoshift is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with autoshift. If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
import sqlite3
from typing import Callable, List, Union
from common import _L
MigrationFunc = Callable[[sqlite3.Connection, bool], bool]
migrationFunctions: dict[int, MigrationFunc] = {}
def register(version: int):
from functools import wraps
def wrap(func: MigrationFunc):
@wraps(func)
def wrapper(conn: sqlite3.Connection, silent: bool):
try:
if func(conn, silent):
conn.cursor().execute(f"PRAGMA user_version = {version}")
conn.commit()
return True
return False
except sqlite3.OperationalError:
_L.error(f"There was an error while migrating database to version {version}."
"Please contact the developer @ github.com/fabbi")
return False
migrationFunctions[version] = wrapper
return wrapper
return wrap
@register(1)
def update_1(conn: sqlite3.Connection, silent=False):
c = conn.cursor()
if not silent:
_L.warn("Some games and platforms have been renamed! "
"Check `--help` or the README.md for further info.")
steps: List[Union[str, tuple]] = [
"""
ALTER TABLE keys
RENAME COLUMN description TO reward;
"""
,
"""
ALTER TABLE keys
RENAME COLUMN key TO code;
"""
, # renamed "ps" to "psn"
"""
UPDATE keys
SET platform="psn"
WHERE platform="ps";
"""
, # renamed "xbox" to "xboxlive"
"""
UPDATE keys
SET platform="xboxlive"
WHERE platform="xbox";
"""
, # renamed "bl" to "bl1"
"""
UPDATE keys
SET game="bl1"
WHERE game="bl";
"""
, # "pc" is now "epic" and "steam"
"""
CREATE VIEW tmp AS
SELECT * FROM keys
WHERE platform="pc";
-- AND redeemed=0;
INSERT INTO keys(reward, code, platform, game, redeemed)
SELECT reward, code, "epic", game, redeemed
FROM tmp;
UPDATE keys
SET platform="steam"
WHERE id in (select id from tmp);
DROP VIEW tmp;
"""
,
"""
CREATE TABLE IF NOT EXISTS seen_platforms
(key TEXT primary key, name TEXT)
"""
,
"""
CREATE TABLE IF NOT EXISTS seen_games
(key TEXT primary key, name TEXT)
"""
]
from query import known_games, known_platforms
for game in known_games.items():
steps.append(("""
INSERT INTO seen_games(key, name)
VALUES(?, ?)
""", game))
for platform in known_platforms.items():
steps.append(("""
INSERT INTO seen_platforms(key, name)
VALUES(?, ?)
""", platform))
for i, step in enumerate(steps):
_L.debug(f"executing step {i} of update_1")
try:
if isinstance(step, tuple):
c.execute(*step)
else:
c.executescript(step)
except sqlite3.OperationalError as e:
_L.error(f"'{e}' in\n{step}")
return False
return True