Create initial database structure

This commit is contained in:
2025-03-17 19:06:18 +01:00
parent 0614b23d59
commit 64b672dc63
11 changed files with 709 additions and 0 deletions

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,6 @@
DROP TABLE IF EXISTS inbox;
DROP TABLE IF EXISTS movement;
DROP TABLE IF EXISTS account;
DROP TABLE IF EXISTS attachment;
DROP TABLE IF EXISTS token;
DROP TABLE IF EXISTS users;

View File

@ -0,0 +1,70 @@
CREATE TABLE users
(
id SERIAL PRIMARY KEY,
mail VARCHAR(255) NOT NULL,
name VARCHAR(150) NOT NULL,
time_create BIGINT NOT NULL,
time_update BIGINT NOT NULL
);
CREATE TABLE token
(
id SERIAL PRIMARY KEY,
label VARCHAR(150) NOT NULL,
time_create BIGINT NOT NULL,
time_update BIGINT NOT NULL,
user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE,
token VARCHAR(150) NOT NULL,
time_used BIGINT NOT NULL,
max_inactivity INTEGER,
ip_restriction VARCHAR(50),
read_only BOOLEAN NOT NULL DEFAULT true,
right_account BOOLEAN NOT NULL DEFAULT false,
right_movement BOOLEAN NOT NULL DEFAULT false,
right_inbox BOOLEAN NOT NULL DEFAULT false,
right_attachment BOOLEAN NOT NULL DEFAULT false,
right_user BOOLEAN NOT NULL DEFAULT false
);
CREATE TABLE attachment
(
id SERIAL PRIMARY KEY,
time_create BIGINT NOT NULL,
mime_type VARCHAR(150) NOT NULL,
sha512 VARCHAR(130) NOT NULL,
file_size INTEGER NOT NULL,
user_id INTEGER NOT NULL REFERENCES users ON DELETE RESTRICT
);
CREATE TABLE account
(
id SERIAL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE,
time_create BIGINT NOT NULL,
time_update BIGINT NOT NULL,
default_account BOOLEAN NOT NULL DEFAULT false
);
CREATE TABLE movement
(
id SERIAL PRIMARY KEY,
account_id INTEGER NOT NULL REFERENCES account ON DELETE CASCADE,
time BIGINT NOT NULL,
label VARCHAR(200) NOT NULL,
attachment_id INT REFERENCES attachment ON DELETE SET NULL,
amount REAL NOT NULL,
checked BOOLEAN NOT NULL DEFAULT false,
time_create BIGINT NOT NULL,
time_update BIGINT NOT NULL
);
CREATE TABLE inbox
(
id SERIAL PRIMARY KEY,
attachment_id INTEGER NOT NULL REFERENCES attachment ON DELETE CASCADE,
user_id INTEGER NOT NULL REFERENCES users ON DELETE CASCADE,
account_id INTEGER REFERENCES account ON DELETE CASCADE,
time_create BIGINT NOT NULL,
time_update BIGINT NOT NULL
);