From ca567b2e60f7baf5fcc121e3fdf3119349a92cd7 Mon Sep 17 00:00:00 2001 From: neviyn Date: Sun, 9 May 2021 16:00:34 +0100 Subject: [PATCH] Entity primary keys are now forcibly sequence generated --- src/main/kotlin/uk/co/neviyn/booru/Entity.kt | 21 +++++++-- .../db/migration/V1_0__Database_Init.sql | 43 +++++++++++-------- src/test/resources/application.properties | 2 +- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/uk/co/neviyn/booru/Entity.kt b/src/main/kotlin/uk/co/neviyn/booru/Entity.kt index beff2e1..f8a0ff9 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Entity.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Entity.kt @@ -11,6 +11,7 @@ import javax.persistence.JoinColumn import javax.persistence.JoinTable import javax.persistence.ManyToMany import javax.persistence.ManyToOne +import javax.persistence.SequenceGenerator import javax.validation.constraints.Pattern @Entity @@ -30,14 +31,20 @@ open class Member( inverseJoinColumns = [JoinColumn(name = "role_id", referencedColumnName = "id")] ) open var roles: MutableSet = mutableSetOf(), - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) open var id: Long = -1 + @Id + @SequenceGenerator(name="member_id_seq", sequenceName = "member_id_seq", allocationSize = 1) + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "member_id_seq") + open var id: Long = -1 ) @Entity open class Role( @Column(unique = true) open var name: String = "", - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) open var id: Long = -1 + @Id + @SequenceGenerator(name="role_id_seq", sequenceName = "role_id_seq", allocationSize = 1) + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "role_id_seq") + open var id: Long = -1 ) @Entity @@ -54,7 +61,10 @@ open class Image( inverseJoinColumns = [JoinColumn(name = "tag_id", referencedColumnName = "id")] ) open var tags: MutableSet = mutableSetOf(), - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) open var id: Long = -1 + @Id + @SequenceGenerator(name="image_id_seq", sequenceName = "image_id_seq", allocationSize = 1) + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "image_id_seq") + open var id: Long = -1 ) @Entity @@ -62,5 +72,8 @@ open class Tag( @Column(unique = true) @Pattern(regexp = "[a-zA-Z0-9_]*") // Only allow alphanumeric and underscores open var tag: String = "", - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) open var id: Long = -1 + @Id + @SequenceGenerator(name="tag_id_seq", sequenceName = "tag_id_seq", allocationSize = 1) + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tag_id_seq") + open var id: Long = -1 ) \ No newline at end of file diff --git a/src/main/resources/db/migration/V1_0__Database_Init.sql b/src/main/resources/db/migration/V1_0__Database_Init.sql index 0f5bd08..15a82d4 100644 --- a/src/main/resources/db/migration/V1_0__Database_Init.sql +++ b/src/main/resources/db/migration/V1_0__Database_Init.sql @@ -1,34 +1,34 @@ -DROP SCHEMA IF EXISTS booru CASCADE ; +DROP SCHEMA IF EXISTS booru CASCADE; CREATE SCHEMA booru; CREATE TABLE booru.member ( - id bigint NOT NULL PRIMARY KEY, - name varchar NOT NULL UNIQUE, - email varchar NOT NULL, - password varchar NOT NULL, - enabled boolean + id bigserial NOT NULL PRIMARY KEY, + name varchar NOT NULL UNIQUE, + email varchar NOT NULL, + password varchar NOT NULL, + enabled boolean NOT NULL ); CREATE TABLE booru.image ( - id bigint NOT NULL PRIMARY KEY, - filename varchar NOT NULL UNIQUE, - uploader bigint NOT NULL, + id bigserial NOT NULL PRIMARY KEY, + filename varchar NOT NULL UNIQUE, + uploader bigint NOT NULL, CONSTRAINT uploader FOREIGN KEY (uploader) REFERENCES booru.member (id) ); CREATE TABLE booru.role ( - id bigint NOT NULL PRIMARY KEY, - name varchar NOT NULL + id bigserial NOT NULL PRIMARY KEY, + name varchar NOT NULL ); CREATE TABLE booru.tag ( - id bigint NOT NULL PRIMARY KEY, - tag varchar NOT NULL UNIQUE + id bigserial NOT NULL PRIMARY KEY, + tag varchar NOT NULL UNIQUE ); CREATE TABLE booru.tag_image @@ -43,17 +43,22 @@ CREATE TABLE booru.tag_image CREATE TABLE booru.member_roles ( member_id bigint NOT NULL, - role_id bigint NOT NULL, + role_id bigint NOT NULL, CONSTRAINT role FOREIGN KEY (role_id) REFERENCES booru.role (id), CONSTRAINT member FOREIGN KEY (member_id) REFERENCES booru.member (id) ); +CREATE SEQUENCE IF NOT EXISTS booru.member_id_seq START WITH 1 INCREMENT BY 1; +CREATE SEQUENCE IF NOT EXISTS booru.image_id_seq START WITH 1 INCREMENT BY 1; +CREATE SEQUENCE IF NOT EXISTS booru.role_id_seq START WITH 1 INCREMENT BY 1; +CREATE SEQUENCE IF NOT EXISTS booru.tag_id_seq START WITH 1 INCREMENT BY 1; + -- Populate default values -INSERT INTO booru.role (id, name) -VALUES (1, 'ADMIN'), - (2, 'USER'); -INSERT INTO booru.member (id, name, email, password, enabled) -VALUES (1, 'root', 'root@example.org', '$2a$10$XiMxQ1Qe.AprOdJKUPum6O2URy2b4ogffujVZVpUT8CJPksfSyNZK', TRUE); +INSERT INTO booru.role (name) +VALUES ('ADMIN'), + ('USER'); +INSERT INTO booru.member (name, email, password, enabled) +VALUES ('root', 'root@example.org', '$2a$10$XiMxQ1Qe.AprOdJKUPum6O2URy2b4ogffujVZVpUT8CJPksfSyNZK', TRUE); INSERT INTO booru.member_roles (member_id, role_id) VALUES (1, 1), (1, 2); \ No newline at end of file diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 5799647..42ff3eb 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -1,3 +1,3 @@ -spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL spring.datasource.driverClassName=org.h2.Driver images.directory=./images \ No newline at end of file