Initial commit with basic DB layout

This commit is contained in:
Lucien Cartier-Tilet 2023-01-03 15:16:10 +01:00
commit a9e300ede2
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
16 changed files with 330 additions and 0 deletions

19
.env.example Normal file
View File

@ -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=

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
/.env
/Cargo.lock

10
Cargo.toml Normal file
View File

@ -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"

8
diesel.toml Normal file
View File

@ -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"

21
docker-compose.yml Normal file
View File

@ -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:

0
migrations/.keep Normal file
View File

View File

@ -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();

View File

@ -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;

View File

@ -0,0 +1,3 @@
-- This file should undo anything in `up.sql`
DROP TABLE UserFollows;
DROP TABLE Users;

View File

@ -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
);

View File

@ -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;

View File

@ -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
);

View File

@ -0,0 +1 @@
-- This file should undo anything in `up.sql`

View File

@ -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
);

3
src/main.rs Normal file
View File

@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

117
src/schema.rs Normal file
View File

@ -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<Varchar>,
relese -> Release,
targetlanguage -> Array<Nullable<Text>>,
genre -> Array<Nullable<Dictgenre>>,
#[sql_name = "abstract"]
abstract_ -> Nullable<Text>,
created -> Nullable<Timestamptz>,
description -> Nullable<Text>,
rights -> Nullable<Text>,
license -> Nullable<Text>,
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<Varchar>,
lemma -> Nullable<Varchar>,
language -> Varchar,
partofspeech -> Partofspeech,
audio -> Nullable<Varchar>,
video -> Nullable<Varchar>,
image -> Nullable<Varchar>,
description -> Nullable<Text>,
etymology -> Nullable<Text>,
lusage -> Nullable<Text>,
morphology -> Nullable<Text>,
}
}
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,
);