diff --git a/.github/workflows/comment.yaml b/.github/workflows/comment.yaml index bebc619..c75652b 100644 --- a/.github/workflows/comment.yaml +++ b/.github/workflows/comment.yaml @@ -49,11 +49,10 @@ jobs: mvn -q test env: DB_HOST: localhost - DB_PORT: 3306 - DB_NAME: db - DB_USER: v1 - DB_PASSWORD: ABCDEF - DB_OPTIONS: serverTimezone=Asia/Shanghai + DB_PORT: 5432 + DB_NAME: postgres + DB_USER: postgres + DB_PASSWORD: postgres TRACING_PROBABILITY: 0.1 OTLP_ENDPOINT: http://localhost:4318/v1/traces VOTE_SERVICE_BASE_URL: http://localhost:5000 diff --git a/graal/compose.yaml b/graal/compose.yaml index cff3af9..3c99d28 100644 --- a/graal/compose.yaml +++ b/graal/compose.yaml @@ -18,33 +18,31 @@ services: ports: - '18080:8080' environment: - - 'DB_HOST=mysql' - - 'DB_PORT=3306' - - 'DB_NAME=db' - - 'DB_USER=v1' - - 'DB_PASSWORD=ABCDEF' - - 'DB_OPTIONS=serverTimezone=Asia/Shanghai' + - 'DB_HOST=postgres' + - 'DB_PORT=5432' + - 'DB_NAME=postgres' + - 'DB_USER=postgres' + - 'DB_PASSWORD=postgres' - 'TRACING_PROBABILITY=0.1' - 'OTLP_ENDPOINT=http://jaeger:4318/v1/traces' - 'VOTE_SERVICE_BASE_URL=http://graph:8081' - 'USER_SERVICE_BASE_URL=http://shauser:5000' restart: 'always' depends_on: - mysql: + postgres: condition: service_healthy - mysql: - image: 'mysql:8.2.0' + postgres: + image: 'postgres:17.0-bookworm' environment: - - 'MYSQL_DATABASE=db' - - 'MYSQL_PASSWORD=ABCDEF' - - 'MYSQL_USER=v1' - - 'MYSQL_ROOT_PASSWORD=verysecret' + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + POSTGRES_DB: postgres volumes: - ./init.sql:/docker-entrypoint-initdb.d/init.sql ports: - - '3306:3306' + - '5432:5432' healthcheck: - test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ] + test: [ "CMD", "pg_isready", "-U", "postgres" ] timeout: 20s retries: 10 redis: diff --git a/graal/init.sql b/graal/init.sql index 8845198..9577106 100644 --- a/graal/init.sql +++ b/graal/init.sql @@ -11,28 +11,20 @@ * limitations under the License. */ -create table counts +CREATE TABLE comments ( - `id` bigint NOT NULL PRIMARY KEY, - `comment_count` int NOT NULL DEFAULT 0 -) engine = innodb; + id bigserial NOT NULL PRIMARY KEY, + user_id bigint NOT NULL, + content varchar(255) NOT NULL, + reply_to bigint, + belong_to bigint NOT NULL, + created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + reply_count int NOT NULL DEFAULT '0', + like_count int NOT NULL DEFAULT '0', + refer_to bigint +); +CREATE INDEX finder ON comments (belong_to, reply_to, id); -CREATE TABLE `comments` -( - `id` bigint NOT NULL AUTO_INCREMENT, - `user_id` bigint NOT NULL, - `content` varchar(255) NOT NULL, - `reply_to` bigint, - `belong_to` bigint NOT NULL, - `created_at` datetime NOT NULL DEFAULT (CURRENT_TIMESTAMP(3)), - `reply_count` int NOT NULL DEFAULT '0', - `like_count` int NOT NULL DEFAULT '0', - `refer_to` bigint, - PRIMARY KEY (`id`), - INDEX `finder` (`belong_to`, `reply_to`, `id`) -) ENGINE = InnoDB - DEFAULT CHARSET = utf8mb4 - COLLATE = utf8mb4_0900_bin; 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); INSERT INTO comments (user_id, content, reply_to, belong_to, created_at, reply_count, like_count) VALUES (2, 'hello world', null, 1, '2023-11-27 07:40:10', 0, 0); INSERT INTO comments (user_id, content, reply_to, belong_to, created_at, reply_count, like_count) -VALUES (2, 'hello world', null, 1, '2023-11-27 07:40:48', 0, 0); - -INSERT INTO counts (id, comment_count) -SELECT belong_to, COUNT(*) -FROM comments -GROUP BY belong_to; +VALUES (2, 'hello world', null, 1, '2023-11-27 07:40:48', 0, 0); \ No newline at end of file diff --git a/graal/pom.xml b/graal/pom.xml index 7eaa616..de3b890 100644 --- a/graal/pom.xml +++ b/graal/pom.xml @@ -79,8 +79,8 @@ opentelemetry-exporter-otlp - com.mysql - mysql-connector-j + org.postgresql + postgresql runtime diff --git a/graal/src/main/java/io/sixwaaaay/sharingcomment/config/DataSourceConfig.java b/graal/src/main/java/io/sixwaaaay/sharingcomment/config/DataSourceConfig.java index 89054e2..e4ebaf8 100644 --- a/graal/src/main/java/io/sixwaaaay/sharingcomment/config/DataSourceConfig.java +++ b/graal/src/main/java/io/sixwaaaay/sharingcomment/config/DataSourceConfig.java @@ -25,7 +25,7 @@ import org.springframework.data.jdbc.core.convert.*; import org.springframework.data.jdbc.core.mapping.JdbcMappingContext; import org.springframework.data.relational.core.dialect.Dialect; -import org.springframework.data.relational.core.dialect.MySqlDialect; +import org.springframework.data.relational.core.dialect.PostgresDialect; import org.springframework.data.relational.core.mapping.DefaultNamingStrategy; import org.springframework.data.relational.core.mapping.NamingStrategy; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; @@ -45,7 +45,6 @@ * The routing between the data sources is done using a custom RoutingDataSource. * The JDBC operations are set up using the NamedParameterJdbcTemplate class from Spring JDBC. * The transaction manager is set up using the DataSourceTransactionManager class from Spring JDBC. - * The dialect is MySQL. */ @Configuration public class DataSourceConfig { @@ -113,10 +112,10 @@ TransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } - // sql dialect, which is MySQL + // sql dialect @Bean Dialect jdbcDialect() { - return MySqlDialect.INSTANCE; + return PostgresDialect.INSTANCE; } // custom conversions diff --git a/graal/src/main/resources/application.yaml b/graal/src/main/resources/application.yaml index 06d860a..bdf30f3 100644 --- a/graal/src/main/resources/application.yaml +++ b/graal/src/main/resources/application.yaml @@ -11,15 +11,15 @@ spring: virtual: enabled: true datasource: - username: ${DB_USER:v1} - password: ${DB_PASSWORD:ABCDEF} - url: jdbc:mysql://${DB_HOST:db}:${DB_PORT:3306}/${DB_NAME:comments}?${DB_OPTIONS:serverTimezone=UTC} - driver-class-name: com.mysql.cj.jdbc.Driver + username: ${DB_USER:postgres} + password: ${DB_PASSWORD:postgres} + url: jdbc:postgresql://${DB_HOST:db}:${DB_PORT:5432}/${DB_NAME:postgres} + driver-class-name: org.postgresql.Driver replica-datasource: - username: ${DB_USER:v1} - password: ${DB_PASSWORD:ABCDEF} - url: jdbc:mysql://${DB_HOST:db}:${DB_PORT:3306}/${DB_NAME:comments}?${DB_OPTIONS:serverTimezone=UTC} - driver-class-name: com.mysql.cj.jdbc.Driver + username: ${DB_USER:postgres} + password: ${DB_PASSWORD:postgres} + url: jdbc:postgresql://${DB_HOST:db}:${DB_PORT:5432}/${DB_NAME:postgres} + driver-class-name: org.postgresql.Driver data: redis: database: 0