Skip to content

Commit 9c846af

Browse files
feat: update test example (#155)
1 parent aba59bf commit 9c846af

File tree

6 files changed

+134
-0
lines changed

6 files changed

+134
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package tech.ydb.liquibase;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.PreparedStatement;
6+
import java.sql.ResultSet;
7+
import java.sql.SQLException;
8+
import liquibase.exception.CommandExecutionException;
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
import org.junit.jupiter.api.Test;
12+
13+
/**
14+
* @author Kirill Kurdyukov
15+
*/
16+
public class YdbDatabaseUpdateChangeLogTest extends BaseTest {
17+
18+
@Test
19+
void integrationTest() throws CommandExecutionException, SQLException {
20+
migrateChangeFile("./changelogs/changelog-init.xml");
21+
try (PreparedStatement ps = DriverManager.getConnection(jdbcUrl()).prepareStatement(
22+
"INSERT INTO DATABASECHANGELOG(" +
23+
"ID, AUTHOR, FILENAME, DATEEXECUTED, " +
24+
"ORDEREXECUTED, EXECTYPE, MD5SUM, " +
25+
"DESCRIPTION, COMMENTS, TAG, LIQUIBASE, " +
26+
"CONTEXTS, LABELS, DEPLOYMENT_ID) " +
27+
"VALUES (?, 'kurdyukov-kir', 'stub-file.xml', DATETIME('2024-04-01T11:30:20Z'), ?, " +
28+
"'EXECUTED', '9:cb49879b530528bc2555422bb7db58da', 'Stub', " +
29+
"'', '', '4.25.1', '', '', '1971019939')"
30+
)) {
31+
for (int i = 0; i < 210; i++) {
32+
ps.setString(1, String.valueOf(i));
33+
ps.setInt(2, i);
34+
35+
ps.executeUpdate();
36+
}
37+
}
38+
39+
migrateChangeFile("./changelogs/update/changelog-step-1.xml");
40+
try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
41+
ResultSet resultSet = connection.createStatement()
42+
.executeQuery("SELECT COUNT(*) AS cnt FROM test WHERE token is NULL");
43+
assertTrue(resultSet.next());
44+
assertEquals(3, resultSet.getLong("cnt"));
45+
}
46+
47+
migrateChangeFile("./changelogs/update/changelog-step-2.xml");
48+
try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
49+
ResultSet resultSet = connection.createStatement()
50+
.executeQuery("SELECT COUNT(*) AS cnt FROM test WHERE token is NULL");
51+
assertTrue(resultSet.next());
52+
assertEquals(0, resultSet.getLong("cnt"));
53+
}
54+
55+
try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
56+
connection.createStatement().execute("DROP TABLE test");
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
7+
8+
<include file="create-table.xml" relativeToChangelogFile="true"/>
9+
<include file="insert.xml" relativeToChangelogFile="true"/>
10+
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
7+
8+
<include file="create-table.xml" relativeToChangelogFile="true"/>
9+
<include file="insert.xml" relativeToChangelogFile="true"/>
10+
<include file="update.xml" relativeToChangelogFile="true"/>
11+
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
7+
8+
<changeSet id="create-table" author="author" context="all">
9+
<sql>
10+
create table test(
11+
id Int32 NOT NULL,
12+
code Text NOT NULL,
13+
token bool,
14+
PRIMARY KEY (id)
15+
);
16+
</sql>
17+
<rollback>
18+
drop table test;
19+
</rollback>
20+
</changeSet>
21+
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
7+
8+
<changeSet id="insert" author="author" context="all">
9+
<sql>
10+
insert into test(id, code, token)
11+
values (1, 'A', null),
12+
(2, 'A', null),
13+
(3, 'A', null),
14+
(4, 'B', true);
15+
</sql>
16+
</changeSet>
17+
</databaseChangeLog>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<databaseChangeLog
3+
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
6+
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
7+
8+
<changeSet id="update" author="author" context="all">
9+
<sql>
10+
update test set token= true where code = 'A';
11+
</sql>
12+
<rollback>
13+
update test set token=NULL where code='A';
14+
</rollback>
15+
</changeSet>
16+
</databaseChangeLog>

0 commit comments

Comments
 (0)