Skip to content

Commit

Permalink
Handles container restarts for already initialized postgresql database
Browse files Browse the repository at this point in the history
  • Loading branch information
davosian committed Dec 11, 2024
1 parent 1b45065 commit fd0ba8f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 51 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.samples</groupId>
<artifactId>spring-petclinic-rest</artifactId>
<version>3.2.1</version>
<version>3.3.0</version>

<description>REST version of the Spring Petclinic sample application</description>
<url>https://spring-petclinic.github.io/</url>
Expand Down
73 changes: 23 additions & 50 deletions src/main/resources/db/postgres/schema.sql
Original file line number Diff line number Diff line change
@@ -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)
);

0 comments on commit fd0ba8f

Please sign in to comment.