Skip to content

Commit

Permalink
refactor(database): update schema by removing unused fields and const…
Browse files Browse the repository at this point in the history
…raints, and adjusting seed data

Co-authored-by: 24Victor <[email protected]>
  • Loading branch information
0x1026 and 24Victor committed Dec 3, 2024
1 parent 504f572 commit 15fda1e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 73 deletions.
82 changes: 38 additions & 44 deletions database/start-scripts/0-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ create table users (
email varchar(255) not null,
role_id int not null,
photo_id int,
status int,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (role_id) references roles(id),
foreign key (photo_id) references photos(id)
);


create table contracts (
id int auto_increment primary key,
name varchar(255) not null,
Expand All @@ -57,17 +55,8 @@ create table machines (
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
constraint UC_MachineType unique (name, max_basket_size),
foreign key (photo_id) references photos(id)
);

create table element_types(
id int auto_increment primary key,
name varchar(255) not null,
description varchar(255),
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp
foreign key (photo_id) references photos(id),
constraint UC_MachineType unique (name, max_basket_size)
);

--* Tree, task and pruning types
Expand All @@ -86,14 +75,22 @@ create table tasks (
id int auto_increment primary key,
name varchar(255) unique,
description varchar(255),
tree_type_id int null,
require_tree_type boolean default false,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (tree_type_id) references tree_types(id)
deleted_at timestamp
);

--* Points, zones and routes
create table element_types (
id int auto_increment primary key,
name varchar(255) not null,
description varchar(255),
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp
);

--* Points and zones
create table points (
id int auto_increment primary key,
latitude decimal(10, 7) not null,
Expand All @@ -120,25 +117,6 @@ create table zones (
foreign key (element_type_id) references element_types(id)
);

create table routes (
id int auto_increment primary key,
distance float,
travel_time int,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp
);

create table route_points (
id int auto_increment primary key,
route_id int not null,
point_id int not null,
point_order int not null,
created_at timestamp default current_timestamp,
foreign key (route_id) references routes(id),
foreign key (point_id) references points(id)
);

--* Elements and incidences
create table elements (
id int auto_increment primary key,
Expand Down Expand Up @@ -174,6 +152,7 @@ create table incidences (
create table work_orders (
id int auto_increment primary key,
contract_id int,
date timestamp,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
Expand All @@ -185,6 +164,8 @@ create table work_orders_users (
work_order_id int not null,
user_id int not null,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (work_order_id) references work_orders(id),
foreign key (user_id) references users(id),
constraint UC_WorkOrderUser unique (work_order_id, user_id)
Expand All @@ -193,6 +174,7 @@ create table work_orders_users (
create table work_orders_blocks (
id int auto_increment primary key,
work_order_id int not null,
notes varchar(255),
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
Expand All @@ -204,6 +186,8 @@ create table work_orders_blocks_zones (
work_orders_block_id int not null,
zone_id int not null,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (work_orders_block_id) references work_orders_blocks(id),
foreign key (zone_id) references zones(id),
constraint UC_WorkOrderBlockZone unique (work_orders_block_id, zone_id)
Expand All @@ -214,31 +198,37 @@ create table work_orders_blocks_tasks (
work_orders_block_id int not null,
task_id int not null,
tree_type_id int,
notes varchar(255),
status int default 0,
route_id int,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (work_orders_block_id) references work_orders_blocks(id),
foreign key (task_id) references tasks(id),
foreign key (tree_type_id) references tree_types(id),
foreign key (route_id) references routes(id)
foreign key (tree_type_id) references tree_types(id)
);


create table work_reports (
id int auto_increment primary key,
work_order_id int unique,
observation varchar(255),
spent_fuel decimal,
photo_id int,
created_at timestamp default current_timestamp,
updated_at timestamp,
foreign key (work_order_id) references work_orders(id),
foreign key (photo_id) references photos(id)
deleted_at timestamp,
foreign key (work_order_id) references work_orders(id)
);

create table work_report_photos (
id int auto_increment primary key,
work_report_id int not null,
photo_id int not null,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (work_report_id) references work_reports(id),
foreign key (photo_id) references photos(id),
constraint UC_WorkReportPhoto unique (work_report_id, photo_id)
);

--* Sensors and sensor history
create table sensors (
Expand All @@ -249,6 +239,8 @@ create table sensors (
model varchar(255),
is_active boolean,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (contract_id) references contracts(id),
foreign key (zone_id) references zones(id),
foreign key (point_id) references points(id),
Expand All @@ -262,6 +254,8 @@ create table sensor_history (
humidity float,
inclination float,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (sensor_id) references sensors(id),
constraint UC_SensorHistory unique (sensor_id, created_at)
);
44 changes: 15 additions & 29 deletions database/start-scripts/1-seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ INSERT INTO roles (name) VALUES
('Trabajador');

--* Users
INSERT INTO users (company, name, surname, dni, password, email, role_id, photo_id, status) VALUES
('Projar', 'Carlos', 'García', '12345678A', 'hashedpassword1', '[email protected]', 1, 1, 1),
('Projar', 'Ana', 'Martínez', '23456789B', 'hashedpassword2', '[email protected]', 2, 2, 1),
('Projar', 'José', 'Rodríguez', '34567890C', 'hashedpassword3', '[email protected]', 3, NULL, 1);
INSERT INTO users (company, name, surname, dni, password, email, role_id, photo_id) VALUES
('Projar', 'Carlos', 'García', '12345678A', 'hashedpassword1', '[email protected]', 1, 1),
('Projar', 'Ana', 'Martínez', '23456789B', 'hashedpassword2', '[email protected]', 2, 2),
('Projar', 'José', 'Rodríguez', '34567890C', 'hashedpassword3', '[email protected]', 3, NULL);

--* Contracts
INSERT INTO contracts (name, start_date, end_date, invoice_proposed, invoice_agreed, invoice_paid) VALUES
Expand All @@ -41,9 +41,9 @@ INSERT INTO tree_types (family, genus, species) VALUES
('Sapindaceae', 'Acer', 'Acer campestre');

--* Tasks
INSERT INTO tasks (name, description, tree_type_id) VALUES
('Podar árboles', 'Tarea de poda general', 1),
('Riego de árboles', 'Riego programado', NULL),
INSERT INTO tasks (name, description, require_tree_type) VALUES
('Podar árboles', 'Tarea de poda general', TRUE),
('Riego de árboles', 'Riego programado', FALSE),
('Fertilización', 'Fertilización básica', NULL);

--* Points
Expand All @@ -58,20 +58,6 @@ INSERT INTO zones (name, point_id, contract_id, city, element_type_id, amount) V
('Zona Sur', 2, 2, 'Barcelona', 2, 30),
('Zona Este', 3, 3, 'Valencia', 3, 20);

--* Routes
INSERT INTO routes (distance, travel_time) VALUES
(1000, 60),
(2000, 120),
(3000, 180);

--* Route Points
INSERT INTO route_points (route_id, point_id, point_order) VALUES
(1, 1, 1),
(1, 2, 2),
(1, 3, 3),
(2, 2, 1),
(2, 3, 2);

--* Elements
INSERT INTO elements (element_type_id, contract_id, zone_id, point_id, tree_type_id) VALUES
(1, 1, 1, 1, 1),
Expand All @@ -97,10 +83,10 @@ INSERT INTO work_orders_users (work_order_id, user_id) VALUES
(3, 3);

--* Work Reports
INSERT INTO work_reports (work_order_id, observation, spent_fuel, photo_id) VALUES
(1, 'Observación de la orden 1', 50.0, NULL),
(2, 'Observación de la orden 2', 60.0, NULL),
(3, 'Observación de la orden 3', 70.0, NULL);
INSERT INTO work_reports (work_order_id, observation, spent_fuel) VALUES
(1, 'Observación de la orden 1', 50.0),
(2, 'Observación de la orden 2', 60.0),
(3, 'Observación de la orden 3', 70.0);

--* Sensors
INSERT INTO sensors (zone_id, contract_id, point_id, model, is_active) VALUES
Expand All @@ -126,7 +112,7 @@ INSERT INTO work_orders_blocks_zones (work_orders_block_id, zone_id) VALUES
(2, 2),
(3, 3);

INSERT INTO work_orders_blocks_tasks (work_orders_block_id, task_id, tree_type_id, notes, status, route_id) VALUES
(1, 1, 1, 'Podar árboles en zona 1', 0, 1),
(2, 2, NULL, 'Riego de árboles en zona 2', 1, 2),
(3, 3, NULL, 'Fertilización en zona 3', 1, 3);
INSERT INTO work_orders_blocks_tasks (work_orders_block_id, task_id, tree_type_id) VALUES
(1, 1, 1),
(2, 2, NULL),
(3, 3, NULL);

0 comments on commit 15fda1e

Please sign in to comment.