Skip to content

Commit

Permalink
增加mysql环境。 #7
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy0v0 committed Oct 10, 2024
1 parent 0038501 commit 13f682f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ name: Java CI with Maven

on:
push:
branches: [ "1.1.x" ]
branches: [ '**' ]
pull_request:
branches: [ "1.1.x" ]
branches: [ '**' ]

jobs:
build:
Expand Down
29 changes: 20 additions & 9 deletions jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>2.0.5</slf4j.version>
<flyway.version>10.10.0</flyway.version>
<testcontainers.version>1.19.8</testcontainers.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -41,13 +42,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>2.1.214</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
Expand All @@ -61,11 +55,23 @@
<version>42.7.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.19.8</version>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>mysql</artifactId>
<version>${testcontainers.version}</version>
<scope>test</scope>
</dependency>

Expand All @@ -80,6 +86,11 @@
<artifactId>flyway-database-postgresql</artifactId>
<version>${flyway.version}</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
<version>${flyway.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 5 additions & 0 deletions jdbc/src/test/java/test/container/ContainerUtil.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package test.container;

import org.testcontainers.containers.MySQLContainer;
import org.testcontainers.containers.PostgreSQLContainer;

public final class ContainerUtil {
Expand All @@ -8,4 +9,8 @@ public final class ContainerUtil {

public static final String PG_LOCATIONS = "classpath:db/pg";

public static final MySQLContainer<?> MYSQL = new MySQLContainer<>("mysql:8");

public static final String MYSQL_LOCATIONS = "classpath:db/mysql";

}
58 changes: 58 additions & 0 deletions jdbc/src/test/java/test/mysql/TransactionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package test.mysql;

import com.github.fantasy0v0.swift.jdbc.JDBC;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import test.container.ContainerUtil;
import test.container.JdbcContainer;

import javax.sql.DataSource;
import java.sql.Connection;

import static com.github.fantasy0v0.swift.jdbc.JDBC.*;

public class TransactionTest {

private final Logger log = LoggerFactory.getLogger(TransactionTest.class);

private final static JdbcContainer container = JdbcContainer.create(
ContainerUtil.MYSQL, ContainerUtil.MYSQL_LOCATIONS
);

@BeforeAll
static void beforeAll() {
DataSource dataSource = container.start();
JDBC.configuration(dataSource);
}

@AfterAll
static void afterAll() {
container.stop();
}

@Test
void test() {
transaction(() -> {
select("select * from student").fetch();
transaction(Connection.TRANSACTION_READ_UNCOMMITTED, () -> {
select("select * from student").fetch();
transaction(Connection.TRANSACTION_READ_COMMITTED, () -> {
modify("update student set name = ? where id = ?")
.execute("修改", 1L);
});
});
});
}

@Test
void rollback() {
transaction(() -> {
modify("update student set name = ? where id = ?")
.execute("修改", 1L);
});
}

}
14 changes: 14 additions & 0 deletions jdbc/src/test/resources/db/mysql/V1.0.0__init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CREATE TABLE student (
id bigint NOT NULL,
name text NOT NULL,
status INT NOT NULL,
ext text
);
CREATE TABLE datetime_test (
id bigint NOT NULL,
date timestamp NOT NULL
);
insert into student(id, name, status) values
(1, '小明', 0), (2, '张三', 1),
(3, '李四', 2), (4, '董超', 2),
(5, '薛霸', 2);

0 comments on commit 13f682f

Please sign in to comment.