Skip to content

Commit

Permalink
refactor(db):update schema and fix foreign key references (#159)
Browse files Browse the repository at this point in the history
This pull request includes significant changes to the database schema
and seed data scripts, focusing on refactoring table structures,
updating foreign key relationships, and enhancing the seed data. The
most important changes include the renaming and restructuring of tables,
the addition of new columns, and the removal of certain tables and
columns.

### Database Schema Changes:

* `database/start-scripts/0-init.sql`: 
- Renamed `task_types` to `tasks`, `pruning_types` to `element_types`,
and added new columns such as `description` and `require_tree_type`.
Removed `photo_id` and associated foreign key constraints from multiple
tables.
- Updated the `zones` table structure by adding new columns and
modifying foreign key relationships. Removed the `zones_predefined` and
`routes` tables.
- Added `deleted_at` and `updated_at` timestamps to several tables
including `work_reports`, `work_report_photos`, `sensors`, and
`sensor_history`.
[[1]](diffhunk://#diff-72bf1e9917f27a912c8b0028f642b9f88128bcbff602cc0d9a40dea20d7cfc4fR209)
[[2]](diffhunk://#diff-72bf1e9917f27a912c8b0028f642b9f88128bcbff602cc0d9a40dea20d7cfc4fR218-R219)
[[3]](diffhunk://#diff-72bf1e9917f27a912c8b0028f642b9f88128bcbff602cc0d9a40dea20d7cfc4fR234-R235)
[[4]](diffhunk://#diff-72bf1e9917f27a912c8b0028f642b9f88128bcbff602cc0d9a40dea20d7cfc4fR249-R250)

### Seed Data Changes:

* `database/start-scripts/1-seed.sql`:
- Added seed data for `element_types`, `tasks`, `zones`, and `elements`.
Updated the seed data for `work_orders`, `work_orders_users`, and
`work_reports`.

These changes aim to streamline the database structure, improve data
integrity, and enhance the initial dataset for better testing and
development.
  • Loading branch information
0x1026 authored Dec 3, 2024
2 parents 2100d4c + f9b7207 commit b8878c2
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 132 deletions.
140 changes: 64 additions & 76 deletions database/start-scripts/0-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,36 +57,32 @@ create table tree_types (
family varchar(255) not null,
genus varchar(255) not null,
species varchar(255) unique,
photo_id int,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (photo_id) references photos(id),
constraint UC_TreeType unique (family, genus, species)
);

create table task_types (
create table tasks (
id int auto_increment primary key,
name varchar(255) unique,
photo_id int,
description varchar(255),
require_tree_type boolean default false,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (photo_id) references photos(id)
deleted_at timestamp
);

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

--* Points, zones and routes
--* Points and zones
create table points (
id int auto_increment primary key,
latitude decimal(10, 7) not null,
Expand All @@ -99,123 +95,108 @@ create table points (

create table zones (
id int auto_increment primary key,
point_id int,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (point_id) references points(id)
);

create table zones_predefined (
id int auto_increment primary key,
zone_id int unique,
name varchar(255) not null,
photo_id int,
point_id int,
contract_id int,
city varchar(255),
element_type_id int,
amount int,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (zone_id) references zones(id),
foreign key (photo_id) references photos(id),
constraint UC_ZonePredefined unique (zone_id, name)
);

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)
foreign key (point_id) references points(id),
foreign key (contract_id) references contracts(id),
foreign key (element_type_id) references element_types(id)
);

--* Elements and incidences
create table elements (
id int auto_increment primary key,
name varchar(255) not null,
element_type_id int not null,
contract_id int not null,
zone_id int not null,
-- point_id int unique,
tree_type_id int not null,
point_id int unique,
tree_type_id int null,
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),
foreign key (tree_type_id) references tree_types(id)
foreign key (point_id) references points(id),
foreign key (tree_type_id) references tree_types(id),
foreign key (element_type_id) references element_types(id)
);

create table incidences (
id int auto_increment primary key,
element_id int not null,
name varchar(255) not null,
description varchar(255),
photo_id int,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (element_id) references elements(id)
);

create table incidence_photos (
id int auto_increment primary key,
incidence_id int not null,
photo_id int not null,
created_at timestamp default current_timestamp,
foreign key (incidence_id) references incidences(id),
foreign key (photo_id) references photos(id),
constraint UC_IncidencePhoto unique (incidence_id, photo_id)
foreign key (element_id) references elements(id),
foreign key (photo_id) references photos(id)
);

--* Work orders, tasks and reports
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,
foreign key (contract_id) references contracts(id)
);

create table tasks (
create table work_orders_users (
id int auto_increment primary key,
work_order_id int not null,
task_type_id int not null,
notes varchar(255),
route_id int,
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 (task_type_id) references task_types(id),
foreign key (route_id) references routes(id)
foreign key (user_id) references users(id),
constraint UC_WorkOrderUser unique (work_order_id, user_id)
);

create table tasks_users (
create table work_orders_blocks (
id int auto_increment primary key,
task_id int,
user_id int,
work_order_id int not null,
notes varchar(255),
created_at timestamp default current_timestamp,
foreign key (task_id) references tasks(id),
foreign key (user_id) references users(id)
updated_at timestamp,
deleted_at timestamp,
foreign key (work_order_id) references work_orders(id)
);

create table tasks_zones (
create table work_orders_blocks_zones (
id int auto_increment primary key,
task_id int,
zone_id int,
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)
);

create table work_orders_blocks_tasks (
id int auto_increment primary key,
work_orders_block_id int not null,
task_id int not null,
tree_type_id int,
status int default 0,
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 (zone_id) references zones(id)
foreign key (tree_type_id) references tree_types(id)
);

create table work_reports (
Expand All @@ -225,6 +206,7 @@ create table work_reports (
spent_fuel decimal,
created_at timestamp default current_timestamp,
updated_at timestamp,
deleted_at timestamp,
foreign key (work_order_id) references work_orders(id)
);

Expand All @@ -233,6 +215,8 @@ create table work_report_photos (
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)
Expand All @@ -247,6 +231,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 @@ -260,6 +246,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)
);
Loading

0 comments on commit b8878c2

Please sign in to comment.