diff --git a/Cargo.toml b/Cargo.toml index 58b3b12..cb6d3cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ surf = "2.3.2" dotenvy = "0.15.7" # For sqlite -sqlx = { version = "0.7.1", features = [ "runtime-tokio", "sqlite" ] } +sqlx = { version = "0.7.1", features = [ "runtime-tokio", "sqlite", "migrate" ] } # create random strings rand = "0.8.5" diff --git a/db/migrations/1_setup.sql b/db/migrations/1_setup.sql new file mode 100644 index 0000000..aa4fb93 --- /dev/null +++ b/db/migrations/1_setup.sql @@ -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) +); \ No newline at end of file diff --git a/db/migrations/2_minecraft-server.sql b/db/migrations/2_minecraft-server.sql new file mode 100644 index 0000000..3aedbc3 --- /dev/null +++ b/db/migrations/2_minecraft-server.sql @@ -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; \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index c8ca3f9..ca57dbc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -269,59 +269,11 @@ pub async fn db_init(config: &Config) -> Result, Error> { ) .await?; - sqlx::query( - "CREATE TABLE IF NOT EXISTS wolves ( - id_wolves integer PRIMARY KEY, - 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) + // migrations are amazing! + sqlx::migrate!("./db/migrations") + .run(&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?; - Ok(pool) }