feat: Implements auto-updating database table
- Automatically updates database table from old versions / Invitarr - Fancy text formatting to boost my ego - Removes synced variable from config, as we now sync always (guild-specific sync is automatic.) task: gh-9
This commit is contained in:
65
app/bot/helper/dbupdater.py
Normal file
65
app/bot/helper/dbupdater.py
Normal file
@@ -0,0 +1,65 @@
|
||||
import sqlite3
|
||||
|
||||
CURRENT_VERSION = 'Membarr V1.1'
|
||||
|
||||
table_history = {
|
||||
'Invitarr V1.0': [
|
||||
(0, 'id', 'INTEGER', 1, None, 1),
|
||||
(1, 'discord_username', 'TEXT', 1, None, 0),
|
||||
(2, 'email', 'TEXT', 1, None, 0)
|
||||
],
|
||||
'Membarr V1.1': [
|
||||
(0, 'id', 'INTEGER', 1, None, 1),
|
||||
(1, 'discord_username', 'TEXT', 1, None, 0),
|
||||
(2, 'email', 'TEXT', 0, None, 0),
|
||||
(3, 'jellyfin_username', 'TEXT', 0, None, 0)
|
||||
]
|
||||
}
|
||||
|
||||
def check_table_version(conn, tablename):
|
||||
dbcur = conn.cursor()
|
||||
dbcur.execute(f"PRAGMA table_info({tablename})")
|
||||
table_format = dbcur.fetchall()
|
||||
for app_version in table_history:
|
||||
if table_history[app_version] == table_format:
|
||||
return app_version
|
||||
raise ValueError("Could not identify database table version.")
|
||||
|
||||
def update_table(conn, tablename):
|
||||
version = check_table_version(conn, tablename)
|
||||
print('------')
|
||||
print(f'DB table version: {version}')
|
||||
if version == CURRENT_VERSION:
|
||||
print('DB table up to date!')
|
||||
print('------')
|
||||
return
|
||||
|
||||
# Table NOT up to date.
|
||||
# Update to Membarr V1.1 table
|
||||
if version == 'Invitarr V1.0':
|
||||
print("Upgrading DB table from Invitarr v1.0 to Membarr V1.1")
|
||||
# Create temp table
|
||||
conn.execute(
|
||||
'''CREATE TABLE "membarr_temp_upgrade_table" (
|
||||
"id" INTEGER NOT NULL UNIQUE,
|
||||
"discord_username" TEXT NOT NULL UNIQUE,
|
||||
"email" TEXT,
|
||||
"jellyfin_username" TEXT,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);''')
|
||||
conn.execute(f'''
|
||||
INSERT INTO membarr_temp_upgrade_table(id, discord_username, email)
|
||||
SELECT id, discord_username, email
|
||||
FROM {tablename};
|
||||
''')
|
||||
conn.execute(f'''
|
||||
DROP TABLE {tablename};
|
||||
''')
|
||||
conn.execute(f'''
|
||||
ALTER TABLE membarr_temp_upgrade_table RENAME TO {tablename}
|
||||
''')
|
||||
conn.commit()
|
||||
version = 'Membarr V1.1'
|
||||
|
||||
print('------')
|
||||
|
||||
Reference in New Issue
Block a user