Skip to content

Commit cd65058

Browse files
authored
Merge pull request #286 from sixwaaaay/pgj
feat: pg support
2 parents dbecd43 + 3e13672 commit cd65058

File tree

6 files changed

+43
-60
lines changed

6 files changed

+43
-60
lines changed

.github/workflows/comment.yaml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,10 @@ jobs:
4949
mvn -q test
5050
env:
5151
DB_HOST: localhost
52-
DB_PORT: 3306
53-
DB_NAME: db
54-
DB_USER: v1
55-
DB_PASSWORD: ABCDEF
56-
DB_OPTIONS: serverTimezone=Asia/Shanghai
52+
DB_PORT: 5432
53+
DB_NAME: postgres
54+
DB_USER: postgres
55+
DB_PASSWORD: postgres
5756
TRACING_PROBABILITY: 0.1
5857
OTLP_ENDPOINT: http://localhost:4318/v1/traces
5958
VOTE_SERVICE_BASE_URL: http://localhost:5000

graal/compose.yaml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,31 @@ services:
1818
ports:
1919
- '18080:8080'
2020
environment:
21-
- 'DB_HOST=mysql'
22-
- 'DB_PORT=3306'
23-
- 'DB_NAME=db'
24-
- 'DB_USER=v1'
25-
- 'DB_PASSWORD=ABCDEF'
26-
- 'DB_OPTIONS=serverTimezone=Asia/Shanghai'
21+
- 'DB_HOST=postgres'
22+
- 'DB_PORT=5432'
23+
- 'DB_NAME=postgres'
24+
- 'DB_USER=postgres'
25+
- 'DB_PASSWORD=postgres'
2726
- 'TRACING_PROBABILITY=0.1'
2827
- 'OTLP_ENDPOINT=http://jaeger:4318/v1/traces'
2928
- 'VOTE_SERVICE_BASE_URL=http://graph:8081'
3029
- 'USER_SERVICE_BASE_URL=http://shauser:5000'
3130
restart: 'always'
3231
depends_on:
33-
mysql:
32+
postgres:
3433
condition: service_healthy
35-
mysql:
36-
image: 'mysql:8.2.0'
34+
postgres:
35+
image: 'postgres:17.0-bookworm'
3736
environment:
38-
- 'MYSQL_DATABASE=db'
39-
- 'MYSQL_PASSWORD=ABCDEF'
40-
- 'MYSQL_USER=v1'
41-
- 'MYSQL_ROOT_PASSWORD=verysecret'
37+
POSTGRES_USER: postgres
38+
POSTGRES_PASSWORD: postgres
39+
POSTGRES_DB: postgres
4240
volumes:
4341
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
4442
ports:
45-
- '3306:3306'
43+
- '5432:5432'
4644
healthcheck:
47-
test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
45+
test: [ "CMD", "pg_isready", "-U", "postgres" ]
4846
timeout: 20s
4947
retries: 10
5048
redis:

graal/init.sql

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,20 @@
1111
* limitations under the License.
1212
*/
1313

14-
create table counts
14+
CREATE TABLE comments
1515
(
16-
`id` bigint NOT NULL PRIMARY KEY,
17-
`comment_count` int NOT NULL DEFAULT 0
18-
) engine = innodb;
16+
id bigserial NOT NULL PRIMARY KEY,
17+
user_id bigint NOT NULL,
18+
content varchar(255) NOT NULL,
19+
reply_to bigint,
20+
belong_to bigint NOT NULL,
21+
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
22+
reply_count int NOT NULL DEFAULT '0',
23+
like_count int NOT NULL DEFAULT '0',
24+
refer_to bigint
25+
);
26+
CREATE INDEX finder ON comments (belong_to, reply_to, id);
1927

20-
CREATE TABLE `comments`
21-
(
22-
`id` bigint NOT NULL AUTO_INCREMENT,
23-
`user_id` bigint NOT NULL,
24-
`content` varchar(255) NOT NULL,
25-
`reply_to` bigint,
26-
`belong_to` bigint NOT NULL,
27-
`created_at` datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP(3)),
28-
`reply_count` int NOT NULL DEFAULT '0',
29-
`like_count` int NOT NULL DEFAULT '0',
30-
`refer_to` bigint,
31-
PRIMARY KEY (`id`),
32-
INDEX `finder` (`belong_to`, `reply_to`, `id`)
33-
) ENGINE = InnoDB
34-
DEFAULT CHARSET = utf8mb4
35-
COLLATE = utf8mb4_0900_bin;
3628

