feat: moved to using db migrations

This commit is contained in:
silver 2025-03-10 20:29:07 +00:00
parent a2341c9d9d
commit c0ba582899
Signed by: silver
GPG key ID: 36F93D61BAD3FD7D
2 changed files with 63 additions and 83 deletions

53
db/1_setup.sql Normal file
View file

@ -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);

View file

@ -61,91 +61,18 @@ pub struct Accounts {
pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, 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)
}