diff --git a/db/1_setup.sql b/db/1_setup.sql new file mode 100644 index 0000000..89a423d --- /dev/null +++ b/db/1_setup.sql @@ -0,0 +1,53 @@ + + +CREATE TABLE IF NOT EXISTS accounts_wolves ( + id_wolves integer PRIMARY KEY, + id_student text, + email text NOT NULL, + expiry text NOT NULL, + name_first text, + name_second text +); + +CREATE TABLE IF NOT EXISTS accounts_new ( + mail text PRIMARY KEY, + auth_code text NOT NULL, + date_iso text NOT NULL, + date_expiry text NOT NULL, + name_first text NOT NULL, + name_surname text NOT NULL, + id_student text NOT NULL +); + + +CREATE INDEX IF NOT EXISTS index_auth_code ON accounts_new (auth_code); +CREATE INDEX IF NOT EXISTS index_date_expiry ON accounts_new (date_expiry); + + +CREATE TABLE IF NOT EXISTS accounts_ssh ( + user text PRIMARY KEY, + auth_code text NOT NULL, + email text NOT NULL +); + +CREATE TABLE IF NOT EXISTS accounts_reset ( + user text PRIMARY KEY, + auth_code text NOT NULL, + date_expiry text NOT NULL +); + +CREATE INDEX IF NOT EXISTS index_auth_code ON accounts_reset (auth_code); +CREATE INDEX IF NOT EXISTS index_date_expiry ON accounts_reset (date_expiry); + +CREATE TABLE IF NOT EXISTS accounts ( + user text PRIMARY KEY, + uid integer NOT NULL, + mail text NOT NULL, + student_id text NOT NULL, + secure integer NOT NULL +); + + +CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid); +CREATE INDEX IF NOT EXISTS index_mail ON accounts (mail); +CREATE INDEX IF NOT EXISTS index_student_id ON accounts (student_id); \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 793d31c..1d7fefb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,91 +61,18 @@ pub struct Accounts { pub async fn db_init(config: &Config) -> Result, Error> { let database = format!("{}/{}", &config.home, &config.database); - println!("Database: {:?}", &database); + let pool = SqlitePoolOptions::new() - .max_connections(5) - .connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true)) - .await?; + .max_connections(5) + .connect_with( + SqliteConnectOptions::from_str(&format!("sqlite://{}", database))? + .foreign_keys(true) + .create_if_missing(true), + ) + .await?; - sqlx::query( - "CREATE TABLE IF NOT EXISTS accounts_wolves ( - id_wolves integer PRIMARY KEY, - id_student text, - email text NOT NULL, - expiry text NOT NULL, - name_first text, - name_second text - )", - ) - .execute(&pool) - .await?; - - sqlx::query( - "CREATE TABLE IF NOT EXISTS accounts_new ( - mail text PRIMARY KEY, - auth_code text NOT NULL, - date_iso text NOT NULL, - date_expiry text NOT NULL, - name_first text NOT NULL, - name_surname text NOT NULL, - id_student text NOT NULL - )", - ) - .execute(&pool) - .await?; - - sqlx::query("CREATE INDEX IF NOT EXISTS index_auth_code ON accounts_new (auth_code)") - .execute(&pool) - .await?; - sqlx::query("CREATE INDEX IF NOT EXISTS index_date_expiry ON accounts_new (date_expiry)") - .execute(&pool) - .await?; - - sqlx::query( - "CREATE TABLE IF NOT EXISTS accounts_ssh ( - user text PRIMARY KEY, - auth_code text NOT NULL, - email text NOT NULL - )", - ) - .execute(&pool) - .await?; - - sqlx::query( - "CREATE TABLE IF NOT EXISTS accounts_reset ( - user text PRIMARY KEY, - auth_code text NOT NULL, - date_expiry text NOT NULL - )", - ) - .execute(&pool) - .await?; - - sqlx::query("CREATE INDEX IF NOT EXISTS index_auth_code ON accounts_reset (auth_code)") - .execute(&pool) - .await?; - sqlx::query("CREATE INDEX IF NOT EXISTS index_date_expiry ON accounts_reset (date_expiry)") - .execute(&pool) - .await?; - - // this is for active use - sqlx::query( - "CREATE TABLE IF NOT EXISTS accounts ( - user text PRIMARY KEY, - uid integer NOT NULL, - mail text NOT NULL, - student_id text NOT NULL, - secure integer NOT NULL - )", - ) - .execute(&pool) - .await?; - - sqlx::query("CREATE INDEX IF NOT EXISTS index_uid_number ON accounts (uid)").execute(&pool).await?; - sqlx::query("CREATE INDEX IF NOT EXISTS index_mail ON accounts (mail)").execute(&pool).await?; - sqlx::query("CREATE INDEX IF NOT EXISTS index_student_id ON accounts (student_id)") - .execute(&pool) - .await?; + // migrations are amazing! + sqlx::migrate!("./db").run(&pool).await?; Ok(pool) }