forked from JosXa/BotListBot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinitialize_database.py
More file actions
109 lines (89 loc) · 2.38 KB
/
Copy pathinitialize_database.py
File metadata and controls
109 lines (89 loc) · 2.38 KB
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
import sys
from pathlib import Path
botlistbot_path = str((Path(__file__).parent.parent / "botlistbot").absolute())
sys.path.append(botlistbot_path)
from pprint import pprint
from peewee import Proxy
from playhouse.db_url import connect
from playhouse.migrate import PostgresqlMigrator
import appglobals
from models import *
from models import Bot, User, Suggestion
from models.keywordmodel import Keyword
connection = connect(appglobals.DATABASE_PATH)
# connection = connect("sqlite:///:memory:")
connection.autorollback = True
database = Proxy()
database.initialize(connection)
postgresql_migrator = PostgresqlMigrator(database)
create_order = [
Country,
User,
APIAccess,
Category,
Revision,
Bot,
Channel,
Favorite,
Group,
Keyword,
Notifications,
Statistic,
Suggestion,
]
delete_order = [
APIAccess,
Revision,
Channel,
Favorite,
Group,
Keyword,
Notifications,
Statistic,
Suggestion,
Bot,
Country,
Category,
User,
]
for model in create_order:
# noinspection PyProtectedMember
model._meta.database = database
def delete_models():
sure = input("Deleting all existing models... Are you sure? (y/n) ")
if sure == "y":
for m in delete_order:
m.drop_table(safe=True)
print("All models drop.")
else:
print("Nothing deleted.")
def try_create_models():
for m in create_order:
m.create_table(safe=True)
print("Created models if they did not exist yet.")
def seed_database():
print("Seeding database...")
categories = [
{**c, **{"order": n + 1, "current_message_id": None}}
for n, c in enumerate(
[
{"name": "Miscellaneous", "emojis": "🌀", "extra": "Miscelaneo"},
{"name": "Social", "emojis": "👥📢", "extra": None},
{"name": "Promoting", "emojis": "🙋👋🏼", "extra": "Divulgación"},
]
)
]
with database.atomic():
Category.insert_many(categories).execute()
print("Inserted categories:")
pprint(categories)
print()
REVISION_NR = 100
Revision.insert({"nr": REVISION_NR}).execute()
print(f"Singleton revision {REVISION_NR} entry created.")
if __name__ == "__main__":
if "recreate" in sys.argv:
delete_models()
try_create_models()
if "seed" in sys.argv:
seed_database()