users: migrations, queries, users crate: create user, get first admin user
This commit is contained in:
parent
8af226cd05
commit
3e168c19bc
13 changed files with 283 additions and 1 deletions
1
crates/database/migrations/20230305135630_users.down.sql
Normal file
1
crates/database/migrations/20230305135630_users.down.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
drop table if exists users;
|
||||
25
crates/database/migrations/20230305135630_users.up.sql
Normal file
25
crates/database/migrations/20230305135630_users.up.sql
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
create table if not exists users
|
||||
(
|
||||
id TEXT not null primary key,
|
||||
created_at TEXT not null default CURRENT_TIMESTAMP,
|
||||
updated_at TEXT not null default CURRENT_TIMESTAMP,
|
||||
is_admin INTEGER not null,
|
||||
username TEXT not null unique,
|
||||
name TEXT,
|
||||
email TEXT unique,
|
||||
password TEXT,
|
||||
password_recover TEXT,
|
||||
paper_key TEXT,
|
||||
is_archived INTEGER not null default 0
|
||||
);
|
||||
|
||||
-- update "updated_at"
|
||||
create trigger if not exists users_updated_at
|
||||
after update
|
||||
on users
|
||||
for each row
|
||||
begin
|
||||
update users
|
||||
set updated_at = CURRENT_TIMESTAMP
|
||||
where id is NEW.id;
|
||||
end;
|
||||
5
crates/database/queries/settings/set_first_admin.sql
Normal file
5
crates/database/queries/settings/set_first_admin.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
update settings
|
||||
|
||||
set first_admin = ?
|
||||
|
||||
where id is 0
|
||||
20
crates/database/queries/users/get_initial_admin.sql
Normal file
20
crates/database/queries/users/get_initial_admin.sql
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
select u.id,
|
||||
u.created_at as "created_at: DateTime<Utc>",
|
||||
u.updated_at as "updated_at: DateTime<Utc>",
|
||||
u.is_admin as "is_admin: bool",
|
||||
u.username,
|
||||
u.name,
|
||||
u.email,
|
||||
u.password,
|
||||
u.password_recover,
|
||||
u.paper_key,
|
||||
u.is_archived as "is_archived: bool"
|
||||
from users u
|
||||
|
||||
inner join settings s on u.id = s.first_admin
|
||||
|
||||
where u.is_admin is 1
|
||||
and u.is_archived is 0
|
||||
and u.id is s.first_admin
|
||||
|
||||
limit 1
|
||||
2
crates/database/queries/users/insert.sql
Normal file
2
crates/database/queries/users/insert.sql
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
insert into users (id, is_admin, username, password)
|
||||
values (?, ?, ?, ?)
|
||||
|
|
@ -20,6 +20,16 @@
|
|||
},
|
||||
"query": "update settings\n\nset business_name = ?\n\nwhere id is 0\n"
|
||||
},
|
||||
"520fe30e21f6b6c4d9a47c457675eebd144cf020e9230d154e9e4d0c8d6e01ca": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Right": 4
|
||||
}
|
||||
},
|
||||
"query": "insert into users (id, is_admin, username, password)\nvalues (?, ?, ?, ?)\n"
|
||||
},
|
||||
"62c75412f673f6a293b0d188d79c50676ec21cf94e2e50e18f9279c91e6b85c8": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
|
|
@ -30,6 +40,94 @@
|
|||
},
|
||||
"query": "insert or ignore into settings(id)\nvalues (0);"
|
||||
},
|
||||
"aae93a39c5a9f46235b5ef871b45ba76d7efa1677bfe8291a62b8cbf9cd9e0d5": {
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"nullable": [],
|
||||
"parameters": {
|
||||
"Right": 1
|
||||
}
|
||||
},
|
||||
"query": "update settings\n\nset first_admin = ?\n\nwhere id is 0\n"
|
||||
},
|
||||
"c5a57c971d07532ec0cc897b5ac06e0814e506f9c24647d1eaf44174dc0a5954": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
{
|
||||
"name": "id",
|
||||
"ordinal": 0,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "created_at: DateTime<Utc>",
|
||||
"ordinal": 1,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "updated_at: DateTime<Utc>",
|
||||
"ordinal": 2,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "is_admin: bool",
|
||||
"ordinal": 3,
|
||||
"type_info": "Int64"
|
||||
},
|
||||
{
|
||||
"name": "username",
|
||||
"ordinal": 4,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "name",
|
||||
"ordinal": 5,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "email",
|
||||
"ordinal": 6,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "password",
|
||||
"ordinal": 7,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "password_recover",
|
||||
"ordinal": 8,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "paper_key",
|
||||
"ordinal": 9,
|
||||
"type_info": "Text"
|
||||
},
|
||||
{
|
||||
"name": "is_archived: bool",
|
||||
"ordinal": 10,
|
||||
"type_info": "Int64"
|
||||
}
|
||||
],
|
||||
"nullable": [
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
true,
|
||||
false
|
||||
],
|
||||
"parameters": {
|
||||
"Right": 0
|
||||
}
|
||||
},
|
||||
"query": "select u.id,\n u.created_at as \"created_at: DateTime<Utc>\",\n u.updated_at as \"updated_at: DateTime<Utc>\",\n u.is_admin as \"is_admin: bool\",\n u.username,\n u.name,\n u.email,\n u.password,\n u.password_recover,\n u.paper_key,\n u.is_archived as \"is_archived: bool\"\nfrom users u\n\n inner join settings s on u.id = s.first_admin\n\nwhere u.is_admin is 1\n and u.is_archived is 0\n and u.id is s.first_admin\n\nlimit 1"
|
||||
},
|
||||
"cc69514c4d9457462e634eb58cbfc82b454197c5cb7f4a451954eb5a421afc3b": {
|
||||
"describe": {
|
||||
"columns": [
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
mod settings;
|
||||
mod users;
|
||||
|
||||
pub use settings::Settings;
|
||||
pub use users::Users;
|
||||
|
|
|
|||
44
crates/database/src/tables/users.rs
Normal file
44
crates/database/src/tables/users.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
use crate::error::{handle_error, Error};
|
||||
use sqlx::sqlite::SqliteQueryResult;
|
||||
use sqlx::types::chrono::{DateTime, Utc};
|
||||
use sqlx::{FromRow, SqliteExecutor};
|
||||
|
||||
#[derive(FromRow)]
|
||||
pub struct Users {
|
||||
pub id: String,
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub updated_at: DateTime<Utc>,
|
||||
pub is_admin: bool,
|
||||
pub username: String,
|
||||
pub name: Option<String>,
|
||||
pub email: Option<String>,
|
||||
pub password: Option<String>,
|
||||
pub password_recover: Option<String>,
|
||||
pub paper_key: Option<String>,
|
||||
pub is_archived: bool,
|
||||
}
|
||||
|
||||
impl Users {
|
||||
pub async fn get_initial_admin(conn: impl SqliteExecutor<'_>) -> Result<Option<Self>, Error> {
|
||||
sqlx::query_file_as!(Self, "queries/users/get_initial_admin.sql")
|
||||
.fetch_optional(conn)
|
||||
.await
|
||||
.map_err(handle_error)
|
||||
}
|
||||
|
||||
pub async fn insert(
|
||||
conn: impl SqliteExecutor<'_>,
|
||||
id: &str,
|
||||
is_admin: bool,
|
||||
username: &str,
|
||||
password: Option<&str>,
|
||||
) -> Result<Option<()>, Error> {
|
||||
let query: SqliteQueryResult =
|
||||
sqlx::query_file!("queries/users/insert.sql", id, is_admin, username, password)
|
||||
.execute(conn)
|
||||
.await
|
||||
.map_err(handle_error)?;
|
||||
|
||||
Ok((query.rows_affected() == 1).then_some(()))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue