From e5f1e7c026e0001eef21daf1174cdbfb5c2b02eb Mon Sep 17 00:00:00 2001 From: klararapo <91880262+klararapo@users.noreply.github.com> Date: Sat, 11 Jun 2022 21:22:25 +0200 Subject: [PATCH 01/10] Added composite PK to intermediate tables and fixed tables names at static queries --- src/db-scripts/static_tables.sql | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/db-scripts/static_tables.sql b/src/db-scripts/static_tables.sql index 2e0b933..82b36d1 100644 --- a/src/db-scripts/static_tables.sql +++ b/src/db-scripts/static_tables.sql @@ -1,22 +1,22 @@ -INSERT INTO contracts_status (name,description) +INSERT INTO contract_statuses (name,description) VALUES ('Not Validated', 'When a contract is created but the meter values are not read yet and the contracted has not started.'), ('Active', 'Meters are read and the contract has started'), ('Expired', 'End date reached and not renewed' ); -INSERT INTO customers_types (description) +INSERT INTO customer_types (description) VALUES ('Private'), ('Company'); -INSERT INTO invoices_types (name,description) +INSERT INTO invoice_types (name,description) VALUES ('Advance Payment', 'Invoicing each month a fixed amount based on the estimated consumption'), ('Debit Note','End of year invoice to be paid by customer'), ('Credit Note', "End of year invoice in customer's favour"); -INSERT INTO plannings_statuses (description) +INSERT INTO planning_statuses (description) VALUES ('Scheduled'), ('Done'); @@ -33,7 +33,7 @@ VALUES ('hr_manager', 'hr_manager'), ('admin', 'admin'); -INSERT INTO statuses_for_invoices (description) +INSERT INTO invoice_statuses (description) VALUES ('Due'), ('Late'), From 9fc487470d437c7ee3592d89d6cc301e754b87b9 Mon Sep 17 00:00:00 2001 From: klararapo <91880262+klararapo@users.noreply.github.com> Date: Sat, 11 Jun 2022 21:37:28 +0200 Subject: [PATCH 02/10] PKs in intermediate tables --- src/db-scripts/init.sql | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/db-scripts/init.sql b/src/db-scripts/init.sql index 5a207a5..ff70c19 100644 --- a/src/db-scripts/init.sql +++ b/src/db-scripts/init.sql @@ -24,9 +24,10 @@ CREATE TABLE IF NOT EXISTS roles ( description VARCHAR(250) ); -CREATE TABLE IF NOT EXISTS users_roles ( +CREATE TABLE IF NOT EXISTS users_roles ( role_id INT NOT NULL, user_id INT NOT NULL, + PRIMARY KEY(role_id, user_id), CONSTRAINT fk_role FOREIGN KEY(role_id) REFERENCES roles(id), CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ); @@ -43,17 +44,18 @@ CREATE TABLE IF NOT EXISTS addresses ( house_number VARCHAR(50) NOT NULL, city_id INT NOT NULL, country VARCHAR(50), - CONSTRAINT fk_city FOREIGN KEY(city_id) REFERENCES cities_postalcodes(id) + CONSTRAINT fk_city FOREIGN KEY(city_id) REFERENCES cities(id) ); CREATE TABLE IF NOT EXISTS users_addresses ( user_id INT NOT NULL, address_id INT NOT NULL, + PRIMARY KEY(user_id, address_id), CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id), CONSTRAINT fk_address FOREIGN KEY(address_id) REFERENCES addresses(id) ); -CREATE TABLE IF NOT EXISTS customers_types ( +CREATE TABLE IF NOT EXISTS customer_types ( id SERIAL PRIMARY KEY, description VARCHAR(50) NOT NULL ); @@ -62,7 +64,7 @@ CREATE TABLE IF NOT EXISTS customers ( user_id INT NOT NULL, type_id INT NOT NULL, CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id), - CONSTRAINT fk_type FOREIGN KEY(type_id) REFERENCES customers_types(id) + CONSTRAINT fk_type FOREIGN KEY(type_id) REFERENCES customer_types(id) ); CREATE TABLE IF NOT EXISTS employees ( @@ -100,10 +102,10 @@ CREATE TABLE IF NOT EXISTS tariffs ( customer_type_id INT NOT NULL, service_type VARCHAR(50) NOT NULL, value REAL NOT NULL, - CONSTRAINT fk_customer_type FOREIGN KEY(customer_type_id) REFERENCES customers_types(id) + CONSTRAINT fk_customer_type FOREIGN KEY(customer_type_id) REFERENCES customer_types(id) ); -CREATE TABLE IF NOT EXISTS contracts_status ( +CREATE TABLE IF NOT EXISTS contract_statuses ( id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL , description VARCHAR(250) NOT NULL @@ -119,17 +121,19 @@ CREATE TABLE IF NOT EXISTS contracts ( status_id INT NOT NULL, CONSTRAINT fk_tariff FOREIGN KEY(tariff_id) REFERENCES tariffs(id), CONSTRAINT fk_estimation FOREIGN KEY(estimation_id) REFERENCES estimations(id), - CONSTRAINT fk_address FOREIGN KEY(address_id) REFERENCES addresses(id) + CONSTRAINT fk_address FOREIGN KEY(address_id) REFERENCES addresses(id), + CONSTRAINT fk_status FOREIGN KEY(status_id) REFERENCES contract_statuses(id) ); CREATE TABLE IF NOT EXISTS customers_contracts ( user_id INT NOT NULL, contract_id INT NOT NULL, + PRIMARY KEY(user_id, contract_id), CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id), CONSTRAINT fk_contract FOREIGN KEY(contract_id) REFERENCES contracts(id) ); -CREATE TABLE IF NOT EXISTS plannings_statuses ( +CREATE TABLE IF NOT EXISTS planning_statuses ( id SERIAL PRIMARY KEY, description VARCHAR(50) NOT NULL ); @@ -140,7 +144,7 @@ CREATE TABLE IF NOT EXISTS plannings ( date DATE NOT NULL, status_id INT NOT NULL, CONSTRAINT fk_contract FOREIGN KEY(contract_id) REFERENCES contracts(id), - CONSTRAINT fk_status FOREIGN KEY(status_id) REFERENCES plannings_statuses(id) + CONSTRAINT fk_status FOREIGN KEY(status_id) REFERENCES planning_statuses(id) ); CREATE TABLE IF NOT EXISTS meters ( @@ -149,7 +153,7 @@ CREATE TABLE IF NOT EXISTS meters ( physical_id INT NOT NULL ); -CREATE TABLE IF NOT EXISTS indexes_values ( +CREATE TABLE IF NOT EXISTS indexed_values ( id SERIAL PRIMARY KEY, meter_id INT NOT NULL, index_value INT NOT NULL, @@ -168,16 +172,17 @@ CREATE TABLE IF NOT EXISTS consumptions ( CREATE TABLE IF NOT EXISTS contracts_meters ( contract_id INT NOT NULL, meter_id INT NOT NULL, + PRIMARY KEY(contract_id, meter_id), CONSTRAINT fk_contract FOREIGN KEY(contract_id) REFERENCES contracts(id), CONSTRAINT fk_meter FOREIGN KEY(meter_id) REFERENCES meters(id) ); -CREATE TABLE IF NOT EXISTS statuses_for_invoices ( +CREATE TABLE IF NOT EXISTS invoice_statuses ( id SERIAL PRIMARY KEY, description VARCHAR(50) NOT NULL ); -CREATE TABLE IF NOT EXISTS invoices_types ( +CREATE TABLE IF NOT EXISTS invoice_types ( id SERIAL PRIMARY KEY, name VARCHAR(50) NOT NULL, description VARCHAR(250) NOT NULL @@ -197,13 +202,14 @@ CREATE TABLE IF NOT EXISTS invoices ( period_end DATE NOT NULL, CONSTRAINT fk_contract FOREIGN KEY(contract_id) REFERENCES contracts(id), CONSTRAINT fk_supplier FOREIGN KEY(supplier_id) REFERENCES users(id), - CONSTRAINT fk_type FOREIGN KEY(type_id) REFERENCES invoices_types(id), + CONSTRAINT fk_type FOREIGN KEY(type_id) REFERENCES invoice_types(id), CONSTRAINT fk_tariff FOREIGN KEY(tariff_id) REFERENCES tariffs(id) ); CREATE TABLE IF NOT EXISTS invoices_statuses ( invoice_id INT NOT NULL, status_id INT NOT NULL, + PRIMARY KEY(invoice_id, status_id), CONSTRAINT fk_invoice FOREIGN KEY(invoice_id) REFERENCES invoices(id), - CONSTRAINT fk_status FOREIGN KEY(status_id) REFERENCES statuses_for_invoices(id) + CONSTRAINT fk_status FOREIGN KEY(status_id) REFERENCES invoice_statuses(id) ); \ No newline at end of file From d5a881b8d5ecb6829163b22c387edd64e76947b8 Mon Sep 17 00:00:00 2001 From: klararapo <91880262+klararapo@users.noreply.github.com> Date: Sat, 11 Jun 2022 22:15:19 +0200 Subject: [PATCH 03/10] small change in an insert query --- src/db-scripts/static_tables.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db-scripts/static_tables.sql b/src/db-scripts/static_tables.sql index 82b36d1..e772a78 100644 --- a/src/db-scripts/static_tables.sql +++ b/src/db-scripts/static_tables.sql @@ -14,7 +14,7 @@ INSERT INTO invoice_types (name,description) VALUES ('Advance Payment', 'Invoicing each month a fixed amount based on the estimated consumption'), ('Debit Note','End of year invoice to be paid by customer'), - ('Credit Note', "End of year invoice in customer's favour"); + ('Credit Note', 'End of year invoice in customer''s favour'); INSERT INTO planning_statuses (description) VALUES @@ -39,7 +39,7 @@ VALUES ('Late'), ('Paid'); -INSERT INTO tariffs(customer_type, service_type, value) +INSERT INTO tariffs(customer_type_id, service_type, value) VALUES (1,'Electricity',0.291), (2,'Electricity',0.106), From 30645c82b731bf2727215d3fa3651bf643580e10 Mon Sep 17 00:00:00 2001 From: klararapo <91880262+klararapo@users.noreply.github.com> Date: Sun, 12 Jun 2022 00:02:21 +0200 Subject: [PATCH 04/10] equipments as array of ints --- src/db-scripts/init.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db-scripts/init.sql b/src/db-scripts/init.sql index ff70c19..b2285ab 100644 --- a/src/db-scripts/init.sql +++ b/src/db-scripts/init.sql @@ -89,7 +89,7 @@ CREATE TABLE IF NOT EXISTS estimations ( building_type SMALLINT NOT NULL, address_id INT NOT NULL, family_size SMALLINT NOT NULL, - equipments VARCHAR(50) NOT NULL, + equipments INT[] NOT NULL, past_consumption REAL NOT NULL, estimated_consumption REAL NOT NULL, CONSTRAINT fk_address FOREIGN KEY(address_id) REFERENCES addresses(id) From 413014de1f9a3a6521fc3ee164a55ecb1ae577e2 Mon Sep 17 00:00:00 2001 From: DrunkenToast Date: Sun, 12 Jun 2022 10:57:09 +0200 Subject: [PATCH 05/10] Service type INT --- src/db-scripts/init.sql | 2 +- src/db-scripts/static_tables.sql | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/db-scripts/init.sql b/src/db-scripts/init.sql index ff70c19..42480b9 100644 --- a/src/db-scripts/init.sql +++ b/src/db-scripts/init.sql @@ -100,7 +100,7 @@ CREATE TABLE IF NOT EXISTS estimations ( CREATE TABLE IF NOT EXISTS tariffs ( id SERIAL PRIMARY KEY, customer_type_id INT NOT NULL, - service_type VARCHAR(50) NOT NULL, + service_type INT NOT NULL, value REAL NOT NULL, CONSTRAINT fk_customer_type FOREIGN KEY(customer_type_id) REFERENCES customer_types(id) ); diff --git a/src/db-scripts/static_tables.sql b/src/db-scripts/static_tables.sql index 82b36d1..089d055 100644 --- a/src/db-scripts/static_tables.sql +++ b/src/db-scripts/static_tables.sql @@ -41,7 +41,8 @@ VALUES INSERT INTO tariffs(customer_type, service_type, value) VALUES - (1,'Electricity',0.291), - (2,'Electricity',0.106), - (1,'Gas',0.068), - (2,'Gas',0.046); + (1,1,0.291), + (2,1,0.106), + (1,2,0.068), + (2,2,0.046); +-- Electricity = 1, Gas = 2 \ No newline at end of file From 96c336914c62e5dc83c5483af0c119e4cdca06df Mon Sep 17 00:00:00 2001 From: DrunkenToast Date: Sun, 12 Jun 2022 12:45:39 +0200 Subject: [PATCH 06/10] Allow NULL: A New Beginning --- src/db-scripts/init.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/db-scripts/init.sql b/src/db-scripts/init.sql index 6f42a84..34a4c70 100644 --- a/src/db-scripts/init.sql +++ b/src/db-scripts/init.sql @@ -113,8 +113,8 @@ CREATE TABLE IF NOT EXISTS contract_statuses ( CREATE TABLE IF NOT EXISTS contracts ( id SERIAL PRIMARY KEY, - start_date DATE NOT NULL, - end_date DATE NOT NULL, + start_date DATE, + end_date DATE, tariff_id INT NOT NULL, estimation_id INT NOT NULL, address_id INT NOT NULL, From 8d3e35cc1e63a0c5155f9144210ccbd5e75cee99 Mon Sep 17 00:00:00 2001 From: Endri Date: Sun, 12 Jun 2022 13:07:56 +0200 Subject: [PATCH 07/10] Rmoved constraint from physical_id --- src/db-scripts/init.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/db-scripts/init.sql b/src/db-scripts/init.sql index 5a207a5..681aa63 100644 --- a/src/db-scripts/init.sql +++ b/src/db-scripts/init.sql @@ -111,8 +111,8 @@ CREATE TABLE IF NOT EXISTS contracts_status ( CREATE TABLE IF NOT EXISTS contracts ( id SERIAL PRIMARY KEY, - start_date DATE NOT NULL, - end_date DATE NOT NULL, + start_date DATE, + end_date DATE, tariff_id INT NOT NULL, estimation_id INT NOT NULL, address_id INT NOT NULL, @@ -145,8 +145,8 @@ CREATE TABLE IF NOT EXISTS plannings ( CREATE TABLE IF NOT EXISTS meters ( id SERIAL PRIMARY KEY, - meter_type VARCHAR(50) NOT NULL, - physical_id INT NOT NULL + meter_type VARCHAR(50), + physical_id INT ); CREATE TABLE IF NOT EXISTS indexes_values ( From c7654426ee52313f55e4b865bd565c635bf12ba5 Mon Sep 17 00:00:00 2001 From: Endri Date: Sun, 12 Jun 2022 13:07:58 +0200 Subject: [PATCH 08/10] Rmoved constraint from physical_id --- src/db-scripts/init.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/db-scripts/init.sql b/src/db-scripts/init.sql index 681aa63..6b0d95a 100644 --- a/src/db-scripts/init.sql +++ b/src/db-scripts/init.sql @@ -145,7 +145,7 @@ CREATE TABLE IF NOT EXISTS plannings ( CREATE TABLE IF NOT EXISTS meters ( id SERIAL PRIMARY KEY, - meter_type VARCHAR(50), + meter_type VARCHAR(50) NOT NULL, physical_id INT ); From ac4674af3d63e93efcfc2ccae77d7087058ff77b Mon Sep 17 00:00:00 2001 From: Endri Date: Sun, 12 Jun 2022 13:50:59 +0200 Subject: [PATCH 09/10] Removed address_id from estimations, added composed PK and removed constraints --- src/db-scripts/init.sql | 17 ++++++++--------- src/db-scripts/static_tables.sql | 8 ++++---- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/db-scripts/init.sql b/src/db-scripts/init.sql index c31792d..2dcdb83 100644 --- a/src/db-scripts/init.sql +++ b/src/db-scripts/init.sql @@ -20,7 +20,7 @@ CREATE TABLE IF NOT EXISTS refreshtokens ( CREATE TABLE IF NOT EXISTS roles ( id SERIAL PRIMARY KEY, - name VARCHAR(50), + name VARCHAR(50) NOT NULL, description VARCHAR(250) ); @@ -43,7 +43,7 @@ CREATE TABLE IF NOT EXISTS addresses ( street VARCHAR(50) NOT NULL, house_number VARCHAR(50) NOT NULL, city_id INT NOT NULL, - country VARCHAR(50), + country VARCHAR(50) NOT NULL, CONSTRAINT fk_city FOREIGN KEY(city_id) REFERENCES cities(id) ); @@ -63,6 +63,7 @@ CREATE TABLE IF NOT EXISTS customer_types ( CREATE TABLE IF NOT EXISTS customers ( user_id INT NOT NULL, type_id INT NOT NULL, + PRIMARY KEY(user_id, type_id), CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id), CONSTRAINT fk_type FOREIGN KEY(type_id) REFERENCES customer_types(id) ); @@ -75,24 +76,22 @@ CREATE TABLE IF NOT EXISTS employees ( ); CREATE TABLE IF NOT EXISTS suppliers ( - user_id INT NOT NULL, - contact_name VARCHAR(50), + id SERIAL PRIMARY KEY, + address_id INT NOT NULL, company_name VARCHAR(50) NOT NULL, - supply_type VARCHAR(50) NOT NULL, + service_type VARCHAR(50) NOT NULL, vat_number VARCHAR(50) NOT NULL, - CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) + CONSTRAINT fk_address FOREIGN KEY(address_id) REFERENCES addresses(id) ); CREATE TABLE IF NOT EXISTS estimations ( id SERIAL PRIMARY KEY, service_type INT NOT NULL, building_type SMALLINT NOT NULL, - address_id INT NOT NULL, family_size SMALLINT NOT NULL, equipments INT[] NOT NULL, past_consumption REAL NOT NULL, - estimated_consumption REAL NOT NULL, - CONSTRAINT fk_address FOREIGN KEY(address_id) REFERENCES addresses(id) + estimated_consumption REAL NOT NULL ); /*https://www.globalpetrolprices.com/Belgium/electricity_prices/#hl183*/ diff --git a/src/db-scripts/static_tables.sql b/src/db-scripts/static_tables.sql index 730931b..b0a9feb 100644 --- a/src/db-scripts/static_tables.sql +++ b/src/db-scripts/static_tables.sql @@ -41,8 +41,8 @@ VALUES INSERT INTO tariffs(customer_type_id, service_type, value) VALUES - (1,1,0.291), - (2,1,0.106), - (1,2,0.068), - (2,2,0.046); + (1,0,0.291), + (2,0,0.106), + (1,1,0.068), + (2,1,0.046); -- Electricity = 1, Gas = 2 \ No newline at end of file From 9f258cf19d5e5c9a8a6ef1abd2337d1c2fe3ddbf Mon Sep 17 00:00:00 2001 From: DrunkenToast Date: Sun, 12 Jun 2022 14:16:21 +0200 Subject: [PATCH 10/10] revert tariffs id --- src/db-scripts/static_tables.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/db-scripts/static_tables.sql b/src/db-scripts/static_tables.sql index b0a9feb..730931b 100644 --- a/src/db-scripts/static_tables.sql +++ b/src/db-scripts/static_tables.sql @@ -41,8 +41,8 @@ VALUES INSERT INTO tariffs(customer_type_id, service_type, value) VALUES - (1,0,0.291), - (2,0,0.106), - (1,1,0.068), - (2,1,0.046); + (1,1,0.291), + (2,1,0.106), + (1,2,0.068), + (2,2,0.046); -- Electricity = 1, Gas = 2 \ No newline at end of file