Add SQLite3-specific migrations

This commit is contained in:
Spotlight 2024-09-24 00:58:18 -05:00
parent 509024d33d
commit 51b3de2307
No known key found for this signature in database
GPG Key ID: 874AA355B3209BDC
4 changed files with 189 additions and 65 deletions

View File

@ -7,9 +7,19 @@ In brief, copy `api/template.private.py` to `api/private.py` and edit accordingl
[Flask-Migrate](https://flask-migrate.readthedocs.io/en/latest/index.html) is used for database migrations.
Please refer to its documentation for in-depth usage.
Regarding this project, `server.py` is the Flask application currently used for migrations.
Please note that `server.py` is the Flask application currently used for migrations.
When interacting with `flask db`, you may have to specify it as the app:
```
flask --app server.py db [...]
```
As a general rule of thumb: whenever you see migrations added, please run them.
You can do so via the following command:
```
flask db upgrade
```
To create migrations:
```
flask db migrate -m "Migration reason"
```

View File

@ -1,43 +1,58 @@
Create Table friends(
friendCode text NOT NULL UNIQUE,
network tinyint NOT NULL,
online boolean NOT NULL,
titleID text NOT NULL,
updID text NOT NULL,
lastAccessed bigint NOT NULL,
accountCreation bigint NOT NULL,
username text,
message text,
mii text,
joinable boolean,
gameDescription text,
lastOnline bigint NOT NULL,
jeuFavori bigint NOT NULL
-- This brings us to our initial Alembic revision.
CREATE TABLE alembic_version (
version_num VARCHAR(32) NOT NULL
CONSTRAINT alembic_version_pkc
PRIMARY KEY
);
INSERT INTO alembic_version VALUES ('f2475122ee84');
CREATE TABLE config (
network INTEGER NOT NULL,
backend_uptime DATETIME,
PRIMARY KEY (network)
);
Create Table config(
backend_uptime text,
network tinyint NOT NULL
-- Provide default uptime for our two defined networks.
INSERT INTO config VALUES (0, NULL);
INSERT INTO config VALUES (1, NULL);
CREATE TABLE friends (
friend_code VARCHAR NOT NULL,
network INTEGER NOT NULL,
online BOOLEAN NOT NULL,
title_id VARCHAR NOT NULL,
upd_id VARCHAR NOT NULL,
last_accessed BIGINT NOT NULL,
account_creation BIGINT NOT NULL,
username VARCHAR,
message VARCHAR,
mii VARCHAR,
joinable BOOLEAN,
game_description VARCHAR,
last_online BIGINT NOT NULL,
favorite_game BIGINT NOT NULL,
PRIMARY KEY (friend_code),
UNIQUE (friend_code)
);
CREATE TABLE discord_friends (
id BIGINT NOT NULL,
friend_code VARCHAR NOT NULL,
network INTEGER NOT NULL,
active BOOLEAN NOT NULL,
PRIMARY KEY (id, friend_code)
);
INSERT INTO config(backend_uptime, network) VALUES (NULL, 0);
INSERT INTO config(backend_uptime, network) VALUES (NULL, 1);
Create Table discord(
ID bigint NOT NULL UNIQUE,
refresh text NOT NULL,
bearer text NOT NULL,
session text,
token text UNIQUE,
lastAccessed bigint NOT NULL,
generationDate bigint NOT NULL,
showProfileButton boolean NOT NULL DEFAULT TRUE,
showSmallImage boolean NOT NULL DEFAULT TRUE
);
Create Table discordFriends(
ID bigint NOT NULL,
friendCode text NOT NULL,
network tinyint NOT NULL,
active boolean NOT NULL
CREATE TABLE discord (
id BIGINT NOT NULL,
refresh VARCHAR NOT NULL,
bearer VARCHAR NOT NULL,
session VARCHAR,
token VARCHAR,
last_accessed BIGINT NOT NULL,
generation_date BIGINT NOT NULL,
show_profile_button BOOLEAN NOT NULL,
show_small_image BOOLEAN NOT NULL,
PRIMARY KEY (id),
UNIQUE (id),
UNIQUE (token)
);

View File

@ -1,28 +1,127 @@
-- This migrates the old database to support pretendo. Don't forget to backup the database first.
-- This migrates the old, handwritten database to the first revision managed by Alembic.
-- Please run `flask db upgrade` as soon as possible!
CREATE TABLE pretendo_friends(
friendCode text NOT NULL UNIQUE,
online boolean NOT NULL,
titleID text NOT NULL,
updID text NOT NULL,
lastAccessed bigint NOT NULL,
accountCreation bigint NOT NULL,
username text,
message text,
mii text,
joinable boolean,
gameDescription text,
lastOnline bigint NOT NULL,
jeuFavori bigint NOT NULL
------------
-- Config --
------------
-- As config now uses a DATETIME, we will delete the old table entirely.
DROP TABLE config;
CREATE TABLE config (
network INTEGER NOT NULL,
backend_uptime DATETIME,
PRIMARY KEY (network)
);
ALTER TABLE discordFriends
ADD network tinyint;
UPDATE discordFriends set network=0;
-- Provide default uptime for our two defined networks.
INSERT INTO config VALUES (0, NULL);
INSERT INTO config VALUES (1, NULL);
ALTER TABLE config
ADD network tinyint;
UPDATE config set network=0; -- This shouldn't be needed, but safe than sorry right?
-------------
-- Friends --
-------------
CREATE TABLE new_friends (
friend_code VARCHAR NOT NULL,
network INTEGER NOT NULL,
online BOOLEAN NOT NULL,
title_id VARCHAR NOT NULL,
upd_id VARCHAR NOT NULL,
last_accessed BIGINT NOT NULL,
account_creation BIGINT NOT NULL,
username VARCHAR,
message VARCHAR,
mii VARCHAR,
joinable BOOLEAN,
game_description VARCHAR,
last_online BIGINT NOT NULL,
favorite_game BIGINT NOT NULL,
PRIMARY KEY (friend_code),
UNIQUE (friend_code)
);
ALTER TABLE friends
RENAME TO nintendo_friends;
-- The primary difference between these two tables are the column names.
-- `jeuFavori` was renamed to its English version, `favorite_game`.
-- `friend_code` is no longer unique.
INSERT INTO new_friends
(friend_code, network, online, title_id,
upd_id, last_accessed, account_creation, username,
message, mii, joinable, game_description,
last_online, favorite_game)
SELECT
friendCode, network, online, titleID,
updID, lastAccessed, accountCreation,
username, message, mii, joinable,
gameDescription, lastOnline, jeuFavori
FROM friends;
-- Swap out.
DROP TABLE friends;
ALTER TABLE new_friends RENAME TO friends;
--------------------
-- discordFriends --
--------------------
-- This table was renamed to discordFriends.
CREATE TABLE discord_friends (
id BIGINT NOT NULL,
friend_code VARCHAR NOT NULL,
network INTEGER NOT NULL,
active BOOLEAN NOT NULL,
PRIMARY KEY (id, friend_code)
);
-- Note that we use `friend_code` instead of `friendCode`.
INSERT INTO discord_friends
(id, friend_code, network, active)
SELECT
ID, friendCode, network, active
FROM discordFriends;
-- Remove our old, poorly-named table.
DROP TABLE discordFriends;
-------------
-- discord --
-------------
CREATE TABLE new_discord (
id BIGINT NOT NULL,
refresh VARCHAR NOT NULL,
bearer VARCHAR NOT NULL,
session VARCHAR,
token VARCHAR,
last_accessed BIGINT NOT NULL,
generation_date BIGINT NOT NULL,
show_profile_button BOOLEAN NOT NULL,
show_small_image BOOLEAN NOT NULL,
PRIMARY KEY (id),
UNIQUE (id),
UNIQUE (token)
);
INSERT INTO new_discord
(id, refresh, bearer, session,
token, last_accessed, generation_date,
show_profile_button, show_small_image)
SELECT
ID, refresh, bearer, session, token, lastAccessed,
generationDate, showProfileButton, showSmallImage
FROM discord;
-- Swap them out.
DROP TABLE discord;
ALTER TABLE new_discord RENAME TO discord;
-------------
-- Alembic --
-------------
-- Lastly, spoof having our initial Alembic revision.
CREATE TABLE alembic_version (
version_num VARCHAR(32) NOT NULL
CONSTRAINT alembic_version_pkc
PRIMARY KEY
);
INSERT INTO alembic_version VALUES ('f2475122ee84');

View File

@ -1,3 +1,3 @@
rm fcLibrary.db
printf 'Please copy and paste:\n\n.open fcLibrary.db\n.read CREATE.sql\n.exit\n\n'
sqlite3
sqlite3 fcLibrary.db < CREATE.sql
echo "Reset!"