Skip to content

Commit

Permalink
Merge pull request #286 from sixwaaaay/pgj
Browse files Browse the repository at this point in the history
feat: pg support
  • Loading branch information
sixwaaaay authored Dec 31, 2024
2 parents dbecd43 + 3e13672 commit cd65058
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 60 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/comment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 13 additions & 15 deletions graal/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
39 changes: 13 additions & 26 deletions graal/init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
4 changes: 2 additions & 2 deletions graal/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions graal/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd65058

Please sign in to comment.