From fd0ba8f214a41368b938355a19d82ec66646532d Mon Sep 17 00:00:00 2001 From: Dennis Staiger <5349+davosian@users.noreply.github.com> Date: Wed, 11 Dec 2024 09:14:32 +0100 Subject: [PATCH] Handles container restarts for already initialized postgresql database --- pom.xml | 2 +- src/main/resources/db/postgres/schema.sql | 73 +++++++---------------- 2 files changed, 24 insertions(+), 51 deletions(-) diff --git a/pom.xml b/pom.xml index 138695756..f8719b4b3 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.samples spring-petclinic-rest - 3.2.1 + 3.3.0 REST version of the Spring Petclinic sample application https://spring-petclinic.github.io/ diff --git a/src/main/resources/db/postgres/schema.sql b/src/main/resources/db/postgres/schema.sql index 49f997b76..9caa06a4b 100644 --- a/src/main/resources/db/postgres/schema.sql +++ b/src/main/resources/db/postgres/schema.sql @@ -1,102 +1,75 @@ +-- Create tables only if they don't exist CREATE TABLE IF NOT EXISTS vets ( - id SERIAL, + id SERIAL PRIMARY KEY, first_name VARCHAR(30), - last_name VARCHAR(30), - CONSTRAINT pk_vets PRIMARY KEY (id) + last_name VARCHAR(30) ); CREATE INDEX IF NOT EXISTS idx_vets_last_name ON vets (last_name); -ALTER SEQUENCE vets_id_seq RESTART WITH 100; - - CREATE TABLE IF NOT EXISTS specialties ( - id SERIAL, - name VARCHAR(80), - CONSTRAINT pk_specialties PRIMARY KEY (id) + id SERIAL PRIMARY KEY, + name VARCHAR(80) ); CREATE INDEX IF NOT EXISTS idx_specialties_name ON specialties (name); -ALTER SEQUENCE specialties_id_seq RESTART WITH 100; - - CREATE TABLE IF NOT EXISTS vet_specialties ( vet_id INT NOT NULL, specialty_id INT NOT NULL, FOREIGN KEY (vet_id) REFERENCES vets(id), FOREIGN KEY (specialty_id) REFERENCES specialties(id), - CONSTRAINT unique_ids UNIQUE (vet_id,specialty_id) + UNIQUE (vet_id, specialty_id) ); - - CREATE TABLE IF NOT EXISTS types ( - id SERIAL, - name VARCHAR(80), - CONSTRAINT pk_types PRIMARY KEY (id) + id SERIAL PRIMARY KEY, + name VARCHAR(80) ); CREATE INDEX IF NOT EXISTS idx_types_name ON types (name); -ALTER SEQUENCE types_id_seq RESTART WITH 100; - CREATE TABLE IF NOT EXISTS owners ( - id SERIAL, + id SERIAL PRIMARY KEY, first_name VARCHAR(30), last_name VARCHAR(30), address VARCHAR(255), city VARCHAR(80), - telephone VARCHAR(20), - CONSTRAINT pk_owners PRIMARY KEY (id) + telephone VARCHAR(20) ); CREATE INDEX IF NOT EXISTS idx_owners_last_name ON owners (last_name); -ALTER SEQUENCE owners_id_seq RESTART WITH 100; - - CREATE TABLE IF NOT EXISTS pets ( - id SERIAL, + id SERIAL PRIMARY KEY, name VARCHAR(30), birth_date DATE, type_id INT NOT NULL, owner_id INT NOT NULL, FOREIGN KEY (owner_id) REFERENCES owners(id), - FOREIGN KEY (type_id) REFERENCES types(id), - CONSTRAINT pk_pets PRIMARY KEY (id) + FOREIGN KEY (type_id) REFERENCES types(id) ); CREATE INDEX IF NOT EXISTS idx_pets_name ON pets (name); -ALTER SEQUENCE pets_id_seq RESTART WITH 100; - - CREATE TABLE IF NOT EXISTS visits ( - id SERIAL, + id SERIAL PRIMARY KEY, pet_id INT NOT NULL, visit_date DATE, description VARCHAR(255), - FOREIGN KEY (pet_id) REFERENCES pets(id), - CONSTRAINT pk_visits PRIMARY KEY (id) + FOREIGN KEY (pet_id) REFERENCES pets(id) ); -ALTER SEQUENCE visits_id_seq RESTART WITH 100; - CREATE TABLE IF NOT EXISTS users ( - username VARCHAR(20) NOT NULL , - password VARCHAR(20) NOT NULL , - enabled boolean NOT NULL DEFAULT true , - CONSTRAINT pk_users PRIMARY KEY (username) + username VARCHAR(20) NOT NULL PRIMARY KEY, + password VARCHAR(20) NOT NULL, + enabled BOOLEAN NOT NULL DEFAULT true ); CREATE TABLE IF NOT EXISTS roles ( - id SERIAL, - username varchar(20) NOT NULL, - role varchar(20) NOT NULL, - CONSTRAINT pk_roles PRIMARY KEY (id), - FOREIGN KEY (username) REFERENCES users (username) -); - -ALTER TABLE roles ADD CONSTRAINT uni_username_role UNIQUE (role,username); -ALTER SEQUENCE roles_id_seq RESTART WITH 100; + id SERIAL PRIMARY KEY, + username VARCHAR(20) NOT NULL, + role VARCHAR(20) NOT NULL, + FOREIGN KEY (username) REFERENCES users (username), + UNIQUE (role, username) +); \ No newline at end of file