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:
Yoruio
2022-07-14 01:08:32 -06:00
parent 8375395bb8
commit 22c9bd1da9
6 changed files with 93 additions and 19 deletions

View File

@@ -1,5 +1,6 @@
from pickle import FALSE from pickle import FALSE
import app.bot.helper.jellyfinhelper as jelly import app.bot.helper.jellyfinhelper as jelly
from app.bot.helper.textformat import bcolors
import discord import discord
from discord.ext import commands from discord.ext import commands
from discord import app_commands from discord import app_commands
@@ -27,7 +28,6 @@ try:
PLEXPASS = config.get(BOT_SECTION, 'plex_pass') PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name') PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
except: except:
print("Could not load plex config")
plex_configured = False plex_configured = False
# Get Plex roles config # Get Plex roles config
@@ -90,11 +90,6 @@ try:
except: except:
USE_PLEX = False USE_PLEX = False
try:
synced = not (float(config.get(BOT_SECTION, "sync_version")) < MEMBARR_VERSION)
except:
synced = False
if USE_PLEX and plex_configured: if USE_PLEX and plex_configured:
try: try:
print("Connecting to Plex......") print("Connecting to Plex......")
@@ -120,9 +115,11 @@ class app(commands.Cog):
@commands.Cog.listener() @commands.Cog.listener()
async def on_ready(self): async def on_ready(self):
print('Made by Yoruio https://github.com/Yoruio/') print('------')
print('Forked from Invitarr https://github.com/Sleepingpirates/Invitarr') print(bcolors.AUTHOR + "{:^41}".format(f"MEMBARR V {MEMBARR_VERSION}") + bcolors.ENDC)
print('Named by lordfransie') print(f'{bcolors.AUTHOR}Made by Yoruio https://github.com/Yoruio/{bcolors.ENDC}')
print(f'Forked from Invitarr https://github.com/Sleepingpirates/Invitarr')
print(f'Named by lordfransie')
print(f'Logged in as {self.bot.user} (ID: {self.bot.user.id})') print(f'Logged in as {self.bot.user} (ID: {self.bot.user.id})')
print('------') print('------')

View File

@@ -12,7 +12,7 @@ config = configparser.ConfigParser()
CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass', CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass',
'plex_roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id', 'plex_roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id',
'auto_remove_user', 'jellyfin_api_key', 'jellyfin_server_url', 'jellyfin_roles', 'auto_remove_user', 'jellyfin_api_key', 'jellyfin_server_url', 'jellyfin_roles',
'jellyfin_libs', 'plex_enabled', 'jellyfin_enabled', 'sync_version'] 'jellyfin_libs', 'plex_enabled', 'jellyfin_enabled']
# settings # settings
Discord_bot_token = "" Discord_bot_token = ""
@@ -133,12 +133,6 @@ except:
print("Could not get Plex enable config. Defaulting to False") print("Could not get Plex enable config. Defaulting to False")
USE_PLEX = False USE_PLEX = False
try:
synced = not (float(config.get(BOT_SECTION, "sync_version")) < MEMBARR_VERSION)
except:
print("Could not find previously synced version. Setting synced to false.")
synced = False
def get_config(): def get_config():
""" """
Function to return current config Function to return current config

View File

@@ -1,6 +1,9 @@
import sqlite3 import sqlite3
from app.bot.helper.dbupdater import check_table_version, update_table
DB_URL = 'app/config/app.db' DB_URL = 'app/config/app.db'
DB_TABLE = 'clients'
def create_connection(db_file): def create_connection(db_file):
""" create a database connection to a SQLite database """ """ create a database connection to a SQLite database """
@@ -26,7 +29,7 @@ def checkTableExists(dbcon, tablename):
conn = create_connection(DB_URL) conn = create_connection(DB_URL)
# Checking if table exists # Checking if table exists
if checkTableExists(conn, 'clients'): if checkTableExists(conn, DB_TABLE):
print('Table exists.') print('Table exists.')
else: else:
conn.execute( conn.execute(
@@ -38,6 +41,8 @@ else:
PRIMARY KEY("id" AUTOINCREMENT) PRIMARY KEY("id" AUTOINCREMENT)
);''') );''')
update_table(conn, DB_TABLE)
def save_user_email(username, email): def save_user_email(username, email):
if username and email: if username and email:
conn.execute(f""" conn.execute(f"""

View 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('------')

View File

@@ -0,0 +1,14 @@
from multiprocessing import AuthenticationError
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
AUTHOR = '\033[1;45;96m'

3
run.py
View File

@@ -11,14 +11,13 @@ from app.bot.helper.confighelper import MEMBARR_VERSION, switch, Discord_bot_tok
import app.bot.helper.confighelper as confighelper import app.bot.helper.confighelper as confighelper
import app.bot.helper.jellyfinhelper as jelly import app.bot.helper.jellyfinhelper as jelly
from app.bot.helper.message import * from app.bot.helper.message import *
maxroles = 10 maxroles = 10
if switch == 0: if switch == 0:
print("Missing Config.") print("Missing Config.")
sys.exit() sys.exit()
print(f"V {MEMBARR_VERSION}")
class Bot(commands.Bot): class Bot(commands.Bot):
def __init__(self) -> None: def __init__(self) -> None:
print("Initializing Discord bot") print("Initializing Discord bot")