init
This commit is contained in:
0
dollhouse/crates/dollhouse-db/migrations/.diesel_lock
Executable file
0
dollhouse/crates/dollhouse-db/migrations/.diesel_lock
Executable file
0
dollhouse/crates/dollhouse-db/migrations/.keep
Executable file
0
dollhouse/crates/dollhouse-db/migrations/.keep
Executable 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();
|
||||
@@ -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;
|
||||
@@ -0,0 +1,11 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
|
||||
DROP TABLE IF EXISTS corps_replicants;
|
||||
DROP TABLE IF EXISTS replicants_stats;
|
||||
DROP TABLE IF EXISTS replicants;
|
||||
DROP TABLE IF EXISTS users;
|
||||
DROP TABLE IF EXISTS corps;
|
||||
|
||||
DROP TYPE IF EXISTS replicant_gender;
|
||||
DROP TYPE IF EXISTS replicant_status;
|
||||
DROP TYPE IF EXISTS user_role;
|
||||
@@ -0,0 +1,44 @@
|
||||
-- Your SQL goes here
|
||||
CREATE TYPE user_role AS ENUM ('corp_admin', 'user');
|
||||
CREATE TYPE replicant_status AS ENUM ('active', 'decommissioned');
|
||||
CREATE TYPE replicant_gender AS ENUM ('male', 'female', 'non-binary');
|
||||
|
||||
CREATE TABLE IF NOT EXISTS corps (
|
||||
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
|
||||
name VARCHAR(255) NOT NULL UNIQUE,
|
||||
description TEXT NOT NULL,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
invite_code VARCHAR(255) NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
|
||||
username VARCHAR(255) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
role user_role NOT NULL DEFAULT 'user',
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
corp_id UUID REFERENCES corps(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS replicants (
|
||||
id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid(),
|
||||
name VARCHAR(255) NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
status replicant_status NOT NULL DEFAULT 'active',
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
gender replicant_gender NOT NULL,
|
||||
corp_id UUID REFERENCES corps(id) NOT NULL,
|
||||
is_private BOOLEAN NOT NULL DEFAULT TRUE,
|
||||
firmware_file VARCHAR(255)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS replicants_stats (
|
||||
replicant_id UUID PRIMARY KEY NOT NULL DEFAULT gen_random_uuid() REFERENCES replicants(id),
|
||||
health INTEGER NOT NULL DEFAULT 100,
|
||||
strength INTEGER NOT NULL DEFAULT 100,
|
||||
intelligence INTEGER NOT NULL DEFAULT 100,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
CREATE INDEX idx_replicant_stats_replicant_id ON replicants_stats(replicant_id);
|
||||
CREATE INDEX idx_replicants_status ON replicants(status);
|
||||
@@ -0,0 +1,20 @@
|
||||
ALTER TABLE replicants_stats
|
||||
DROP CONSTRAINT IF EXISTS replicants_stats_replicant_id_fkey;
|
||||
|
||||
ALTER TABLE replicants_stats
|
||||
ADD CONSTRAINT replicants_stats_replicant_id_fkey
|
||||
FOREIGN KEY (replicant_id) REFERENCES replicants(id);
|
||||
|
||||
ALTER TABLE replicants
|
||||
DROP CONSTRAINT IF EXISTS replicants_corp_id_fkey;
|
||||
|
||||
ALTER TABLE replicants
|
||||
ADD CONSTRAINT replicants_corp_id_fkey
|
||||
FOREIGN KEY (corp_id) REFERENCES corps(id);
|
||||
|
||||
ALTER TABLE users
|
||||
DROP CONSTRAINT IF EXISTS users_corp_id_fkey;
|
||||
|
||||
ALTER TABLE users
|
||||
ADD CONSTRAINT users_corp_id_fkey
|
||||
FOREIGN KEY (corp_id) REFERENCES corps(id);
|
||||
@@ -0,0 +1,20 @@
|
||||
ALTER TABLE users
|
||||
DROP CONSTRAINT IF EXISTS users_corp_id_fkey;
|
||||
|
||||
ALTER TABLE users
|
||||
ADD CONSTRAINT users_corp_id_fkey
|
||||
FOREIGN KEY (corp_id) REFERENCES corps(id) ON DELETE CASCADE;
|
||||
|
||||
ALTER TABLE replicants
|
||||
DROP CONSTRAINT IF EXISTS replicants_corp_id_fkey;
|
||||
|
||||
ALTER TABLE replicants
|
||||
ADD CONSTRAINT replicants_corp_id_fkey
|
||||
FOREIGN KEY (corp_id) REFERENCES corps(id) ON DELETE CASCADE;
|
||||
|
||||
ALTER TABLE replicants_stats
|
||||
DROP CONSTRAINT IF EXISTS replicants_stats_replicant_id_fkey;
|
||||
|
||||
ALTER TABLE replicants_stats
|
||||
ADD CONSTRAINT replicants_stats_replicant_id_fkey
|
||||
FOREIGN KEY (replicant_id) REFERENCES replicants(id) ON DELETE CASCADE;
|
||||
Reference in New Issue
Block a user