From 1d3b21a8d4f630904df6f2956f98c4090c4fa110 Mon Sep 17 00:00:00 2001 From: Nuno Rolo Date: Mon, 5 Aug 2024 14:08:40 +0100 Subject: [PATCH] Added sqlite database option --- medicines/build.gradle | 29 ++++--- .../com/nr/medicines/models/DayPeriod.java | 4 +- .../models/dto/DayPeriodRequest.java | 5 +- .../resources/application-postgres.properties | 5 ++ .../resources/application-sqlite.properties | 12 +++ .../src/main/resources/application.properties | 9 +- .../db/changelog/001-day-periods-postgres.sql | 15 ++++ ...ds-data.sql => 001-day-periods-sqlite.sql} | 17 ++-- .../db/changelog/001-initial-schema.sql | 87 ------------------- .../db/changelog/002-initial-schema.sql | 61 +++++++++++++ ...-initial-data.sql => 003-initial-data.sql} | 0 medicines/src/main/resources/db/master.yaml | 43 +++++++-- 12 files changed, 164 insertions(+), 123 deletions(-) create mode 100644 medicines/src/main/resources/application-postgres.properties create mode 100644 medicines/src/main/resources/application-sqlite.properties create mode 100644 medicines/src/main/resources/db/changelog/001-day-periods-postgres.sql rename medicines/src/main/resources/db/changelog/{003-day-periods-data.sql => 001-day-periods-sqlite.sql} (65%) delete mode 100644 medicines/src/main/resources/db/changelog/001-initial-schema.sql create mode 100644 medicines/src/main/resources/db/changelog/002-initial-schema.sql rename medicines/src/main/resources/db/changelog/{002-initial-data.sql => 003-initial-data.sql} (100%) diff --git a/medicines/build.gradle b/medicines/build.gradle index 556911d..39155ef 100644 --- a/medicines/build.gradle +++ b/medicines/build.gradle @@ -24,26 +24,29 @@ repositories { } dependencies { - implementation ('org.springframework.boot:spring-boot-starter-data-jpa') + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.postgresql:postgresql:42.7.3' implementation 'org.mapstruct:mapstruct:1.5.5.Final' implementation 'org.springframework.boot:spring-boot-starter-actuator' - compileOnly 'org.projectlombok:lombok' + implementation 'org.springframework.boot:spring-boot-starter-webflux' + implementation 'io.netty:netty-resolver-dns-native-macos:4.1.72.Final:osx-aarch_64' + implementation 'org.liquibase:liquibase-core:4.25.1' + implementation 'org.jsoup:jsoup:1.17.2' + implementation 'org.xhtmlrenderer:flying-saucer-pdf:9.5.1' + implementation 'com.itextpdf.tool:xmlworker:5.5.13.3' + implementation 'commons-io:commons-io:2.15.1' + implementation 'org.springframework.boot:spring-boot-starter-json:3.3.2' + implementation 'org.springframework.boot:spring-boot-starter-aop' + implementation 'org.xerial:sqlite-jdbc:3.46.0.1' + implementation 'org.hibernate.orm:hibernate-community-dialects:6.4.2.Final' + implementation 'org.hibernate.orm:hibernate-core:6.4.2.Final' annotationProcessor 'org.projectlombok:lombok' annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.5.Final' - implementation group: 'org.springframework.boot', name: 'spring-boot-starter-webflux', version: '3.2.1' - implementation 'io.netty:netty-resolver-dns-native-macos:4.1.72.Final:osx-aarch_64' - implementation group: 'org.liquibase', name: 'liquibase-core', version: '4.25.1' - implementation group: 'org.jsoup', name: 'jsoup', version: '1.17.2' - implementation group: 'org.xhtmlrenderer', name: 'flying-saucer-pdf', version: '9.5.1' - implementation group: 'com.itextpdf.tool', name: 'xmlworker', version: '5.5.13.3' - implementation group: 'commons-io', name: 'commons-io', version: '2.15.1' - implementation group: 'org.springframework.boot', name: 'spring-boot-starter-json', version: '3.2.3' - implementation 'org.springframework.boot:spring-boot-starter-aop:3.2.5' + compileOnly 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' - testImplementation group: 'com.h2database', name: 'h2', version: '2.2.224' - testImplementation group: 'org.mockito', name: 'mockito-junit-jupiter', version: '5.10.0' + testImplementation 'com.h2database:h2:2.3.230' + testImplementation 'org.mockito:mockito-junit-jupiter:5.12.0' } bootJar{ diff --git a/medicines/src/main/java/com/nr/medicines/models/DayPeriod.java b/medicines/src/main/java/com/nr/medicines/models/DayPeriod.java index 058bfcc..c3a518a 100644 --- a/medicines/src/main/java/com/nr/medicines/models/DayPeriod.java +++ b/medicines/src/main/java/com/nr/medicines/models/DayPeriod.java @@ -3,8 +3,6 @@ import jakarta.persistence.*; import lombok.*; -import java.time.LocalTime; - @Entity @Table(name = "day_periods") @AllArgsConstructor @@ -25,5 +23,5 @@ public class DayPeriod { private Integer order; @Column(name = "day_period_hour") - private LocalTime hour; + private String hour; } diff --git a/medicines/src/main/java/com/nr/medicines/models/dto/DayPeriodRequest.java b/medicines/src/main/java/com/nr/medicines/models/dto/DayPeriodRequest.java index af52dd8..f44745d 100644 --- a/medicines/src/main/java/com/nr/medicines/models/dto/DayPeriodRequest.java +++ b/medicines/src/main/java/com/nr/medicines/models/dto/DayPeriodRequest.java @@ -1,10 +1,9 @@ package com.nr.medicines.models.dto; import lombok.Data; -import java.time.LocalTime; @Data public class DayPeriodRequest { private String name; - private LocalTime hour; -} + private String hour; +} \ No newline at end of file diff --git a/medicines/src/main/resources/application-postgres.properties b/medicines/src/main/resources/application-postgres.properties new file mode 100644 index 0000000..d745cf1 --- /dev/null +++ b/medicines/src/main/resources/application-postgres.properties @@ -0,0 +1,5 @@ +spring.datasource.url=${DATASOURCE_URL} +spring.datasource.username=${DATASOURCE_USERNAME} +spring.datasource.password=${DATASOURCE_PWD} +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.open-in-view=false \ No newline at end of file diff --git a/medicines/src/main/resources/application-sqlite.properties b/medicines/src/main/resources/application-sqlite.properties new file mode 100644 index 0000000..be0c129 --- /dev/null +++ b/medicines/src/main/resources/application-sqlite.properties @@ -0,0 +1,12 @@ +spring.application.name=sqlite +spring.datasource.url=jdbc:sqlite:medicines.db +spring.datasource.driver-class-name=org.sqlite.JDBC +spring.jpa.show-sql=false +spring.jpa.database-platform=org.hibernate.community.dialect.SQLiteDialect +hibernate.dialect=org.hibernate.community.dialect.SQLiteDialect +spring.jpa.properties.hibernate.dialect=org.hibernate.community.dialect.SQLiteDialect +spring.jpa.open-in-view=false + +liquibase.shouldRun=true +liquibase.enable-logging=false +liquibase.autoCommit=true \ No newline at end of file diff --git a/medicines/src/main/resources/application.properties b/medicines/src/main/resources/application.properties index 858b7a9..74cec19 100644 --- a/medicines/src/main/resources/application.properties +++ b/medicines/src/main/resources/application.properties @@ -1,9 +1,6 @@ -# Database -spring.datasource.url=${DATASOURCE_URL} -spring.datasource.username=${DATASOURCE_USERNAME} -spring.datasource.password=${DATASOURCE_PWD} -spring.jpa.properties.hibernate.format_sql=true -spring.jpa.open-in-view=false +spring.profiles.default=postgres +spring.liquibase.contexts=${spring.profiles.active} + #spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect #spring.jpa.show-sql=true diff --git a/medicines/src/main/resources/db/changelog/001-day-periods-postgres.sql b/medicines/src/main/resources/db/changelog/001-day-periods-postgres.sql new file mode 100644 index 0000000..aca75fe --- /dev/null +++ b/medicines/src/main/resources/db/changelog/001-day-periods-postgres.sql @@ -0,0 +1,15 @@ +CREATE TABLE day_periods ( + id SERIAL PRIMARY KEY, + description character varying, + display_order integer, + day_period_hour varchar(8) +); + +INSERT INTO day_periods (description, display_order, day_period_hour) VALUES + ('BEFORE BREAKFAST', 1, '08:00'), + ('BREAKFAST', 2, '09:00'), + ('LUNCH', 3, '13:00'), + ('DINNER', 4, '20:00'), + ('NIGHT', 5, '22:00'); + +SELECT SETVAL('day_periods_id_seq', (SELECT MAX(id) FROM day_periods)); \ No newline at end of file diff --git a/medicines/src/main/resources/db/changelog/003-day-periods-data.sql b/medicines/src/main/resources/db/changelog/001-day-periods-sqlite.sql similarity index 65% rename from medicines/src/main/resources/db/changelog/003-day-periods-data.sql rename to medicines/src/main/resources/db/changelog/001-day-periods-sqlite.sql index fb2a72f..e4b5357 100644 --- a/medicines/src/main/resources/db/changelog/003-day-periods-data.sql +++ b/medicines/src/main/resources/db/changelog/001-day-periods-sqlite.sql @@ -1,6 +1,13 @@ +CREATE TABLE day_periods ( + id INTEGER PRIMARY KEY, + description character varying, + display_order integer, + day_period_hour varchar(8) +); + INSERT INTO day_periods (description, display_order, day_period_hour) VALUES - ('BEFORE BREAKFAST', 1, '08:00:00'), - ('BREAKFAST', 2, '09:00:00'), - ('LUNCH', 3, '13:00:00'), - ('DINNER', 4, '20:00:00'), - ('NIGHT', 5, '22:00:00'); + ('BEFORE BREAKFAST', 1, '08:00'), + ('BREAKFAST', 2, '09:00'), + ('LUNCH', 3, '13:00'), + ('DINNER', 4, '20:00'), + ('NIGHT', 5, '22:00'); diff --git a/medicines/src/main/resources/db/changelog/001-initial-schema.sql b/medicines/src/main/resources/db/changelog/001-initial-schema.sql deleted file mode 100644 index 0ab7565..0000000 --- a/medicines/src/main/resources/db/changelog/001-initial-schema.sql +++ /dev/null @@ -1,87 +0,0 @@ -CREATE TABLE medicines ( - barcode integer NOT NULL, - "name" varchar NULL, - dosage varchar NULL, - generic bool NULL, - international_common_name varchar NULL, - form varchar NULL, - packaging_size varchar NULL -); - -CREATE TABLE stock ( - uuid uuid NOT NULL, - medicine_barcode integer NOT NULL, - validity_end_date timestamp without time zone, - created timestamp without time zone, - person_uuid uuid NOT NULL -); - -CREATE TABLE stock_notifications ( - uuid uuid NOT NULL, - stock_uuid uuid, - title character varying, - body character varying, - created timestamp without time zone -); - -CREATE TABLE day_periods ( - id integer NOT NULL, - description character varying, - display_order integer, - day_period_hour time without time zone -); - - -CREATE SEQUENCE day_periods_id_seq - AS integer - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - -CREATE TABLE medicine_image ( - medicine_barcode integer, - image text, - created timestamp without time zone, - medicine_image_uuid uuid -); - -CREATE TABLE persons ( - person_uuid uuid, - name character varying -); - -CREATE TABLE regular_medication ( - uuid uuid NOT NULL, - medicine_barcode integer, - day_period_id integer, - created timestamp without time zone, - observations text, - person_uuid uuid NOT NULL -); - -CREATE TABLE temporary_medication ( - uuid uuid, - medication_uuid uuid, - days integer, - start_date timestamp without time zone -); - -ALTER TABLE ONLY day_periods ALTER COLUMN id SET DEFAULT nextval('day_periods_id_seq'::regclass); -ALTER TABLE ONLY stock_notifications ADD CONSTRAINT stock_notifications_pk PRIMARY KEY (uuid); -ALTER TABLE ONLY medicines ADD CONSTRAINT medicamentos_pk PRIMARY KEY (barcode); -ALTER TABLE ONLY medicine_image ADD CONSTRAINT medicine_image_unique UNIQUE (medicine_image_uuid); -ALTER TABLE ONLY day_periods ADD CONSTRAINT newtable_pk PRIMARY KEY (id); -ALTER TABLE ONLY persons ADD CONSTRAINT persons_unique UNIQUE (person_uuid); -ALTER TABLE ONLY regular_medication ADD CONSTRAINT regular_medication_pk PRIMARY KEY (uuid); -ALTER TABLE ONLY stock ADD CONSTRAINT stock_pk PRIMARY KEY (uuid); -ALTER TABLE ONLY temporary_medication ADD CONSTRAINT temporary_medication_unique UNIQUE (uuid); -ALTER TABLE ONLY stock_notifications ADD CONSTRAINT stock_notifications_fk FOREIGN KEY (stock_uuid) REFERENCES stock(uuid); -ALTER TABLE ONLY medicine_image ADD CONSTRAINT medicine_image_medicines_fk FOREIGN KEY (medicine_barcode) REFERENCES medicines(barcode); -ALTER TABLE ONLY regular_medication ADD CONSTRAINT regular_medication_fk FOREIGN KEY (day_period_id) REFERENCES day_periods(id); -ALTER TABLE ONLY regular_medication ADD CONSTRAINT regular_medication_persons_fk FOREIGN KEY (person_uuid) REFERENCES persons(person_uuid); -ALTER TABLE ONLY stock ADD CONSTRAINT stock_fk FOREIGN KEY (medicine_barcode) REFERENCES medicines(barcode); -ALTER TABLE ONLY stock ADD CONSTRAINT stock_persons_fk FOREIGN KEY (person_uuid) REFERENCES persons(person_uuid); -ALTER TABLE ONLY temporary_medication ADD CONSTRAINT temporary_medication_regular_medication_fk FOREIGN KEY (medication_uuid) REFERENCES regular_medication(uuid); diff --git a/medicines/src/main/resources/db/changelog/002-initial-schema.sql b/medicines/src/main/resources/db/changelog/002-initial-schema.sql new file mode 100644 index 0000000..229def3 --- /dev/null +++ b/medicines/src/main/resources/db/changelog/002-initial-schema.sql @@ -0,0 +1,61 @@ +CREATE TABLE medicines ( + barcode integer NOT NULL PRIMARY KEY, + "name" varchar NULL, + dosage varchar NULL, + generic bool NULL, + international_common_name varchar NULL, + form varchar NULL, + packaging_size varchar NULL +); + +CREATE TABLE persons ( + person_uuid uuid PRIMARY KEY, + name character varying +); + +CREATE TABLE stock ( + uuid uuid NOT NULL PRIMARY KEY, + medicine_barcode integer NOT NULL, + validity_end_date timestamp without time zone, + created timestamp without time zone, + person_uuid uuid NOT NULL, + FOREIGN KEY (medicine_barcode) REFERENCES medicines(barcode), + FOREIGN KEY (person_uuid) REFERENCES persons(person_uuid) +); + +CREATE TABLE stock_notifications ( + uuid uuid NOT NULL PRIMARY KEY, + stock_uuid uuid, + title character varying, + body character varying, + created timestamp without time zone, + FOREIGN KEY (stock_uuid) REFERENCES stock(uuid) +); + +CREATE TABLE medicine_image ( + medicine_image_uuid uuid PRIMARY KEY, + medicine_barcode integer, + image text, + created timestamp without time zone, + FOREIGN KEY (medicine_barcode) REFERENCES medicines(barcode) +); + +CREATE TABLE regular_medication ( + uuid uuid NOT NULL PRIMARY KEY, + medicine_barcode integer, + day_period_id integer, + created timestamp without time zone, + observations text, + person_uuid uuid NOT NULL, + FOREIGN KEY (day_period_id) REFERENCES day_periods(id), + FOREIGN KEY (person_uuid) REFERENCES persons(person_uuid) +); + +CREATE TABLE temporary_medication ( + uuid uuid PRIMARY KEY, + medication_uuid uuid, + days integer, + start_date timestamp without time zone, + FOREIGN KEY (medication_uuid) REFERENCES regular_medication(uuid) +); + diff --git a/medicines/src/main/resources/db/changelog/002-initial-data.sql b/medicines/src/main/resources/db/changelog/003-initial-data.sql similarity index 100% rename from medicines/src/main/resources/db/changelog/002-initial-data.sql rename to medicines/src/main/resources/db/changelog/003-initial-data.sql diff --git a/medicines/src/main/resources/db/master.yaml b/medicines/src/main/resources/db/master.yaml index f6234eb..b305f84 100644 --- a/medicines/src/main/resources/db/master.yaml +++ b/medicines/src/main/resources/db/master.yaml @@ -1,7 +1,38 @@ databaseChangeLog: - - include: - file: classpath:/db/changelog/001-initial-schema.sql - - include: - file: classpath:/db/changelog/002-initial-data.sql - - include: - file: classpath:/db/changelog/003-day-periods-data.sql + - changeSet: + id: 1 + author: admin + context: postgres + changes: + - sqlFile: + path: classpath:/db/changelog/001-day-periods-postgres.sql + endDelimiter: ";" + splitStatements: true + + - changeSet: + id: 2 + author: admin + context: sqlite + changes: + - sqlFile: + path: classpath:/db/changelog/001-day-periods-sqlite.sql + endDelimiter: ";" + splitStatements: true + + - changeSet: + id: 3 + author: admin + changes: + - sqlFile: + path: classpath:/db/changelog/002-initial-schema.sql + endDelimiter: ";" + splitStatements: true + + - changeSet: + id: 4 + author: admin + changes: + - sqlFile: + path: classpath:/db/changelog/003-initial-data.sql + endDelimiter: ";" + splitStatements: true