Repository navigation
Expand file tree
/
Copy pathdb.py
More file actions
209 lines (184 loc) · 5.63 KB
/
Copy pathdb.py
File metadata and controls
209 lines (184 loc) · 5.63 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import aiosqlite
import os
from datetime import datetime
from pathlib import Path
import sqlite3
from config import DB_LOCATION, BACKUPS_LOCATION
async def get_database(location: str) -> aiosqlite.Connection:
if os.path.isfile(location):
con = await aiosqlite.connect(location)
con.row_factory = aiosqlite.Row
return con
raise FileNotFoundError
def check_backups(backup_folder: str):
print("Checking backup folder.")
backup_folder = Path(backup_folder)
if not os.path.exists(backup_folder):
print("Can't find backup folder.")
return
def check(f: str) -> bool:
if not os.path.isfile(os.path.join(backup_folder, f)):
return False
date = f[:19]
db = f[19:]
if db != "_db.db":
return False
try:
datetime.fromisoformat(date)
except ValueError:
return False
return True
for file in os.listdir(backup_folder):
if not check(file):
print(f"Unexpected file or folder in backups folder: {file}")
continue
print("Done")
def backup(db_location: str, backup_folder: str):
print("Backup start.")
if not os.path.isfile(db_location):
raise FileNotFoundError
backup_folder = Path(backup_folder)
if not os.path.exists(backup_folder):
print("No backup folder found, creating...")
os.makedirs(backup_folder)
now = datetime.now().isoformat(timespec="seconds")
new_backup = os.path.join(backup_folder, now + "_db.db")
if os.path.isfile(new_backup):
print("Last backup was less than one second ago? Aborting.")
return
print(f"Creating new backup file: {new_backup}")
new_con = sqlite3.connect(new_backup)
con = sqlite3.connect(db_location)
con.backup(new_con)
new_con.close()
con.close()
print("Backup done.")
def initialise_database(location: str):
"""Columns CamelCase, Tables snake_case"""
location = Path(DB_LOCATION)
os.makedirs(location.parent, exist_ok=True)
if not os.path.isfile(location):
print("Database doesn't exist yet! This is weird and should be investigated.")
with sqlite3.connect(location) as con:
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
memories (userID INTEGER, postID INTEGER, postUrl TEXT, reminder TEXT, queryMade INT, queryDue INT, status TEXT);
"""
)
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
remove_role (
UserID INT NOT NULL,
GuildID INT NOT NULL,
RoleID INT NOT NULL,
Due INT NOT NULL,
Error TEXT
);"""
)
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
emojis_default (
GuildID INT NOT NULL,
Name TEXT NOT NULL,
Uses INT NOT NULL,
UNIQUE (GuildID, Name)
);
"""
)
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
emojis_custom (
GuildID INT NOT NULL,
EmojiID INT NOT NULL,
Name TEXT NOT NULL,
URL TEXT NOT NULL,
Uses INT NOT NULL,
UNIQUE (GuildID, EmojiID)
);
"""
)
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
suggestions (
Suggestion TEXT NOT NULL
);
"""
)
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
used_titles (
GuildID INT NOT NULL,
Date INT NOT NULL,
Title TEXT NOT NULL
);
"""
)
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
yuds (date INT, userID INT, postID INT, width INT, height INT, quality INT)
"""
)
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
yudminders (userID INT, due INT);
"""
)
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
part (
UserID INT NOT NULL,
GuildID INT NOT NULL,
ChannelID INT NOT NULL,
Due INT NOT NULL,
Error TEXT
);
"""
)
con.execute(
"""
CREATE TABLE
IF NOT EXISTS
hotwords (
UserID INT NOT NULL,
Hotword TEXT NOT NULL
);
"""
)
async def add_hotword(con: aiosqlite.Connection, user_id: int, hotword: str):
await con.execute(
"INSERT INTO hotwords (UserID, Hotword) VALUES (?, ?)", (user_id, hotword)
)
await con.commit()
async def remove_hotword(con: aiosqlite.Connection, user_id: int, hotword: str):
await con.execute(
"DELETE FROM hotwords WHERE UserID = ? AND Hotword = ?", (user_id, hotword)
)
await con.commit()
async def get_hotwords(con: aiosqlite.Connection, user_id: int):
cursor = await con.execute(
"SELECT Hotword FROM hotwords WHERE UserID = ?", (user_id,)
)
rows = await cursor.fetchall()
return [row["Hotword"] for row in rows]
if __name__ == "__main__":
initialise_database(DB_LOCATION)
backup(DB_LOCATION, BACKUPS_LOCATION)
check_backups(BACKUPS_LOCATION)