From e64dbae1c5d9546d1842576044a9718b90bea995 Mon Sep 17 00:00:00 2001 From: Daniel Czosnek Date: Tue, 17 Oct 2023 20:33:30 +0200 Subject: [PATCH] Add migrations --- migrations/V12_recreate_table_target.sql | 14 ++++++++++++++ migrations/V13_add_column_created_by.sql | 9 +++++++++ migrations/V14__add_periodicity_column.sql | 20 ++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 migrations/V12_recreate_table_target.sql create mode 100644 migrations/V13_add_column_created_by.sql create mode 100644 migrations/V14__add_periodicity_column.sql diff --git a/migrations/V12_recreate_table_target.sql b/migrations/V12_recreate_table_target.sql new file mode 100644 index 0000000..6e71812 --- /dev/null +++ b/migrations/V12_recreate_table_target.sql @@ -0,0 +1,14 @@ +DROP TABLE IF EXISTS public.target; + +CREATE TABLE target ( + target_id int8 generated by default as identity, + circle_id int8 not null, + kpi_id int8 not null, + target_value numeric not null, + created_at timestamp default current_timestamp not null, + updated_at timestamp, + PRIMARY KEY (target_id), + CONSTRAINT fk_kpi FOREIGN KEY(kpi_id) REFERENCES kpi_definition(kpi_id), + CONSTRAINT fk_circle FOREIGN KEY(circle_id) REFERENCES circle(circle_id), + CONSTRAINT unique_circle_kpi unique (circle_id, kpi_id) +); \ No newline at end of file diff --git a/migrations/V13_add_column_created_by.sql b/migrations/V13_add_column_created_by.sql new file mode 100644 index 0000000..744ad10 --- /dev/null +++ b/migrations/V13_add_column_created_by.sql @@ -0,0 +1,9 @@ +ALTER TABLE + kpi_definition +ADD + COLUMN IF NOT EXISTS created_by uuid; + +ALTER TABLE + kpi_definition +ADD + CONSTRAINT IF NOT EXISTS fk_auth_user FOREIGN KEY (created_by) REFERENCES auth.users (id); \ No newline at end of file diff --git a/migrations/V14__add_periodicity_column.sql b/migrations/V14__add_periodicity_column.sql new file mode 100644 index 0000000..4009cdb --- /dev/null +++ b/migrations/V14__add_periodicity_column.sql @@ -0,0 +1,20 @@ +ALTER TABLE + kpi_values_history +ADD + COLUMN IF NOT EXISTS periodicity periodicity; + +UPDATE + kpi_values_history kvh +SET + periodicity = k.periodicity +FROM + kpi_definition k +WHERE + kvh.kpi_id = k.kpi_id; + +ALTER TABLE + kpi_values_history +ALTER COLUMN + periodicity +SET + NOT NULL; \ No newline at end of file