Skip to content

Commit

Permalink
Merge pull request #48 from renegade-fi/joey/auth-server-docker
Browse files Browse the repository at this point in the history
auth-server: Initialze DB migrations and api keys table
  • Loading branch information
joeykraut authored Oct 21, 2024
2 parents f848776 + 5857bac commit f0d353c
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions auth-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ reqwest = { version = "0.11", features = ["json", "stream"] }
tokio = { version = "1", features = ["full"] }
warp = "0.3"

# === Database === #
diesel = { version = "2", features = ["postgres"] }

# === Misc Dependencies === #
bytes = "1.0"
futures-util = "0.3"
Expand Down
9 changes: 9 additions & 0 deletions auth-server/diesel.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId", "Clone"]

[migrations_directory]
dir = "./migrations"
Empty file added auth-server/migrations/.keep
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.

DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
DROP FUNCTION IF EXISTS diesel_set_updated_at();
36 changes: 36 additions & 0 deletions auth-server/migrations/00000000000000_diesel_initial_setup/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- This file was automatically created by Diesel to setup helper functions
-- and other internal bookkeeping. This file is safe to edit, any future
-- changes will be added to existing projects as new migrations.




-- Sets up a trigger for the given table to automatically set a column called
-- `updated_at` whenever the row is modified (unless `updated_at` was included
-- in the modified columns)
--
-- # Example
--
-- ```sql
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
--
-- SELECT diesel_manage_updated_at('users');
-- ```
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
BEGIN
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
BEGIN
IF (
NEW IS DISTINCT FROM OLD AND
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
) THEN
NEW.updated_at := current_timestamp;
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- Drop the api_keys table
DROP TABLE IF EXISTS api_keys;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Create the api_keys table
CREATE TABLE api_keys (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
encrypted_key VARCHAR NOT NULL UNIQUE,
username VARCHAR NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
is_active BOOLEAN NOT NULL DEFAULT TRUE
);

3 changes: 3 additions & 0 deletions auth-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#![deny(clippy::needless_pass_by_ref_mut)]
#![feature(trivial_bounds)]

#[allow(missing_docs, clippy::missing_docs_in_private_items)]
pub(crate) mod schema;

use bytes::Bytes;
use clap::Parser;
use reqwest::{Client, Method, StatusCode};
Expand Down
11 changes: 11 additions & 0 deletions auth-server/src/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @generated automatically by Diesel CLI.

diesel::table! {
api_keys (id) {
id -> Uuid,
encrypted_key -> Varchar,
username -> Varchar,
created_at -> Timestamp,
is_active -> Bool,
}
}

0 comments on commit f0d353c

Please sign in to comment.