commit a9e300ede286e419c0c37f86953c2fdcc61bce6e Author: Lucien Cartier-Tilet Date: Tue Jan 3 15:16:10 2023 +0100 Initial commit with basic DB layout diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..7956ca6 --- /dev/null +++ b/.env.example @@ -0,0 +1,19 @@ +# Database +POSTGRES_HOST=0.0.0.0 +POSTGRES_PASSWORD=changeme +POSTGRES_USER=ordabok +POSTGRES_DB=ordabok +DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}/${POSTGRES_DB} + +# S3 Object Storing +S3_ENDPOINT=https://s3.example.com +S3_REGION= +S3_BUCKET_ID=ordabok +AWS_ACCESS_KEY_ID= +AWS_SECRET_ACCESS_KEY= +AWS_SESSION_TOKEN= + +# Session manager +APPWRITE_ENDPOINT=appwrite.phundrak.com +APPWRITE_PROJECT= +APPWRITE_API_KEY= diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f848e7d --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +/.env +/Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cc42944 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "ordabok" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +diesel = { version = "2.0", features = ["postgres"] } +dotenvy = "0.15" diff --git a/diesel.toml b/diesel.toml new file mode 100644 index 0000000..35a12ff --- /dev/null +++ b/diesel.toml @@ -0,0 +1,8 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" + +[migrations_directory] +dir = "migrations" diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..a8603ba --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,21 @@ +version: '3' + +x-logging: &default-logging + options: + max-size: "100m" + max-file: "5" + driver: json-file + +services: + database: + image: postgres:15-alpine + container_name: ordabok_db + env_file: .env + restart: unless-stopped + volumes: + - ordabok_db_data:/var/lib/postgresql/data + ports: + - 5432:5432 + +volumes: + ordabok_db_data: diff --git a/migrations/.keep b/migrations/.keep new file mode 100644 index 0000000..e69de29 diff --git a/migrations/00000000000000_diesel_initial_setup/down.sql b/migrations/00000000000000_diesel_initial_setup/down.sql new file mode 100644 index 0000000..a9f5260 --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/down.sql @@ -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(); diff --git a/migrations/00000000000000_diesel_initial_setup/up.sql b/migrations/00000000000000_diesel_initial_setup/up.sql new file mode 100644 index 0000000..d68895b --- /dev/null +++ b/migrations/00000000000000_diesel_initial_setup/up.sql @@ -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; diff --git a/migrations/2023-01-03-134423_create_user/down.sql b/migrations/2023-01-03-134423_create_user/down.sql new file mode 100644 index 0000000..6c1ff41 --- /dev/null +++ b/migrations/2023-01-03-134423_create_user/down.sql @@ -0,0 +1,3 @@ +-- This file should undo anything in `up.sql` +DROP TABLE UserFollows; +DROP TABLE Users; diff --git a/migrations/2023-01-03-134423_create_user/up.sql b/migrations/2023-01-03-134423_create_user/up.sql new file mode 100644 index 0000000..d8111b9 --- /dev/null +++ b/migrations/2023-01-03-134423_create_user/up.sql @@ -0,0 +1,20 @@ +-- Your SQL goes here + +CREATE TABLE Users ( + id VARCHAR(31) PRIMARY KEY, -- Appwrite User ID + username VARCHAR(64) NOT NULL +); + +CREATE TABLE UserFollows ( + id SERIAL PRIMARY KEY, + follower VARCHAR(31) + REFERENCES Users(id) + ON UPDATE CASCADE + ON DELETE CASCADE + NOT NULL, + following VARCHAR(31) + REFERENCES Users(id) + ON UPDATE CASCADE + ON DELETE CASCADE + NOT NULL +); diff --git a/migrations/2023-01-03-134426_create_language/down.sql b/migrations/2023-01-03-134426_create_language/down.sql new file mode 100644 index 0000000..135c57d --- /dev/null +++ b/migrations/2023-01-03-134426_create_language/down.sql @@ -0,0 +1,6 @@ +-- This file should undo anything in `up.sql` +DROP TABLE LangAndAgents; +DROP TABLE Languages; +DROP TYPE Release; +DROP TYPE DictGenre; +DROP TYPE AgentLanguageRelation; diff --git a/migrations/2023-01-03-134426_create_language/up.sql b/migrations/2023-01-03-134426_create_language/up.sql new file mode 100644 index 0000000..045dc31 --- /dev/null +++ b/migrations/2023-01-03-134426_create_language/up.sql @@ -0,0 +1,37 @@ +-- Your SQL goes here +CREATE TYPE Release as ENUM ('PUBLIC', 'NONCOMMERCIAL', 'RESEARCH', 'PRIVATE'); +CREATE TYPE DictGenre as ENUM ('gen', 'lrn', 'ety', 'spe', 'his', 'ort', 'trm'); +CREATE TYPE AgentLanguageRelation as ENUM ('publisher', 'author'); + +CREATE TABLE Languages ( + name VARCHAR(255) PRIMARY KEY, + native VARCHAR(255), + relese Release NOT NULL, + targetLanguage TEXT[] NOT NULL, + genre DictGenre[] NOT NULL, + abstract TEXT, + created TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, + description TEXT, + rights TEXT, + license TEXT, + owner VARCHAR(31) + REFERENCES Users(id) + ON UPDATE CASCADE + ON DELETE CASCADE + NOT NULL +); + +CREATE TABLE LangAndAgents ( + id SERIAL PRIMARY KEY, + agent VARCHAR(31) + REFERENCES Users(id) + ON UPDATE CASCADE + ON DELETE CASCADE + NOT NULL, + language VARCHAR(255) + REFERENCES Languages(name) + ON UPDATE CASCADE + ON DELETE CASCADE + NOT NULL, + relationship AgentLanguageRelation NOT NULL +); diff --git a/migrations/2023-01-03-134434_create_word/down.sql b/migrations/2023-01-03-134434_create_word/down.sql new file mode 100644 index 0000000..291a97c --- /dev/null +++ b/migrations/2023-01-03-134434_create_word/down.sql @@ -0,0 +1 @@ +-- This file should undo anything in `up.sql` \ No newline at end of file diff --git a/migrations/2023-01-03-134434_create_word/up.sql b/migrations/2023-01-03-134434_create_word/up.sql new file mode 100644 index 0000000..a8dd2fe --- /dev/null +++ b/migrations/2023-01-03-134434_create_word/up.sql @@ -0,0 +1,40 @@ +-- Your SQL goes here +CREATE TYPE PartOfSpeech as ENUM ('ADJ', 'ADP', 'ADV', 'AUX', 'CCONJ', 'DET', 'INTJ', 'NOUN', 'NUM', 'PART', 'PRON', 'PROPN', 'PUNCT', 'SCONJ', 'SYM', 'VERB', 'X'); +CREATE TYPE WordRelationship as ENUM('def', 'related'); + +CREATE TABLE Words ( + norm VARCHAR(255) PRIMARY KEY, -- normalized word + native VARCHAR(255), + lemma VARCHAR(255) + REFERENCES Words(norm) + ON UPDATE CASCADE + ON DELETE SET NULL, + language VARCHAR(255) + REFERENCES Languages(name) + ON UPDATE CASCADE + ON DELETE CASCADE + NOT NULL, + partofspeech PartOfSpeech NOT NULL, + audio VARCHAR(511), + video VARCHAR(511), + image VARCHAR(511), + description TEXT, -- Markdown + etymology TEXT, -- Markdown + lusage TEXT, -- Markdown + morphology TEXT -- Markdown +); + +CREATE TABLE WordRelation ( + id SERIAL PRIMARY KEY, + wordsource VARCHAR(255) + REFERENCES Words(norm) + ON UPDATE CASCADE + ON DELETE CASCADE + NOT NULL, + wordtarget VARCHAR(255) + REFERENCES Words(norm) + ON UPDATE CASCADE + ON DELETE CASCADE + NOT NULL, + relationship WordRelationship NOT NULL +); diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/schema.rs b/src/schema.rs new file mode 100644 index 0000000..592fb7d --- /dev/null +++ b/src/schema.rs @@ -0,0 +1,117 @@ +// @generated automatically by Diesel CLI. + +pub mod sql_types { + #[derive(diesel::sql_types::SqlType)] + #[diesel(postgres_type(name = "agentlanguagerelation"))] + pub struct Agentlanguagerelation; + + #[derive(diesel::sql_types::SqlType)] + #[diesel(postgres_type(name = "dictgenre"))] + pub struct Dictgenre; + + #[derive(diesel::sql_types::SqlType)] + #[diesel(postgres_type(name = "partofspeech"))] + pub struct Partofspeech; + + #[derive(diesel::sql_types::SqlType)] + #[diesel(postgres_type(name = "release"))] + pub struct Release; + + #[derive(diesel::sql_types::SqlType)] + #[diesel(postgres_type(name = "wordrelationship"))] + pub struct Wordrelationship; +} + +diesel::table! { + use diesel::sql_types::*; + use super::sql_types::Agentlanguagerelation; + + langandagents (id) { + id -> Int4, + agent -> Varchar, + language -> Varchar, + relationship -> Agentlanguagerelation, + } +} + +diesel::table! { + use diesel::sql_types::*; + use super::sql_types::Release; + use super::sql_types::Dictgenre; + + languages (name) { + name -> Varchar, + native -> Nullable, + relese -> Release, + targetlanguage -> Array>, + genre -> Array>, + #[sql_name = "abstract"] + abstract_ -> Nullable, + created -> Nullable, + description -> Nullable, + rights -> Nullable, + license -> Nullable, + owner -> Varchar, + } +} + +diesel::table! { + userfollows (id) { + id -> Int4, + follower -> Varchar, + following -> Varchar, + } +} + +diesel::table! { + users (id) { + id -> Varchar, + username -> Varchar, + } +} + +diesel::table! { + use diesel::sql_types::*; + use super::sql_types::Wordrelationship; + + wordrelation (id) { + id -> Int4, + wordsource -> Varchar, + wordtarget -> Varchar, + relationship -> Wordrelationship, + } +} + +diesel::table! { + use diesel::sql_types::*; + use super::sql_types::Partofspeech; + + words (norm) { + norm -> Varchar, + native -> Nullable, + lemma -> Nullable, + language -> Varchar, + partofspeech -> Partofspeech, + audio -> Nullable, + video -> Nullable, + image -> Nullable, + description -> Nullable, + etymology -> Nullable, + lusage -> Nullable, + morphology -> Nullable, + } +} + +diesel::joinable!(langandagents -> languages (language)); +diesel::joinable!(langandagents -> users (agent)); +diesel::joinable!(languages -> users (owner)); +diesel::joinable!(words -> languages (language)); + +diesel::allow_tables_to_appear_in_same_query!( + langandagents, + languages, + userfollows, + users, + wordrelation, + words, +);