Работа с ERD-диаграмой

This commit is contained in:
2025-06-01 21:38:33 +04:00
parent b96bd2a224
commit 75eba984b5
5 changed files with 3601 additions and 637 deletions
-637
View File
@@ -1,637 +0,0 @@
-- Common Enums
DROP TYPE IF EXISTS "enum_size";
CREATE TYPE "enum_size" AS ENUM (
'med',
'lanuages',
'system',
'tiny'
);
-- Entity
CREATE SEQUENCE "entity_sequence";
DROP TYPE IF EXISTS "entity_type";
CREATE TYPE "entity_type" AS ENUM (
'action',
'ancestry',
'background',
'backpack',
'campaign_feature',
'class',
'condition',
'deity',
'feat',
'heritage',
'spell'
);
DROP TYPE IF EXISTS "entity_rarity";
CREATE TYPE "entity_rarity" AS ENUM (
'common',
'uncommon',
'rare',
'unique'
);
CREATE TABLE "entity" (
"id" int DEFAULT nextval('entity_sequence') PRIMARY KEY,
"name" varchar(150) NOT NULL,
"img" varchar(150) NOT NULL,
"description" text NOT NULL,
"description_gm" text,
"type" entity_type NOT NULL,
"slug" varchar(150) NOT NULL,
"rarity" entity_rarity NOT NULL DEFAULT 'common',
"is_translated" bool DEFAULT false,
"is_checked" bool DEFAULT false
);
-- Alias
DROP TYPE IF EXISTS "alias_type";
CREATE TYPE "alias_type" AS ENUM (
'old',
'english',
'hobby'
);
CREATE TABLE "entity_aliases" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"entity_id" int NOT NULL,
"alias" varchar NOT NULL,
"type" alias_type NOT NULL
);
ALTER TABLE "entity_aliases" ADD FOREIGN KEY ("entity_id") REFERENCES "entity" ("id");
-- Keywords
CREATE TABLE "keyword" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"slug" varchar(50) NOT NULL
);
CREATE TABLE "entity_keywords" (
"entity_id" int NOT NULL,
"keyword_id" int NOT NULL
);
ALTER TABLE "entity_keywords" ADD FOREIGN KEY ("entity_id") REFERENCES "entity" ("id");
ALTER TABLE "entity_keywords" ADD FOREIGN KEY ("keyword_id") REFERENCES "keyword" ("id");
CREATE TABLE "keyword_aliases" (
"keyword_id" int,
"type" alias_type,
"value" varchar NOT NULL,
PRIMARY KEY ("keyword_id", "type")
);
ALTER TABLE "keyword_aliases" ADD FOREIGN KEY ("keyword_id") REFERENCES "keyword" ("id");
-- Source
CREATE TABLE "source" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" varchar NOT NULL,
"url" varchar NOT NULL
);
CREATE TABLE "entity_sources" (
"entity_id" int NOT NULL,
"source_id" int NOT NULL,
"suffix" varchar NOT NULL DEFAULT '',
"is_remaster" bool NOT NULL
);
ALTER TABLE "entity_sources" ADD FOREIGN KEY ("entity_id") REFERENCES "entity" ("id");
ALTER TABLE "entity_sources" ADD FOREIGN KEY ("source_id") REFERENCES "source" ("id");
-- External
CREATE TABLE "external_target" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" varchar NOT NULL,
"icon" varchar NOT NULL
);
CREATE TABLE "entity_external" (
"entity_id" int,
"target_id" int,
"path" varchar NOT NULL,
"suffix" varchar NOT NULL DEFAULT '',
"md5" varchar NOT NULL,
PRIMARY KEY ("entity_id", "target_id")
);
ALTER TABLE "entity_external" ADD FOREIGN KEY ("entity_id") REFERENCES "entity" ("id");
ALTER TABLE "entity_external" ADD FOREIGN KEY ("target_id") REFERENCES "external_target" ("id");
-- Trait
DROP TYPE IF EXISTS "trait_type";
CREATE TYPE "trait_type" AS ENUM (
'regular',
'tradition'
);
CREATE TABLE "trait" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" varchar NOT NULL,
"slug" varchar NOT NULL,
"description" text NOT NULL,
"type" trait_type NOT NULL
);
CREATE TABLE "entity_traits" (
"entity_id" int,
"trait_id" int,
PRIMARY KEY ("entity_id", "trait_id")
);
ALTER TABLE "entity_traits" ADD FOREIGN KEY ("entity_id") REFERENCES "entity" ("id");
ALTER TABLE "entity_traits" ADD FOREIGN KEY ("trait_id") REFERENCES "trait" ("id");
-- Frequency
CREATE TABLE "frequency" (
"id" int PRIMARY KEY,
"max" int NOT NULL,
"per" int NOT NULL,
"value" int
);
ALTER TABLE "frequency" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
-- Language
CREATE TABLE "language" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"name" varchar NOT NULL,
"rarity" entity_rarity NOT NULL
);
-- Training
CREATE TABLE "trainings" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"key" varchar NOT NULL
);
------------------------------------------------------------------------------------------
-- Spell
DROP TYPE IF EXISTS "spell_area_type";
CREATE TYPE "spell_area_type" AS ENUM (
'burst',
'emanation',
'cone',
'line',
'square',
'cube',
'cylinder'
);
DROP TYPE IF EXISTS "spell_damage_category";
CREATE TYPE "spell_damage_category" AS ENUM (
'persistent',
'splash'
);
DROP TYPE IF EXISTS "spell_damage_kinds";
CREATE TYPE "spell_damage_kinds" AS ENUM (
'damage',
'healing',
'any'
);
CREATE TABLE "spell" (
"id" int PRIMARY KEY,
"level" int NOT NULL,
"cost" int NOT NULL,
"counteraction" bool NOT NULL,
"sustained" bool NOT NULL,
"duration" varchar NOT NULL,
"range" varchar NOT NULL,
"requirements" varchar NOT NULL,
"target" varchar NOT NULL,
"time" varchar NOT NULL,
"can_heightening" bool NOT NULL
);
ALTER TABLE "spell" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
CREATE TABLE "spell_area" (
"spell_id" int PRIMARY KEY,
"details" varchar,
"type" spell_area_type NOT NULL,
"value" int NOT NULL
);
ALTER TABLE "spell_area" ADD FOREIGN KEY ("spell_id") REFERENCES "spell" ("id");
CREATE TABLE "spell_damage" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"spell_id" int NOT NULL,
"key" varchar NOT NULL,
"apply_mod" bool,
"category" spell_damage_category,
"formula" varchar NOT NULL,
"kinds" spell_damage_kinds NOT NULL,
"materials" varchar,
"type" varchar NOT NULL
);
ALTER TABLE "spell_damage" ADD FOREIGN KEY ("spell_id") REFERENCES "spell" ("id");
CREATE TABLE "spell_ritual" (
"spell_id" int PRIMARY KEY,
"primary_check" varchar NOT NULL,
"secondary_casters" int NOT NULL,
"secondary_checks" varchar NOT NULL
);
ALTER TABLE "spell_ritual" ADD FOREIGN KEY ("spell_id") REFERENCES "spell" ("id");
CREATE TABLE "spell_defense" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"spell_id" int NOT NULL,
"is_passive" bool NOT NULL,
"is_basic" bool,
"statistic" varchar NOT NULL
);
ALTER TABLE "spell_defense" ADD FOREIGN KEY ("spell_id") REFERENCES "spell" ("id");
-- Action
DROP TYPE IF EXISTS "action_type";
CREATE TYPE "action_type" AS ENUM (
'action',
'free',
'reaction',
'passive'
);
DROP TYPE IF EXISTS "action_category";
CREATE TYPE "action_category" AS ENUM (
'offensive',
'defensive',
'interaction',
'familiar'
);
DROP TYPE IF EXISTS "ancestry_vision";
CREATE TYPE "ancestry_vision" AS ENUM (
'normal',
'low_light_vision',
'darkvision'
);
CREATE TABLE "action" (
"id" int PRIMARY KEY,
"action_type" action_type NOT NULL,
"actions" int,
"category" action_category,
"death_note" bool,
"extra" text
);
ALTER TABLE "action" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
-- Ancestry
CREATE TABLE "ancestry" (
"id" int PRIMARY KEY,
"hp" int NOT NULL,
"reach" int NOT NULL,
"size" enum_size NOT NULL,
"speed" int NOT NULL,
"vision" ancestry_vision NOT NULL
);
ALTER TABLE "ancestry" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
CREATE TABLE "ancestry_languages" (
"ancestry_id" int,
"language_id" int,
"is_additional" bool NOT NULL,
PRIMARY KEY ("ancestry_id", "language_id")
);
ALTER TABLE "ancestry_languages" ADD FOREIGN KEY ("ancestry_id") REFERENCES "ancestry" ("id");
ALTER TABLE "ancestry_languages" ADD FOREIGN KEY ("language_id") REFERENCES "language" ("id");
CREATE TABLE "ancestry_boosts" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"ancestry_id" int NOT NULL,
"boost" bool NOT NULL DEFAULT true,
"value" varchar(3)
);
ALTER TABLE "ancestry_boosts" ADD FOREIGN KEY ("ancestry_id") REFERENCES "ancestry" ("id");
CREATE TABLE "ancestry_items" (
"ancestry_id" int,
"item_id" int,
"level" int NOT NULL,
PRIMARY KEY ("ancestry_id", "item_id")
);
ALTER TABLE "ancestry_items" ADD FOREIGN KEY ("ancestry_id") REFERENCES "ancestry" ("id");
ALTER TABLE "ancestry_items" ADD FOREIGN KEY ("item_id") REFERENCES "entity" ("id");
-- Background
CREATE TABLE "background" (
"id" int PRIMARY KEY
);
ALTER TABLE "background" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
CREATE TABLE "background_items" (
"background_id" int,
"entity_id" int,
"level" int,
PRIMARY KEY ("background_id", "entity_id")
);
ALTER TABLE "background_items" ADD FOREIGN KEY ("background_id") REFERENCES "background" ("id");
ALTER TABLE "background_items" ADD FOREIGN KEY ("entity_id") REFERENCES "entity" ("id");
CREATE TABLE "background_skills" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"background_id" int NOT NULL,
"name" varchar NOT NULL,
"is_skill" bool NOT NULL
);
ALTER TABLE "background_skills" ADD FOREIGN KEY ("background_id") REFERENCES "background" ("id");
CREATE TABLE "background_boosts" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"background_id" int NOT NULL,
"value" varchar(3)
);
ALTER TABLE "background_boosts" ADD FOREIGN KEY ("background_id") REFERENCES "background" ("id");
-- Campaign Feature
DROP TYPE IF EXISTS "campaign_feature_category";
CREATE TYPE "campaign_feature_category" AS ENUM (
'army_tactic',
'army_war_action',
'kingdom_activity',
'kingdom_feat',
'kingdom_feature'
);
CREATE TABLE "campaign_feature" (
"id" int PRIMARY KEY,
"level" int,
"action_type" action_type NOT NULL,
"actions" int NOT NULL,
"category" campaign_feature_category NOT NULL,
"prerequisites" varchar,
"extra" text
);
ALTER TABLE "campaign_feature" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
-- Class
DROP TYPE IF EXISTS "class_progression_type";
CREATE TYPE "class_progression_type" AS ENUM (
'entity',
'ancestry_feat',
'class_feat',
'general_feat',
'skill_feat',
'skill_increase'
);
CREATE TABLE "class" (
"id" int PRIMARY KEY,
"hp" int NOT NULL,
"perception" int NOT NULL,
"spellcasting" bool NOT NULL,
"additionalTrainedSkills" int NOT NULL
);
ALTER TABLE "class" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
CREATE TABLE "class_key_abilities" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"class_id" int NOT NULL,
"value" varchar(3) NOT NULL
);
ALTER TABLE "class_key_abilities" ADD FOREIGN KEY ("class_id") REFERENCES "class" ("id");
CREATE TABLE "class_progression" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"class_id" int NOT NULL,
"level" int NOT NULL,
"type" class_progression_type NOT NULL,
"entity_id" int
);
ALTER TABLE "class_progression" ADD FOREIGN KEY ("class_id") REFERENCES "class" ("id");
ALTER TABLE "class_progression" ADD FOREIGN KEY ("entity_id") REFERENCES "entity" ("id");
CREATE TABLE "class_trainings" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"class_id" int NOT NULL,
"item" int,
"name" varchar,
"stage" int NOT NULL
);
ALTER TABLE "class_trainings" ADD FOREIGN KEY ("class_id") REFERENCES "class" ("id");
ALTER TABLE "class_trainings" ADD FOREIGN KEY ("item") REFERENCES "trainings" ("id");
-- Condition
DROP TYPE IF EXISTS "condition_group";
CREATE TYPE "condition_group" AS ENUM (
'senses',
'abilities',
'death',
'attitudes',
'detection'
);
CREATE TABLE "condition" (
"id" int PRIMARY KEY,
"group" condition_group,
"extra" text
);
ALTER TABLE "condition" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
-- Deity
DROP TYPE IF EXISTS "deity_category";
CREATE TYPE "deity_category" AS ENUM (
'deity',
'pantheon',
'covenant',
'philosophy'
);
DROP TYPE IF EXISTS "deity_fonts";
CREATE TYPE "deity_fonts" AS ENUM (
'none',
'heal',
'harm',
'any'
);
DROP TYPE IF EXISTS "deity_sanctification";
CREATE TYPE "deity_sanctification" AS ENUM (
'holy',
'unholy',
'any'
);
CREATE TABLE "deity" (
"id" int PRIMARY KEY,
"category" deity_category NOT NULL,
"font" deity_fonts NOT NULL,
"sanctification_must" bool NOT NULL,
"sanctification_type" deity_sanctification NOT NULL
);
ALTER TABLE "deity" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
CREATE TABLE "deity_attributes" (
"deity_id" int,
"attribute" varchar(3),
PRIMARY KEY ("deity_id", "attribute")
);
ALTER TABLE "deity_attributes" ADD FOREIGN KEY ("deity_id") REFERENCES "deity" ("id");
CREATE TABLE "deity_skills" (
"deity_id" int,
"skill_id" int,
PRIMARY KEY ("deity_id", "skill_id")
);
ALTER TABLE "deity_skills" ADD FOREIGN KEY ("deity_id") REFERENCES "deity" ("id");
ALTER TABLE "deity_skills" ADD FOREIGN KEY ("skill_id") REFERENCES "trainings" ("id");
CREATE TABLE "deity_spells" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"deity_id" int NOT NULL,
"level" int NOT NULL,
"spell_id" int NOT NULL
);
ALTER TABLE "deity_spells" ADD FOREIGN KEY ("deity_id") REFERENCES "deity" ("id");
ALTER TABLE "deity_spells" ADD FOREIGN KEY ("spell_id") REFERENCES "spell" ("id");
CREATE TABLE "deity_weapons" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"deity_id" int NOT NULL,
"weapon" varchar NOT NULL
);
ALTER TABLE "deity_weapons" ADD FOREIGN KEY ("deity_id") REFERENCES "deity" ("id");
CREATE TABLE "deity_domains" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"deity_id" int NOT NULL,
"is_primary" bool NOT NULL,
"value" varchar NOT NULL
);
ALTER TABLE "deity_domains" ADD FOREIGN KEY ("deity_id") REFERENCES "deity" ("id");
-- Feat
DROP TYPE IF EXISTS "feat_category";
CREATE TYPE "feat_category" AS ENUM (
'ancestryfeature',
'classfeature',
'calling',
'class',
'skill',
'general',
'ancestry',
'bonus',
'deityboon',
'curse',
'pfsboon'
);
CREATE TABLE "feat" (
"id" int PRIMARY KEY,
"action_type" action_type NOT NULL,
"actions" int,
"category" feat_category NOT NULL,
"level" int NOT NULL,
"level_taken" int,
"max_takable" int,
"location" varchar(12),
"only_level_1" bool,
"extra" text
);
ALTER TABLE "feat" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
CREATE TABLE "feat_prerequisites" (
"id" INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"feat_id" int NOT NULL,
"value" varchar NOT NULL
);
ALTER TABLE "feat_prerequisites" ADD FOREIGN KEY ("feat_id") REFERENCES "feat" ("id");
-- Heritage
CREATE TABLE "heritage" (
"id" int PRIMARY KEY,
"ancestry_id" int
);
ALTER TABLE "heritage" ADD FOREIGN KEY ("id") REFERENCES "entity" ("id");
ALTER TABLE "heritage" ADD FOREIGN KEY ("ancestry_id") REFERENCES "ancestry" ("id");
----------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION f_truncate_tables()
RETURNS void
LANGUAGE plpgsql AS
$func$
BEGIN
EXECUTE
(SELECT 'TRUNCATE TABLE '
|| string_agg(format('%I.%I', schemaname, tablename), ', ')
|| ' CASCADE;'
FROM pg_tables
WHERE schemaname = 'public'
);
END
$func$;
----------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION f_drop_tables()
RETURNS void
LANGUAGE plpgsql AS
$func$
BEGIN
EXECUTE
(SELECT 'DROP TABLE '
|| string_agg(format('%I.%I', schemaname, tablename), ', ')
|| ' CASCADE;'
FROM pg_tables
WHERE schemaname = 'public'
);
END
$func$;
----------------------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION f_permission_tables(_username text)
RETURNS void
LANGUAGE plpgsql AS
$func$
DECLARE
_tbl text;
_sch text;
BEGIN
FOR _sch, _tbl IN
SELECT schemaname, tablename
FROM pg_tables
WHERE schemaname = 'public'
LOOP
EXECUTE format('ALTER TABLE %I.%I OWNER TO %I', _sch, _tbl, _username);
END LOOP;
END
$func$;