feat: add mysql (mariadb) support

This commit is contained in:
Logan 2024-08-20 03:30:17 -06:00
parent dbdbe530c4
commit c46e6025d3
6 changed files with 28 additions and 11 deletions

View File

@ -42,3 +42,13 @@ CLIENT_SECRET:str = "" # Taken from OAuth2 page
HOST:str = ""
# http(s)://subdomain.domain.extension
# No ending slash!
### DATABASE-SPECIFIC ###
IS_SQLITE:bool = True # SQLite should generally only be used for testing. It is recommended to use MySQL software like MariaDB
# MySQL specifc
DB_HOST:str = "localhost"
DB_NAME:str = "3dsrpc"
DB_USERNAME:str = "username"
DB_PASSWORD:str = "password"

View File

@ -7,7 +7,7 @@ from sqlalchemy import create_engine, delete, select, update
from sqlalchemy.orm import Session
import anyio, sys, argparse
from database import start_db_time, Friend, DiscordFriends
from database import start_db_time, get_db_url, Friend, DiscordFriends
sys.path.append('../')
from api.private import SERIAL_NUMBER, MAC_ADDRESS, DEVICE_CERT, DEVICE_NAME, REGION, LANGUAGE, NINTENDO_PID, PRETENDO_PID, PID_HMAC, NINTENDO_NEX_PASSWORD, PRETENDO_NEX_PASSWORD
@ -28,7 +28,7 @@ network: NetworkType = NetworkType.NINTENDO
async def main():
engine = create_engine('sqlite:///' + os.path.abspath('sqlite/fcLibrary.db'))
engine = create_engine(get_db_url())
while True:
time.sleep(1)

View File

@ -3,10 +3,11 @@ import os
from sqlalchemy import create_engine, delete
from sqlalchemy.orm import DeclarativeBase, mapped_column, Mapped, Session
from sqlalchemy.types import Integer, TypeDecorator
import sys
import sys, urllib, pymysql
sys.path.append('../')
from api.networks import NetworkType
from api.private import DB_HOST, DB_NAME, DB_USERNAME, DB_PASSWORD, IS_SQLITE
class Base(DeclarativeBase):
@ -77,10 +78,16 @@ class Discord(Base):
def start_db_time(time: float, network_type: NetworkType):
"""Updates the database to track the starting time for the specific backend."""
engine = create_engine('sqlite:///' + os.path.abspath('sqlite/fcLibrary.db'))
engine = create_engine(get_db_url())
with Session(engine) as session:
# TODO: This should be an upsert, not a deletion and insertion.
session.execute(delete(Config).where(Config.network == network_type))
new_time = Config(network=network_type, backend_uptime=time)
session.add(new_time)
session.commit()
def get_db_url():
if IS_SQLITE:
return 'sqlite:///' + os.path.abspath('sqlite/fcLibrary.db')
else:
return 'mysql+pymysql://' + urllib.parse.quote_plus(DB_USERNAME) + ':' + urllib.parse.quote_plus(DB_PASSWORD) + '@' + DB_HOST + '/' + urllib.parse.quote_plus(DB_NAME)

View File

@ -7,7 +7,7 @@ from api.networks import NetworkType
from sqlalchemy import create_engine, select, update, delete
from sqlalchemy.orm import Session
from database import DiscordFriends, Friend
from database import get_db_url, DiscordFriends, Friend
from database import Discord as DiscordTable
API_ENDPOINT:str = 'https://discord.com/api/v10'
@ -17,7 +17,7 @@ with open('./cache/databases.dat', 'rb') as file:
titleDatabase = t[0]
titlesToUID = t[1]
engine = create_engine('sqlite:///' + os.path.abspath('sqlite/fcLibrary.db'))
engine = create_engine(get_db_url())
class DiscordSession():
def retire(self, refresh):

View File

@ -9,7 +9,7 @@ import sqlite3, requests, sys, os, time, json, multiprocessing, datetime, xmltod
from sqlalchemy import create_engine, select, update, insert, delete
from sqlalchemy.orm import Session
from database import start_db_time, Friend, DiscordFriends, Discord, Config
from database import start_db_time, get_db_url, Friend, DiscordFriends, Discord, Config
sys.path.append('../')
from api import *
@ -19,11 +19,8 @@ from api.public import pretendoBotFC, nintendoBotFC
from api.networks import NetworkType, nameToNetworkType
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.abspath('sqlite/fcLibrary.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
#db1 = SQLAlchemy(app)
engine = create_engine('sqlite:///' + os.path.abspath('sqlite/fcLibrary.db'))
engine = create_engine(get_db_url())
limiter = Limiter(app, key_func = lambda : request.access_route[-1])

View File

@ -20,6 +20,9 @@ Create Table config(
network tinyint NOT NULL
);
INSERT INTO config(BACKEND_UPTIME, network) VALUES (0, 0);
INSERT INTO config(BACKEND_UPTIME, network) VALUES (0, 1);
Create Table discord(
ID bigint NOT NULL UNIQUE,
refresh text NOT NULL,