diff --git a/build.gradle b/build.gradle index 711ecd5..ab832f7 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,7 @@ ext { jwtVersion = '0.13.0' passwordEncoderVersion = '6.4.5' swaggerUIVersion = '2.2.0' + flywayVersion = '11.19.0' } java { @@ -74,6 +75,9 @@ dependencies { // * Swagger UI implementation "org.springdoc:springdoc-openapi-starter-webmvc-ui:${swaggerUIVersion}" + + // * Flyway + implementation "org.flywaydb:flyway-mysql:${flywayVersion}" } tasks.named('test') { diff --git a/src/main/java/com/example/bak/user/domain/Profile.java b/src/main/java/com/example/bak/user/domain/Profile.java index a7cd109..45fb230 100644 --- a/src/main/java/com/example/bak/user/domain/Profile.java +++ b/src/main/java/com/example/bak/user/domain/Profile.java @@ -26,6 +26,7 @@ public class Profile { @Column(nullable = false) private String nickname; + @Column(name = "user_id") private Long userId; private Profile(Long id, String name, String nickname) { diff --git a/src/main/java/com/example/bak/user/domain/User.java b/src/main/java/com/example/bak/user/domain/User.java index 608a2b5..78bf3f5 100644 --- a/src/main/java/com/example/bak/user/domain/User.java +++ b/src/main/java/com/example/bak/user/domain/User.java @@ -30,6 +30,7 @@ public class User { @Column(nullable = false) private String password; + @Column(name = "profile_id") private Long profileId; @Enumerated(EnumType.STRING) diff --git a/src/main/resources/application-local.yaml b/src/main/resources/application-local.yaml index d7f2bc8..eeb0acf 100644 --- a/src/main/resources/application-local.yaml +++ b/src/main/resources/application-local.yaml @@ -19,6 +19,13 @@ spring: h2: console: enabled: false + flyway: + enabled: true + locations: + - classpath:db/migration + baseline-on-migrate: true + baseline-version: 0 + clean-disabled: false # (Caution!) Progress in Dev Environment springdoc: swagger-ui: diff --git a/src/main/resources/application-prod.yaml b/src/main/resources/application-prod.yaml index 3f3b6c3..9720681 100644 --- a/src/main/resources/application-prod.yaml +++ b/src/main/resources/application-prod.yaml @@ -11,7 +11,7 @@ spring: jpa: hibernate: - ddl-auto: update + ddl-auto: none properties: hibernate: format_sql: false @@ -19,6 +19,13 @@ spring: dialect: org.hibernate.dialect.MySQL8Dialect open-in-view: false + flyway: + enabled: true + locations: + - classpath:db/migration + baseline-on-migrate: true + baseline-version: 0 + springdoc: swagger-ui: path: /api-test diff --git a/src/main/resources/db/migration/V1__init.sql b/src/main/resources/db/migration/V1__init.sql new file mode 100644 index 0000000..a54282f --- /dev/null +++ b/src/main/resources/db/migration/V1__init.sql @@ -0,0 +1,81 @@ +-- Create companies table +CREATE TABLE companies +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + career_link VARCHAR(500) NOT NULL, + logo_url VARCHAR(500) NOT NULL, + description TEXT NOT NULL +); + +-- Create communities table +CREATE TABLE communities +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + job_group VARCHAR(255) NOT NULL, + company_id BIGINT +); + +-- Create profiles table +CREATE TABLE profiles +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + nickname VARCHAR(255) NOT NULL, + user_id BIGINT +); + +-- Create users table +CREATE TABLE users +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + email VARCHAR(255) NOT NULL UNIQUE, + password VARCHAR(255) NOT NULL, + role VARCHAR(50) DEFAULT 'NORMAL', + profile_id BIGINT NOT NULL +); + +-- Create feeds table +CREATE TABLE feeds +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + title VARCHAR(255) NOT NULL, + content TEXT NOT NULL, + community_id BIGINT NOT NULL, + author_id BIGINT NOT NULL +); + +-- Create comments table +CREATE TABLE comments +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + feed_id BIGINT NOT NULL, + author_id BIGINT NOT NULL, + author_nickname VARCHAR(255) NOT NULL, + content VARCHAR(1000) NOT NULL +); + +-- Create private_messages table +CREATE TABLE private_messages +( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + sender_id BIGINT NOT NULL, + receiver_id BIGINT NOT NULL, + content VARCHAR(1000) NOT NULL, + read_at DATETIME, + deleted_at_by_sender DATETIME, + deleted_at_by_receiver DATETIME, + created_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), + updated_at DATETIME(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6) +); + +-- Create indexes for better query performance +CREATE INDEX idx_users_email ON users (email); +CREATE INDEX idx_feeds_author ON feeds (author_id); +CREATE INDEX idx_feeds_community ON feeds (community_id); +CREATE INDEX idx_comments_feed ON comments (feed_id); +CREATE INDEX idx_comments_author ON comments (author_id); +CREATE INDEX idx_private_messages_sender ON private_messages (sender_id); +CREATE INDEX idx_private_messages_receiver ON private_messages (receiver_id); +CREATE INDEX idx_communities_company ON communities (company_id);