fix schema file

This commit is contained in:
Bartosz Wieczorek 2018-01-30 11:50:08 +01:00
parent 3c5b5d3be3
commit e134454482

View File

@ -21,8 +21,8 @@ RETURNS trigger AS $$
BEGIN BEGIN
NEW.last_update = now(); NEW.last_update = now();
RETURN NEW; RETURN NEW;
END; END; $$ -- $$
$$ language 'plpgsql'; language 'plpgsql';
drop table if exists "user" cascade; drop table if exists "user" cascade;
@ -38,6 +38,7 @@ drop table if exists measurands cascade;
drop table if exists privilege cascade; drop table if exists privilege cascade;
drop table if exists pointed_values cascade; drop table if exists pointed_values cascade;
drop table if exists "auth_identity" cascade; drop table if exists "auth_identity" cascade;
drop table if exists "group" cascade;
drop table if exists category_files; drop table if exists category_files;
drop table if exists units_conversions; drop table if exists units_conversions;
@ -80,7 +81,7 @@ declare id integer;
BEGIN BEGIN
SELECT "group"."gid" FROM "group" WHERE "group"."name" = 'nogroup' limit 1 into id; SELECT "group"."gid" FROM "group" WHERE "group"."name" = 'nogroup' limit 1 into id;
return id; return id;
end $$ end $$ -- $$
language 'plpgsql'; language 'plpgsql';
create or replace function default_users_group_id() create or replace function default_users_group_id()
@ -89,7 +90,7 @@ declare id integer;
BEGIN BEGIN
SELECT "group"."gid" FROM "group" WHERE "group"."name" = 'users' limit 1 into id; SELECT "group"."gid" FROM "group" WHERE "group"."name" = 'users' limit 1 into id;
return id; return id;
end $$ end $$ -- $$
language 'plpgsql'; language 'plpgsql';
create or replace function default_admins_group_id() create or replace function default_admins_group_id()
@ -98,13 +99,14 @@ declare id integer;
BEGIN BEGIN
SELECT "group"."gid" FROM "group" WHERE "group"."name" = 'admins' limit 1 into id; SELECT "group"."gid" FROM "group" WHERE "group"."name" = 'admins' limit 1 into id;
return id; return id;
end $$ end $$ -- $$
language 'plpgsql'; language 'plpgsql'; -- $$
create table "user" ( create table "user" (
"uid" serial not null, "uid" serial not null,
"group" int not null default default_users_group_id(), "group" int not null default default_users_group_id(),
"status" integer DEFAULT 0 NOT NULL,
"full_name" text not null, "full_name" text not null,
"created" timestamp DEFAULT now() not null, "created" timestamp DEFAULT now() not null,
"updated" timestamp, "updated" timestamp,
@ -120,6 +122,94 @@ comment on column "user"."created" is '';
comment on column "user"."config" is ''; comment on column "user"."config" is '';
create table "auth_info" (
"id" serial not null,
"user_uid" integer,
"password_hash" varchar(100) not null,
"password_method" varchar(20) not null,
"password_salt" varchar(20) not null,
"email" varchar(256) not null,
"email_verified" boolean not null default false,
"email_token" varchar(64),
"email_token_expires" timestamp,
"email_token_role" integer,
constraint "pk_auth_into_id" primary key("id"),
constraint "fk_auth_info_user_uid" foreign key ("user_uid") references "user" ("uid") on delete cascade deferrable initially deferred,
constraint "ux_auth_info_email" unique ("email")
);
create table "auth_identity" (
"id" serial not null,
"auth_info_id" bigint,
"provider" varchar(64) not null,
"identity" varchar(512) not null,
constraint "pk_auth_identity_id" primary key("id"),
constraint "fk_auth_identity_auth_info" foreign key ("auth_info_id") references "auth_info" ("id") on delete cascade deferrable initially deferred
);
comment on table "auth_identity" is '';
comment on column "auth_identity"."id" is '';
comment on column "auth_identity"."auth_info_id" is '';
comment on column "auth_identity"."provider" is '';
comment on column "auth_identity"."identity" is '';
create table "auth_token" (
"id" serial not null,
"auth_info_id" bigint,
"value" varchar(64) not null,
"expires" timestamp not null,
constraint "pk_auth_token_id" primary key("id"),
constraint "fk_auth_token_auth_info_id" foreign key ("auth_info_id") references "auth_info" ("id") on delete cascade deferrable initially deferred
);
create index "ix_auth_token_value" ON "auth_token" ("value");
comment on table "auth_token" is '';
comment on column "auth_token"."id" is '';
comment on column "auth_token"."auth_info_id" is '';
comment on column "auth_token"."value" is '';
comment on column "auth_token"."expires" is '';
create table "user_audit_action" (
"id" serial not null,
"name" text,
constraint "pk_user_audit_action_id" primary key ("id"),
constraint "ux_user_audit_action_name" unique ("name" )
);
comment on table "user_audit_action" is 'action that user can take (like login/logout/config change?)';
comment on column "user_audit_action"."id" is '';
comment on column "user_audit_action"."name" is 'action name';
insert into "user_audit_action"("name") values ('login'), ('logout');
create table "user_audit" (
"id" serial not null,
"user_id" int not null,
"action_id" int not null,
"data" jsonb,
"when_happened" timestamp DEFAULT(now()),
constraint "pk_user_history_id" primary key ("id"),
constraint "fk_user_history_user_uid" foreign key ("user_id") references "user"("uid") on delete cascade deferrable initially deferred,
constraint "fk_user_history_user_action" foreign key ("action_id") references "user_audit_action"("id") on delete cascade deferrable initially deferred
);
create index "ix_user_history_data" ON "user_audit" ((("data" ->> 'status')::text)) WHERE ("data" ->> 'status') IS not null;
comment on table "user_audit" IS 'saves user actions like login/logout';
comment on column "user_audit"."id" is '';
comment on column "user_audit"."user_id" is '';
comment on column "user_audit"."action_id" is '';
comment on column "user_audit"."data" is 'data column contains information about taken action (if login was successful? if not, from what ip this action was taken?)';
comment on column "user_audit"."when_happened" is '';
create table "user_groups"(
"uid" integer not null,
"gid" integer not null
);
comment on table "user_groups" is '';
comment on column "user_groups"."uid" is '';
comment on column "user_groups"."gid" is '';
create table "stat"( create table "stat"(
"id" serial not null, "id" serial not null,
"owner" int not null default 1, "owner" int not null default 1,
@ -171,7 +261,7 @@ create table "privilege" (
"role" varchar(30) not null, -- TODO change to enum "role" varchar(30) not null, -- TODO change to enum
"who" int not null default 0, "who" int not null default 0,
"action" text not null, "action" text not null,
"type" varchar(30) not null, -- TODO change in future to enum "type" varchar(30) not null, -- TODO change to enum
"related_table_name" varchar(100) not null, "related_table_name" varchar(100) not null,
"related_object_uid" int not null default 0, "related_object_uid" int not null default 0,
constraint "pk_privilege" primary key("role", "who", "action", "type", "related_table_name", "related_object_uid") constraint "pk_privilege" primary key("role", "who", "action", "type", "related_table_name", "related_object_uid")
@ -189,94 +279,6 @@ comment on column "privilege"."related_table_name" is 'holds the name of the ta
comment on column "privilege"."related_object_uid" is 'stores the ID of the object to which the privilege applies, if its an object privilege. This has no meaning for table and global privileges, of course. The one applies to a table, not an object, and the second applies to all rows in a table, so an ID is immaterial. This is also not used for self privileges, because by definition a self privilege has to apply to the user requesting permission to do something.'; comment on column "privilege"."related_object_uid" is 'stores the ID of the object to which the privilege applies, if its an object privilege. This has no meaning for table and global privileges, of course. The one applies to a table, not an object, and the second applies to all rows in a table, so an ID is immaterial. This is also not used for self privileges, because by definition a self privilege has to apply to the user requesting permission to do something.';
create table "auth_info" (
"id" serial not null,
"user_uid" integer,
"password_hash" varchar(100) not null,
"password_method" varchar(20) not null,
"password_salt" varchar(20) not null,
"email" varchar(256) not null,
"email_verified" boolean not null default false,
"email_token" varchar(64),
"email_token_expires" timestamp,
"email_token_role" integer,
constraint "pk_auth_into_id" primary key("id"),
constraint "fk_auth_info_user_uid" foreign key ("user_uid") references "user" ("uid") on delete cascade deferrable initially deferred,
constraint "ux_auth_info_email" unique ("email")
);
create table "auth_identity" (
"id" serial not null,
"auth_info_id" bigint,
"provider" varchar(64) not null,
"identity" varchar(512) not null,
constraint "pk_auth_identity_id" primary key("id"),
constraint "fk_auth_identity_auth_info" foreign key ("auth_info_id") references "auth_info" ("id") on delete cascade deferrable initially deferred
);
comment on table "auth_identity" is '';
comment on column "auth_identity"."id" is '';
comment on column "auth_identity"."auth_info_id" is '';
comment on column "auth_identity"."provider" is '';
comment on column "auth_identity"."identity" is '';
create table "auth_token" (
"id" serial not null,
"auth_info_id" bigint,
"value" varchar(64) not null,
"expires" timestamp not null,
constraint "pk_auth_token_id" primary key("id"),
constraint "fk_auth_token_auth_info_id" foreign key ("auth_info_id") references "auth_info" ("id") on delete cascade deferrable initially deferred
);
create index "ix_auth_token_value" ON "auth_token" ("value");
comment on table "auth_token" is '';
comment on column "auth_token"."id" is '';
comment on column "auth_token"."auth_info_id" is '';
comment on column "auth_token"."value" is '';
comment on column "auth_token"."expires" is '';
create table "user_audit_action" (
"id" serial not null,
"name" text,
constraint "pk_user_audit_action_id" primary key ("id"),
constraint "ux_user_audit_action_name" unique ("name" )
);
comment on table "user_audit_action" is 'action that user can take (like login/logout/config change?)';
comment on column "user_audit_action"."id" is '';
comment on column "user_audit_action"."name" is 'action name';
insert into "user_audit_action"("name") values ('login'), ('logout');
create table "user_audit" (
"id" serial not null,
"user_id" int not null,
"action_id" int not null,
"data" jsonb,
"when_happened" timestamp DEFAULT(now()),
constraint "pk_user_history_id" primary key ("id"),
constraint "fk_user_history_user_uid" foreign key ("user_id") references "user"("uid") on delete cascade deferrable initially deferred,
constraint "fk_user_history_user_action" foreign key ("action_id") references "user_audit_action"("id") on delete cascade deferrable initially deferred
);
create index "ix_user_history_data" ON "user_audit" ((("data" ->> 'status')::text)) WHERE ("data" ->> 'status') IS not null;
comment on table "user_audit" IS 'saves user actions like login/logout';
comment on column "user_audit"."id" is '';
comment on column "user_audit"."user_id" is '';
comment on column "user_audit"."action_id" is '';
comment on column "user_audit"."data" is 'data column contains information about taken action (if login was successful? if not, from what ip this action was taken?)';
comment on column "user_audit"."when_happened" is '';
create table "user_groups"(
"uid" integer not null,
"gid" integer not null
);
comment on table "user_groups" is '';
comment on column "user_groups"."uid" is '';
comment on column "user_groups"."gid" is '';
create or replace function update_category_parent_path() returns trigger as $$ create or replace function update_category_parent_path() returns trigger as $$
DECLARE DECLARE
path ltree; path ltree;
@ -312,7 +314,6 @@ comment on column "category"."description" is '';
comment on column "category"."parent_path" is ''; comment on column "category"."parent_path" is '';
/*
create table "measurands"( create table "measurands"(
"id" serial not null unique primary key, "id" serial not null unique primary key,
"name" text not null unique, "name" text not null unique,
@ -326,58 +327,56 @@ create table "metric_systems"(
"description" text "description" text
); );
comment on table measurands IS 'Information about measured quantity length, time etc.'; --comment on table measurands IS 'Information about measured quantity length, time etc.';
create table units( --create table units(
symbol VARCHAR (20) not null, -- symbol VARCHAR (20) not null,
description TEXT check(length(description) < 100000), -- description TEXT check(length(description) < 100000),
measurand_id int REFERENCES measurands(id), -- measurand_id int REFERENCES measurands(id),
base_unit int REFERENCES units(uid), -- base_unit int REFERENCES units(uid),
metric_system int REFERENCES metric_systems(id), -- metric_system int REFERENCES metric_systems(id),
constraint units_pkey primary key (uid), -- constraint units_pkey primary key (uid),
constraint unitowner_fk foreign key (owner) REFERENCES users (uid) deferrable initially IMMEDIATE, -- constraint unitowner_fk foreign key (owner) REFERENCES users (uid) deferrable initially IMMEDIATE,
constraint units_unique UNIQUE(name, symbol) -- constraint units_unique UNIQUE(name, symbol)
) inherits(stat); --) inherits(stat);
comment on table units IS 'Table holds information about units used in application'; --comment on table units IS 'Table holds information about units used in application';
comment on column units.name IS 'Parameter name e.g. Ampere'; --comment on column units.name IS 'Parameter name e.g. Ampere';
comment on column units.symbol IS 'Parameter symbol e.g. A'; --comment on column units.symbol IS 'Parameter symbol e.g. A';
comment on column units.description IS 'Simple description'; --comment on column units.description IS 'Simple description';
create table units_conversions( --create table units_conversions(
from_unit INTEGER not null REFERENCES units(uid), -- from_unit INTEGER not null REFERENCES units(uid),
to_unit INTEGER not null REFERENCES units(uid), -- to_unit INTEGER not null REFERENCES units(uid),
equation TEXT not null, -- equation TEXT not null,
constraint unit_conversions_unique primary key (from_unit, to_unit) -- constraint unit_conversions_unique primary key (from_unit, to_unit)
); --);
comment on table units_conversions IS 'This table contains a mathematical equation for converting one unitl to other, more info available at http://www.partow.net/programming/exprtk/index.html'; --comment on table units_conversions IS 'This table contains a mathematical equation for converting one unitl to other, more info available at http://www.partow.net/programming/exprtk/index.html';
comment on column units_conversions.equation IS 'this equation should be a proper exprtk equation'; --comment on column units_conversions.equation IS 'this equation should be a proper exprtk equation';
create table pointed_values( --create table pointed_values(
id serial primary key, -- id serial primary key,
data jsonb default('{}') -- data jsonb default('{}')
); --);
create INDEX items_pointed_values_idx on pointed_values USING GIN (data); --create INDEX items_pointed_values_idx on pointed_values USING GIN (data);
create table parameters ( --create table parameters (
symbol VARCHAR(20), -- symbol VARCHAR(20),
unit INTEGER REFERENCES units(uid), -- unit INTEGER REFERENCES units(uid),
description TEXT check(length(description) < 100000), -- description TEXT check(length(description) < 100000),
ptype parameter_type default('stored'), -- ptype parameter_type default('stored'),
constraint parameters_pkey primary key (uid), -- constraint parameters_pkey primary key (uid),
constraint parametereowner_fk foreign key (owner) REFERENCES users (uid) deferrable initially IMMEDIATE, -- constraint parametereowner_fk foreign key (owner) REFERENCES users (uid) deferrable initially IMMEDIATE,
constraint parameters_unique UNIQUE(name, symbol) -- constraint parameters_unique UNIQUE(name, symbol)
) INHERITS (stat); --) INHERITS (stat);
comment on column parameters.name IS 'Parameter name e.g. "Load current max." '; --comment on column parameters.name IS 'Parameter name e.g. "Load current max." ';
comment on column parameters.symbol IS 'Parameter symbol e.g. "I<sub>R</sub>'; --comment on column parameters.symbol IS 'Parameter symbol e.g. "I<sub>R</sub>';
comment on column parameters.unit IS 'Parameter unit e.g. id od Amper unit from unit table'; --comment on column parameters.unit IS 'Parameter unit e.g. id od Amper unit from unit table';
comment on column parameters.ptype IS '''stored'' parameter is a parameter which value is stored directly in items table, ''pointed'' means that the value of parameter is an ID of value stored in parameter_values table'; --comment on column parameters.ptype IS '''stored'' parameter is a parameter which value is stored directly in items table, ''pointed'' means that the value of parameter is an ID of value stored in parameter_values table';
*/ --create table "item_namespaces"(
--);
create table "item_namespaces"(
)
create table "item"( create table "item"(
"category_id" int not null, "category_id" int not null,
@ -419,30 +418,30 @@ create table "user_inventory"(
constraint user_inventory_pk primary key ("inventory_id", "user_id") constraint user_inventory_pk primary key ("inventory_id", "user_id")
); );
/*
create table in_stock(
item_id INTEGER not null REFERENCES items,
inventory_id INTEGER not null REFERENCES inventory,
amount numeric(10,10) not null DEFAULT 0
);
comment on table in_stock IS 'Table contains information about items being available in storage'; --create table in_stock(
-- item_id INTEGER not null REFERENCES items,
-- inventory_id INTEGER not null REFERENCES inventory,
-- amount numeric(10,10) not null DEFAULT 0
--);
create table inventory_operations( --comment on table in_stock IS 'Table contains information about items being available in storage';
constraint inventory_operations_pkey primary key (uid),
constraint OperationOwner_fk foreign key (owner) REFERENCES users (uid) deferrable initially IMMEDIATE,
constraint inventory_operation_unique UNIQUE (name)
) INHERITS(stat);
create table inventory_history( --create table inventory_operations(
inventory_from_id INTEGER not null REFERENCES inventory on DELETE CASCADE, -- constraint inventory_operations_pkey primary key (uid),
inventory_to_id INTEGER not null REFERENCES inventory on DELETE CASCADE, -- constraint OperationOwner_fk foreign key (owner) REFERENCES users (uid) deferrable initially IMMEDIATE,
operation_id INTEGER not null REFERENCES inventory_Operations on DELETE CASCADE , -- constraint inventory_operation_unique UNIQUE (name)
amount NUMERIC(10,10), --) INHERITS(stat);
--create table inventory_history(
-- inventory_from_id INTEGER not null REFERENCES inventory on DELETE CASCADE,
-- inventory_to_id INTEGER not null REFERENCES inventory on DELETE CASCADE,
-- operation_id INTEGER not null REFERENCES inventory_Operations on DELETE CASCADE ,
-- amount NUMERIC(10,10),
-- date timestamp not null default now()
--);
date timestamp not null default now()
);
*/
-- create INDEX users_stat_index on users (uid, owner, group, unixperms, status) with ( FILLFACTOR=100 ); -- create INDEX users_stat_index on users (uid, owner, group, unixperms, status) with ( FILLFACTOR=100 );
-- create INDEX categories_stat_index on categories (uid, owner, group, unixperms, status) with ( FILLFACTOR=100 ); -- create INDEX categories_stat_index on categories (uid, owner, group, unixperms, status) with ( FILLFACTOR=100 );
@ -450,9 +449,6 @@ create table inventory_history(
-- create INDEX files_stat_index on files (uid, owner, group, unixperms, status) with ( FILLFACTOR=100 ); -- create INDEX files_stat_index on files (uid, owner, group, unixperms, status) with ( FILLFACTOR=100 );
-- create INDEX items_stat_index on items (uid, owner, group, unixperms, status) with ( FILLFACTOR=100 ); -- create INDEX items_stat_index on items (uid, owner, group, unixperms, status) with ( FILLFACTOR=100 );
-- create trigger update_parameters_last_update before update on parameters FOR EACH ROW EXECUTE PROCEDURE last_update_column(); -- create trigger update_parameters_last_update before update on parameters FOR EACH ROW EXECUTE PROCEDURE last_update_column();
-- create trigger update_files_last_update before update on files FOR EACH ROW EXECUTE PROCEDURE last_update_column(); -- create trigger update_files_last_update before update on files FOR EACH ROW EXECUTE PROCEDURE last_update_column();
-- create trigger update_packages_last_update before update on packages FOR EACH ROW EXECUTE PROCEDURE last_update_column(); -- create trigger update_packages_last_update before update on packages FOR EACH ROW EXECUTE PROCEDURE last_update_column();