3729

3830
INSERT INTO comments (user_id, content, reply_to, belong_to, created_at, reply_count, like_count)
@@ -200,9 +192,4 @@ VALUES (2, 'hello world', null, 1, '2023-11-27 06:44:55', 0, 0);
200192
INSERT INTO comments (user_id, content, reply_to, belong_to, created_at, reply_count, like_count)
201193
VALUES (2, 'hello world', null, 1, '2023-11-27 07:40:10', 0, 0);
202194
INSERT INTO comments (user_id, content, reply_to, belong_to, created_at, reply_count, like_count)
203-
VALUES (2, 'hello world', null, 1, '2023-11-27 07:40:48', 0, 0);
204-
205-
INSERT INTO counts (id, comment_count)
206-
SELECT belong_to, COUNT(*)
207-
FROM comments
208-
GROUP BY belong_to;
195+
VALUES (2, 'hello world', null, 1, '2023-11-27 07:40:48', 0, 0);

graal/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@
7979
<artifactId>opentelemetry-exporter-otlp</artifactId>
8080
</dependency>
8181
<dependency>
82-
<groupId>com.mysql</groupId>
83-
<artifactId>mysql-connector-j</artifactId>
82+
<groupId>org.postgresql</groupId>
83+
<artifactId>postgresql</artifactId>
8484
<scope>runtime</scope>
8585
</dependency>
8686
<dependency>

graal/src/main/java/io/sixwaaaay/sharingcomment/config/DataSourceConfig.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import org.springframework.data.jdbc.core.convert.*;
2626
import org.springframework.data.jdbc.core.mapping.JdbcMappingContext;
2727
import org.springframework.data.relational.core.dialect.Dialect;
28-
import org.springframework.data.relational.core.dialect.MySqlDialect;
28+
import org.springframework.data.relational.core.dialect.PostgresDialect;
2929
import org.springframework.data.relational.core.mapping.DefaultNamingStrategy;
3030
import org.springframework.data.relational.core.mapping.NamingStrategy;
3131
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
@@ -45,7 +45,6 @@
4545
* The routing between the data sources is done using a custom RoutingDataSource.
4646
* The JDBC operations are set up using the NamedParameterJdbcTemplate class from Spring JDBC.
4747
* The transaction manager is set up using the DataSourceTransactionManager class from Spring JDBC.
48-
* The dialect is MySQL.
4948
*/
5049
@Configuration
5150
public class DataSourceConfig {
@@ -113,10 +112,10 @@ TransactionManager transactionManager(DataSource dataSource) {
113112
return new DataSourceTransactionManager(dataSource);
114113
}
115114

116-
// sql dialect, which is MySQL
115+
// sql dialect
117116
@Bean
118117
Dialect jdbcDialect() {
119-
return MySqlDialect.INSTANCE;
118+
return PostgresDialect.INSTANCE;
120119
}
121120

122121
// custom conversions

graal/src/main/resources/application.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ spring:
1111
virtual:
1212
enabled: true
1313
datasource:
14-
username: ${DB_USER:v1}
15-
password: ${DB_PASSWORD:ABCDEF}
16-
url: jdbc:mysql://${DB_HOST:db}:${DB_PORT:3306}/${DB_NAME:comments}?${DB_OPTIONS:serverTimezone=UTC}
17-
driver-class-name: com.mysql.cj.jdbc.Driver
14+
username: ${DB_USER:postgres}
15+
password: ${DB_PASSWORD:postgres}
16+
url: jdbc:postgresql://${DB_HOST:db}:${DB_PORT:5432}/${DB_NAME:postgres}
17+
driver-class-name: org.postgresql.Driver
1818
replica-datasource:
19-
username: ${DB_USER:v1}
20-
password: ${DB_PASSWORD:ABCDEF}
21-
url: jdbc:mysql://${DB_HOST:db}:${DB_PORT:3306}/${DB_NAME:comments}?${DB_OPTIONS:serverTimezone=UTC}
22-
driver-class-name: com.mysql.cj.jdbc.Driver
19+
username: ${DB_USER:postgres}
20+
password: ${DB_PASSWORD:postgres}
21+
url: jdbc:postgresql://${DB_HOST:db}:${DB_PORT:5432}/${DB_NAME:postgres}
22+
driver-class-name: org.postgresql.Driver
2323
data:
2424
redis:
2525
database: 0

0 commit comments

Comments
 (0)