Skip to content

Commit

Permalink
Bake provisioning into admin tool
Browse files Browse the repository at this point in the history
  • Loading branch information
h3ndrk committed Dec 1, 2023
1 parent ea1eb38 commit 50fd72b
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 46 deletions.
46 changes: 0 additions & 46 deletions provision.sql

This file was deleted.

11 changes: 11 additions & 0 deletions src/application/administration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::authentication::hash;

#[async_trait]
pub trait AdministrationService {
async fn provision(&self) -> Result<(), Error>;
async fn reset_password(&self, user: &str, team: &str, password: &str) -> Result<(), Error>;
async fn import(
&self,
Expand Down Expand Up @@ -138,6 +139,16 @@ impl<
MemberRepo,
>
{
async fn provision(&self) -> Result<(), Error> {
self.team_repository.provision().await?;
self.user_repository.provision().await?;
self.role_repository.provision().await?;
self.token_repository.provision().await?;
self.talk_repository.provision().await?;
self.member_repository.provision().await?;
Ok(())
}

async fn reset_password(&self, user: &str, team: &str, password: &str) -> Result<(), Error> {
let Some(team_id) = self.team_repository.get_id_by_name(team).await? else {
eprintln!("unknown team");
Expand Down
2 changes: 2 additions & 0 deletions src/bin/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct Arguments {

#[derive(Subcommand)]
enum Command {
Provision,
ResetPassword {
user: String,
team: String,
Expand Down Expand Up @@ -61,6 +62,7 @@ async fn main() {
);

match arguments.command {
Command::Provision => service.provision().await.unwrap(),
Command::ResetPassword {
user,
team,
Expand Down
18 changes: 18 additions & 0 deletions src/persistence/member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sqlx::{query, query_as, Error, Pool, Sqlite};

#[async_trait]
pub trait MemberRepository {
async fn provision(&self) -> Result<(), Error>;
async fn get_state_by_user_and_talk(&self, user_id: i64, talk_id: i64) -> Result<State, Error>;
async fn set_state_by_user_and_talk(
&self,
Expand Down Expand Up @@ -44,6 +45,23 @@ impl SqliteMemberRepository {

#[async_trait]
impl MemberRepository for SqliteMemberRepository {
async fn provision(&self) -> Result<(), Error> {
query("DROP TABLE IF EXISTS members")
.execute(self.pool.as_ref())
.await?;
query(
"CREATE TABLE members (
user INTEGER REFERENCES users(id) NOT NULL,
talk INTEGER REFERENCES talks(id) NOT NULL,
is_nerd INTEGER NOT NULL,
CONSTRAINT user_and_talk UNIQUE (user, talk)
)",
)
.execute(self.pool.as_ref())
.await?;
Ok(())
}

async fn get_state_by_user_and_talk(&self, user_id: i64, talk_id: i64) -> Result<State, Error> {
match query_as("SELECT is_nerd FROM members WHERE user = ? AND talk = ?")
.bind(user_id)
Expand Down
16 changes: 16 additions & 0 deletions src/persistence/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use sqlx::{query, query_as, Error, Pool, Sqlite};

#[async_trait]
pub trait RoleRepository {
async fn provision(&self) -> Result<(), Error>;
async fn get_roles_by_id(&self, user_id: i64) -> Result<Vec<Role>, Error>;
async fn get_all(&self) -> Result<Vec<UserRole>, Error>;
async fn clear(&self) -> Result<(), Error>;
Expand Down Expand Up @@ -33,6 +34,21 @@ impl SqliteRoleRepository {

#[async_trait]
impl RoleRepository for SqliteRoleRepository {
async fn provision(&self) -> Result<(), Error> {
query("DROP TABLE IF EXISTS roles")
.execute(self.pool.as_ref())
.await?;
query(
"CREATE TABLE roles (
user INTEGER REFERENCES users(id) NOT NULL,
role INTEGER NOT NULL
)",
)
.execute(self.pool.as_ref())
.await?;
Ok(())
}

async fn get_roles_by_id(&self, user_id: i64) -> Result<Vec<Role>, Error> {
let roles = query_as("SELECT role FROM roles WHERE user = ?")
.bind(user_id)
Expand Down
21 changes: 21 additions & 0 deletions src/persistence/talk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sqlx::{query, query_as, Error, Pool, Sqlite};

#[async_trait]
pub trait TalkRepository {
async fn provision(&self) -> Result<(), Error>;
async fn insert(
&self,
creator_id: i64,
Expand Down Expand Up @@ -55,6 +56,26 @@ impl SqliteTalkRepository {

#[async_trait]
impl TalkRepository for SqliteTalkRepository {
async fn provision(&self) -> Result<(), Error> {
query("DROP TABLE IF EXISTS talks")
.execute(self.pool.as_ref())
.await?;
query(
"CREATE TABLE talks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
creator INTEGER REFERENCES users(id) NOT NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
scheduled_at INTEGER,
duration INTEGER NOT NULL,
location TEXT
)",
)
.execute(self.pool.as_ref())
.await?;
Ok(())
}

async fn insert(
&self,
creator_id: i64,
Expand Down
16 changes: 16 additions & 0 deletions src/persistence/team.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use sqlx::{query, query_as, Error, Pool, Sqlite};

#[async_trait]
pub trait TeamRepository {
async fn provision(&self) -> Result<(), Error>;
async fn get_name_by_id(&self, id: i64) -> Result<Option<String>, Error>;
async fn get_id_by_name(&self, name: &str) -> Result<Option<i64>, Error>;
async fn get_all(&self) -> Result<Vec<Team>, Error>;
Expand All @@ -29,6 +30,21 @@ impl SqliteTeamRepository {

#[async_trait]
impl TeamRepository for SqliteTeamRepository {
async fn provision(&self) -> Result<(), Error> {
query("DROP TABLE IF EXISTS teams")
.execute(self.pool.as_ref())
.await?;
query(
"CREATE TABLE teams (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
)",
)
.execute(self.pool.as_ref())
.await?;
Ok(())
}

async fn get_name_by_id(&self, id: i64) -> Result<Option<String>, Error> {
query_as("SELECT name FROM teams WHERE id = ?")
.bind(id)
Expand Down
17 changes: 17 additions & 0 deletions src/persistence/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use sqlx::{query, query_as, Error, Pool, Sqlite};

#[async_trait]
pub trait TokenRepository {
async fn provision(&self) -> Result<(), Error>;
async fn insert_or_update_token_for_user(
&self,
token: &str,
Expand Down Expand Up @@ -39,6 +40,22 @@ impl SqliteTokenRepository {

#[async_trait]
impl TokenRepository for SqliteTokenRepository {
async fn provision(&self) -> Result<(), Error> {
query("DROP TABLE IF EXISTS tokens")
.execute(self.pool.as_ref())
.await?;
query(
"CREATE TABLE tokens (
token TEXT PRIMARY KEY,
user INTEGER REFERENCES users(id) NOT NULL,
expires_at INTEGER NOT NULL
)",
)
.execute(self.pool.as_ref())
.await?;
Ok(())
}

async fn insert_or_update_token_for_user(
&self,
token: &str,
Expand Down
19 changes: 19 additions & 0 deletions src/persistence/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use sqlx::{error::ErrorKind, query, query_as, Error, Pool, Sqlite};

#[async_trait]
pub trait UserRepository {
async fn provision(&self) -> Result<(), Error>;
async fn exists(&self, id: i64) -> Result<bool, Error>;
async fn get_name_and_team_id_by_id(&self, id: i64) -> Result<Option<(String, i64)>, Error>;
async fn get_id_by_name_and_team(&self, name: &str, team_id: i64)
Expand Down Expand Up @@ -45,6 +46,24 @@ impl SqliteUserRepository {

#[async_trait]
impl UserRepository for SqliteUserRepository {
async fn provision(&self) -> Result<(), Error> {
query("DROP TABLE IF EXISTS users")
.execute(self.pool.as_ref())
.await?;
query(
"CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
team INTEGER REFERENCES teams(id) NOT NULL,
hash TEXT NOT NULL,
CONSTRAINT name_and_team UNIQUE (name, team)
)",
)
.execute(self.pool.as_ref())
.await?;
Ok(())
}

async fn exists(&self, id: i64) -> Result<bool, Error> {
query_as("SELECT id FROM users WHERE id = ?")
.bind(id)
Expand Down

0 comments on commit 50fd72b

Please sign in to comment.