feat: got migrations working!!!!

This commit is contained in:
silver 2024-03-03 00:18:46 +00:00
parent 480fc9b1a0
commit a9f55da04d
4 changed files with 45 additions and 52 deletions

View file

@ -21,7 +21,7 @@ surf = "2.3.2"
dotenvy = "0.15.7" dotenvy = "0.15.7"
# For sqlite # For sqlite
sqlx = { version = "0.7.1", features = [ "runtime-tokio", "sqlite" ] } sqlx = { version = "0.7.1", features = [ "runtime-tokio", "sqlite", "migrate" ] }
# create random strings # create random strings
rand = "0.8.5" rand = "0.8.5"

38
db/migrations/1_setup.sql Normal file
View file

@ -0,0 +1,38 @@
-- setup initial tables
-- this handles the users "floating" account
CREATE TABLE IF NOT EXISTS wolves (
id_wolves integer PRIMARY KEY,
email text not null,
discord integer,
minecraft text
);
CREATE INDEX IF NOT EXISTS index_discord ON wolves (discord);
-- used to verify the users email address
CREATE TABLE IF NOT EXISTS wolves_verify (
discord integer PRIMARY KEY,
email text not null,
auth_code text not null,
date_expiry text not null
);
CREATE INDEX IF NOT EXISTS index_date_expiry ON wolves_verify (date_expiry);
-- information on teh server the bot is registered on
CREATE TABLE IF NOT EXISTS servers (
server integer PRIMARY KEY,
wolves_api text not null,
role_past integer,
role_current integer,
member_past integer DEFAULT 0,
member_current integer DEFAULT 0
);
-- keep track of the members on the server
CREATE TABLE IF NOT EXISTS server_members (
server integer not null,
id_wolves integer not null,
expiry text not null,
PRIMARY KEY(server,id_wolves),
FOREIGN KEY (id_wolves) REFERENCES wolves (id_wolves)
);

View file

@ -0,0 +1,3 @@
-- add teh option to associate each discord server with a minecraft one managed by skynet
ALTER TABLE servers
ADD server_minecraft text;

View file

@ -269,57 +269,9 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
) )
.await?; .await?;
sqlx::query( // migrations are amazing!
"CREATE TABLE IF NOT EXISTS wolves ( sqlx::migrate!("./db/migrations")
id_wolves integer PRIMARY KEY, .run(&pool)
email text not null,
discord integer,
minecraft text
)",
)
.execute(&pool)
.await?;
sqlx::query("CREATE INDEX IF NOT EXISTS index_discord ON wolves (discord)").execute(&pool).await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS wolves_verify (
discord integer PRIMARY KEY,
email text not null,
auth_code text not null,
date_expiry text not null
)",
)
.execute(&pool)
.await?;
sqlx::query("CREATE INDEX IF NOT EXISTS index_date_expiry ON wolves_verify (date_expiry)")
.execute(&pool)
.await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS server_members (
server integer not null,
id_wolves integer not null,
expiry text not null,
PRIMARY KEY(server,id_wolves),
FOREIGN KEY (id_wolves) REFERENCES wolves (id_wolves)
)",
)
.execute(&pool)
.await?;
sqlx::query(
"CREATE TABLE IF NOT EXISTS servers (
server integer PRIMARY KEY,
wolves_api text not null,
role_past integer,
role_current integer,
member_past integer DEFAULT 0,
member_current integer DEFAULT 0
)",
)
.execute(&pool)
.await?; .await?;
Ok(pool) Ok(pool)