From 53277258c9b2c1c4c4c69dfd1e1d37149ef1b5e8 Mon Sep 17 00:00:00 2001 From: Immanuel Onyeka Date: Sun, 23 Oct 2022 16:26:51 -0400 Subject: [PATCH] Fix table setup errors --- migrations/0_29092022_create_main_tables.sql | 84 ++++++++++++-------- migrations/reset.sql | 8 +- 2 files changed, 57 insertions(+), 35 deletions(-) diff --git a/migrations/0_29092022_create_main_tables.sql b/migrations/0_29092022_create_main_tables.sql index a2c5070..aa72e08 100644 --- a/migrations/0_29092022_create_main_tables.sql +++ b/migrations/0_29092022_create_main_tables.sql @@ -1,7 +1,19 @@ /* Precision for all money amounts assumes cents are excluded. */ +CREATE TABLE comparison ( + id INT AUTO_INCREMENT, + PRIMARY KEY (id) +); + +CREATE TABLE branch ( + id INT AUTO_INCREMENT, + type ENUM('NMLS', 'FSRA'), + num VARCHAR(40) NOT NULL, + PRIMARY KEY (id) +); + CREATE TABLE user ( - id INT AUTO_INCREMENT NOT NULL, + id INT AUTO_INCREMENT, email VARCHAR(40) NOT NULL, first_name VARCHAR(30) NOT NULL, last_name VARCHAR(30) NOT NULL, @@ -24,32 +36,20 @@ CREATE TABLE user ( ); CREATE TABLE license ( - id INT AUTO_INCREMENT NOT NULL, + id INT AUTO_INCREMENT, user_id INT NOT NULL, type ENUM('NMLS', 'FSRA'), - number VARCHAR(40) NOT NULL, + num VARCHAR(40) NOT NULL, PRIMARY KEY (`id`), FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE ON UPDATE RESTRICT ); -CREATE TABLE branch ( - id INT AUTO_INCREMENT NOT NULL, - type ENUM('NMLS', 'FSRA'), - num VARCHAR(40) NOT NULL, - PRIMARY KEY (`id`), -); - -CREATE TABLE comparison ( - id INT AUTO_INCREMENT NOT NULL, - PRIMARY KEY (`id`), -); - /* Officers or managers may need to create new loan types with custom settings. * Types with branch_id and user_id values of null will be general cases. */ CREATE TABLE loan_type ( - id INT AUTO_INCREMENT NOT NULL, + id INT AUTO_INCREMENT, branch_id INT, user_id INT, name VARCHAR(30), @@ -58,9 +58,18 @@ CREATE TABLE loan_type ( PRIMARY KEY (`id`) ); +CREATE TABLE borrower ( + id INT AUTO_INCREMENT, + credit_score SMALLINT, + monthly_income INT, + num TINYINT, /* Number of people borrowing. */ + PRIMARY KEY (`id`) +); + CREATE TABLE estimate ( - id INT AUTO_INCREMENT NOT NULL, + id INT AUTO_INCREMENT, user_id INT NOT NULL, + borrower_id INT NOT NULL, comparison_id INT, transaction ENUM('Purchase', 'Refinance'), loan_type_id INT NOT NULL, @@ -82,33 +91,40 @@ CREATE TABLE estimate ( ON UPDATE RESTRICT, FOREIGN KEY (comparison_id) REFERENCES comparison(id) ON DELETE CASCADE - ON UPDATE RESTRICT -); - -CREATE TABLE borrower ( - id INT AUTO_INCREMENT NOT NULL, - estimate_id INT, - credit_score SMALLINT, - monthly_income INT, - num TINYINT, /* Number of people borrowing. */ - PRIMARY KEY (`id`) - FOREIGN KEY (estimate_id) REFERENCES estimate(id) + ON UPDATE RESTRICT, + FOREIGN KEY (borrower_id) REFERENCES borrower(id) ); /* template = true fees are saved for users or branches. If template or default * are true, estimate_id should be null. */ CREATE TABLE fee ( id INT AUTO_INCREMENT NOT NULL, - user_id INT NOT NULL, - user_id INT NOT NULL, estimate_id INT, + amount INT NOT NULL, + type ENUM('Goverment', 'Title', 'Other'), + notes VARCHAR(255), + name VARCHAR(30), + /* Group heading shown in report */ + category VARCHAR(60), + PRIMARY KEY (`id`), + FOREIGN KEY (estimate_id) REFERENCES estimate(id) +); + +/* Templates to be reused by users or branches. Either user_id or branch_id must + * be non-null. */ +CREATE TABLE fee_template ( + id INT AUTO_INCREMENT, + user_id INT, + branch_id INT, + amount INT NOT NULL, type ENUM('Goverment', 'Title', 'Other'), notes VARCHAR(255), name VARCHAR(30), - category VARCHAR(60), /* Group heading shown in report */ - template BOOLEAN, /* If fee should appear reusable through a menu */ - default BOOLEAN, /* If fee should be automatically applied */ + /* Group heading shown in report */ + category VARCHAR(60), + auto BOOLEAN, + /* If fee should be automatically applied */ PRIMARY KEY (`id`), FOREIGN KEY (user_id) REFERENCES user(id), - FOREIGN KEY (estimate_id) REFERENCES estimate(id) ON DELETE CASCADE, + FOREIGN KEY (branch_id) REFERENCES branch(id) ); diff --git a/migrations/reset.sql b/migrations/reset.sql index 1162342..98cb39b 100644 --- a/migrations/reset.sql +++ b/migrations/reset.sql @@ -1,2 +1,8 @@ +DROP TABLE IF EXISTS fee; +DROP TABLE IF EXISTS estimate; +DROP TABLE IF EXISTS borrower; +DROP TABLE IF EXISTS loan_type; DROP TABLE IF EXISTS license; -DROP TABLE IF EXISTS users +DROP TABLE IF EXISTS user; +DROP TABLE IF EXISTS branch; +DROP TABLE IF EXISTS comparison;