Skip to content

Commit 614b4e3

Browse files
committed
Add MySQL support with Docker integration and update schema
1 parent 27e9a78 commit 614b4e3

File tree

6 files changed

+70
-47
lines changed

6 files changed

+70
-47
lines changed

api/src/main/kotlin/io/github/gunkim/realworld/config/SecurityConfig.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import org.springframework.beans.factory.annotation.Value
66
import org.springframework.boot.autoconfigure.security.servlet.PathRequest
77
import org.springframework.context.annotation.Bean
88
import org.springframework.context.annotation.Configuration
9-
import org.springframework.http.HttpMethod
109
import org.springframework.security.config.annotation.web.builders.HttpSecurity
1110
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
1211
import org.springframework.security.config.annotation.web.configurers.AuthorizeHttpRequestsConfigurer
@@ -26,6 +25,8 @@ class SecurityConfiguration(
2625
private val headerPrefix: String,
2726
@Value("\${jwt.header-name}")
2827
private val headerName: String,
28+
@Value("\${spring.h2.console.enabled:false}")
29+
private val h2ConsoleEnabled: Boolean,
2930
) {
3031
@Bean
3132
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain =
@@ -79,7 +80,7 @@ class SecurityConfiguration(
7980
private fun configureAuthorization(it: AuthorizeHttpRequestsConfigurer<HttpSecurity>.AuthorizationManagerRequestMatcherRegistry) {
8081
// users
8182
it.requestMatchers("/api/users/**").permitAll()
82-
it.requestMatchers(HttpMethod.GET, "/api/profiles/**").permitAll()
83+
it.requestMatchers("/api/profiles/**").permitAll()
8384

8485
// articles
8586
it.requestMatchers("/api/articles/**").permitAll()
@@ -88,7 +89,9 @@ class SecurityConfiguration(
8889
it.requestMatchers("/api/tags/**").permitAll()
8990

9091
// H2 Console
91-
it.requestMatchers(PathRequest.toH2Console()).permitAll()
92+
if (h2ConsoleEnabled) {
93+
it.requestMatchers(PathRequest.toH2Console()).permitAll()
94+
}
9295
it.anyRequest().authenticated()
9396
}
9497

api/src/main/resources/application-datasource.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ spring:
1414
activate:
1515
on-profile: local-h2-datasource
1616
datasource:
17-
url: jdbc:h2:mem:testdb
17+
url: jdbc:h2:mem:testdb;MODE=MySQL
1818
driverClassName: org.h2.Driver
1919
username: sa
2020
h2:
@@ -28,12 +28,13 @@ spring:
2828
init:
2929
mode: always
3030
---
31+
# Note: Execute /docker/docker-compose.yml to start MySQL for local development!
3132
spring:
3233
config:
3334
activate:
3435
on-profile: local-mysql-datasource
3536
datasource:
36-
url: jdbc:mysql://localhost:3306/realworld?useSSL=false&serverTimezone=Asia/Seoul
37+
url: jdbc:mysql://localhost:3306/realworld?useSSL=false
3738
driverClassName: com.mysql.cj.jdbc.Driver
3839
username: root
3940
password: test

core-impl/src/main/kotlin/io/github/gunkim/realworld/infrastructure/jdbc/article/model/ArticleJpaEntity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ArticleJpaEntity(
4545
@Column(name = "article_id")
4646
@GeneratedValue(strategy = GenerationType.IDENTITY)
4747
val databaseId: Int? = null,
48-
@Column(name = "uuid")
48+
@Column(name = "uuid", columnDefinition = "BINARY(16)")
4949
@Convert(converter = ArticleIdConverter::class)
5050
override val id: ArticleId,
5151
slug: Slug,

core-impl/src/main/kotlin/io/github/gunkim/realworld/infrastructure/jdbc/user/model/UserJpaEntity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class UserJpaEntity(
2626
@Column(name = "user_id")
2727
@GeneratedValue(strategy = GenerationType.IDENTITY)
2828
val databaseId: Int? = null,
29-
@Column(name = "uuid")
29+
@Column(name = "uuid", columnDefinition = "BINARY(16)")
3030
@Convert(converter = UserIdConverter::class)
3131
override val id: UserId,
3232
name: String,

core-impl/src/main/resources/schema.sql

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,70 @@
11
-- Add Tables
22
CREATE TABLE IF NOT EXISTS users
33
(
4-
user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
5-
uuid UUID NOT NULL,
6-
email VARCHAR(255) UNIQUE NOT NULL,
7-
name VARCHAR(255) UNIQUE NOT NULL,
8-
password VARCHAR(255) NOT NULL,
4+
user_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
5+
uuid BINARY(16) NOT NULL,
6+
email VARCHAR(255) UNIQUE NOT NULL,
7+
name VARCHAR(255) UNIQUE NOT NULL,
8+
password VARCHAR(255) NOT NULL,
99
bio VARCHAR(255),
1010
image VARCHAR(255),
11-
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
12-
updated_at TIMESTAMP(6) NOT NULL
11+
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL,
12+
updated_at TIMESTAMP(6) NOT NULL
1313
);
1414

1515
CREATE TABLE IF NOT EXISTS article
1616
(
17-
article_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
18-
uuid UUID NOT NULL,
19-
slug VARCHAR(255) UNIQUE NOT NULL,
20-
title VARCHAR(255) NOT NULL,
21-
description VARCHAR(255) NOT NULL,
22-
body VARCHAR(255) NOT NULL,
23-
author_id INT NOT NULL,
24-
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
25-
updated_at TIMESTAMP(6) NOT NULL,
17+
article_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
18+
uuid BINARY(16) NOT NULL,
19+
slug VARCHAR(255) UNIQUE NOT NULL,
20+
title VARCHAR(255) NOT NULL,
21+
description VARCHAR(255) NOT NULL,
22+
body VARCHAR(255) NOT NULL,
23+
author_id INT NOT NULL,
24+
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL,
25+
updated_at TIMESTAMP(6) NOT NULL,
2626
deleted_at TIMESTAMP(6),
2727
CONSTRAINT fk_article_author FOREIGN KEY (author_id) REFERENCES users (user_id)
2828
);
2929

3030
CREATE TABLE IF NOT EXISTS article_favorite
3131
(
32-
favorite_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
33-
article_id INT NOT NULL,
34-
user_id INT NOT NULL,
35-
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
36-
updated_at TIMESTAMP(6) NOT NULL,
32+
favorite_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
33+
article_id INT NOT NULL,
34+
user_id INT NOT NULL,
35+
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL,
36+
updated_at TIMESTAMP(6) NOT NULL,
3737
CONSTRAINT fk_favorite_article FOREIGN KEY (article_id) REFERENCES article (article_id),
3838
CONSTRAINT fk_favorite_user FOREIGN KEY (user_id) REFERENCES users (user_id),
3939
CONSTRAINT unique_article_favorite UNIQUE (article_id, user_id)
4040
);
4141

4242
CREATE TABLE IF NOT EXISTS comment
4343
(
44-
comment_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
45-
article_id INT NOT NULL,
46-
body VARCHAR(255) NOT NULL,
47-
author_id INT NOT NULL,
48-
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
49-
updated_at TIMESTAMP(6) NOT NULL,
44+
comment_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
45+
article_id INT NOT NULL,
46+
body VARCHAR(255) NOT NULL,
47+
author_id INT NOT NULL,
48+
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL,
49+
updated_at TIMESTAMP(6) NOT NULL,
5050
CONSTRAINT fk_comment_article FOREIGN KEY (article_id) REFERENCES article (article_id),
5151
CONSTRAINT fk_comment_author FOREIGN KEY (author_id) REFERENCES users (user_id)
5252
);
5353

5454
CREATE TABLE IF NOT EXISTS tag
5555
(
56-
tag_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
57-
name VARCHAR(255) UNIQUE NOT NULL,
58-
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
59-
updated_at TIMESTAMP(6) NOT NULL
56+
tag_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
57+
name VARCHAR(255) UNIQUE NOT NULL,
58+
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL,
59+
updated_at TIMESTAMP(6) NOT NULL
6060
);
6161

6262
CREATE TABLE IF NOT EXISTS article_tag
6363
(
64-
article_tag_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
65-
article_id INT NOT NULL,
66-
tag_id INT NOT NULL,
67-
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
64+
article_tag_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
65+
article_id INT NOT NULL,
66+
tag_id INT NOT NULL,
67+
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL,
6868
updated_at TIMESTAMP(6),
6969
CONSTRAINT fk_article_tag_article FOREIGN KEY (article_id) REFERENCES article (article_id),
7070
CONSTRAINT fk_article_tag_tag FOREIGN KEY (tag_id) REFERENCES tag (tag_id),
@@ -73,11 +73,11 @@ CREATE TABLE IF NOT EXISTS article_tag
7373

7474
CREATE TABLE IF NOT EXISTS user_follow
7575
(
76-
follow_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
77-
followee_id INT NOT NULL,
78-
follower_id INT NOT NULL,
79-
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP NOT NULL,
80-
updated_at TIMESTAMP(6) NOT NULL,
76+
follow_id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
77+
followee_id INT NOT NULL,
78+
follower_id INT NOT NULL,
79+
created_at TIMESTAMP(6) DEFAULT CURRENT_TIMESTAMP(6) NOT NULL,
80+
updated_at TIMESTAMP(6) NOT NULL,
8181
UNIQUE (follower_id, followee_id),
8282
CONSTRAINT fk_user_follow_followee FOREIGN KEY (followee_id) REFERENCES users (user_id),
8383
CONSTRAINT fk_user_follow_follower FOREIGN KEY (follower_id) REFERENCES users (user_id)

docker/docker-compose.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
version: '3.1'
2+
3+
services:
4+
mysql:
5+
image: mysql:latest
6+
container_name: realworld-mysql
7+
environment:
8+
MYSQL_ROOT_PASSWORD: test
9+
MYSQL_DATABASE: realworld
10+
MYSQL_USER: realworld
11+
MYSQL_PASSWORD: test
12+
ports:
13+
- "3306:3306"
14+
volumes:
15+
- mysql-data:/var/lib/mysql
16+
- ../core-impl/src/main/resources/schema.sql:/docker-entrypoint-initdb.d/schema.sql
17+
18+
volumes:
19+
mysql-data:

0 commit comments

Comments
 (0)