feat: moved to using db migrations
This commit is contained in:
parent
a2341c9d9d
commit
c0ba582899
2 changed files with 63 additions and 83 deletions
53
db/1_setup.sql
Normal file
53
db/1_setup.sql
Normal 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);
|
93
src/lib.rs
93
src/lib.rs
|
@ -61,91 +61,18 @@ pub struct Accounts {
|
||||||
|
|
||||||
pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
|
||||||
let database = format!("{}/{}", &config.home, &config.database);
|
let database = format!("{}/{}", &config.home, &config.database);
|
||||||
println!("Database: {:?}", &database);
|
|
||||||
let pool = SqlitePoolOptions::new()
|
let pool = SqlitePoolOptions::new()
|
||||||
.max_connections(5)
|
.max_connections(5)
|
||||||
.connect_with(SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?.create_if_missing(true))
|
.connect_with(
|
||||||
.await?;
|
SqliteConnectOptions::from_str(&format!("sqlite://{}", database))?
|
||||||
|
.foreign_keys(true)
|
||||||
|
.create_if_missing(true),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
sqlx::query(
|
// migrations are amazing!
|
||||||
"CREATE TABLE IF NOT EXISTS accounts_wolves (
|
sqlx::migrate!("./db").run(&pool).await?;
|
||||||
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?;
|
|
||||||
|
|
||||||
Ok(pool)
|
Ok(pool)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue