Skip to content

Commit

Permalink
feat: update test example (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirillKurdyukov authored Sep 6, 2024
1 parent aba59bf commit 9c846af
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package tech.ydb.liquibase;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import liquibase.exception.CommandExecutionException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.Test;

/**
* @author Kirill Kurdyukov
*/
public class YdbDatabaseUpdateChangeLogTest extends BaseTest {

@Test
void integrationTest() throws CommandExecutionException, SQLException {
migrateChangeFile("./changelogs/changelog-init.xml");
try (PreparedStatement ps = DriverManager.getConnection(jdbcUrl()).prepareStatement(
"INSERT INTO DATABASECHANGELOG(" +
"ID, AUTHOR, FILENAME, DATEEXECUTED, " +
"ORDEREXECUTED, EXECTYPE, MD5SUM, " +
"DESCRIPTION, COMMENTS, TAG, LIQUIBASE, " +
"CONTEXTS, LABELS, DEPLOYMENT_ID) " +
"VALUES (?, 'kurdyukov-kir', 'stub-file.xml', DATETIME('2024-04-01T11:30:20Z'), ?, " +
"'EXECUTED', '9:cb49879b530528bc2555422bb7db58da', 'Stub', " +
"'', '', '4.25.1', '', '', '1971019939')"
)) {
for (int i = 0; i < 210; i++) {
ps.setString(1, String.valueOf(i));
ps.setInt(2, i);

ps.executeUpdate();
}
}

migrateChangeFile("./changelogs/update/changelog-step-1.xml");
try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
ResultSet resultSet = connection.createStatement()
.executeQuery("SELECT COUNT(*) AS cnt FROM test WHERE token is NULL");
assertTrue(resultSet.next());
assertEquals(3, resultSet.getLong("cnt"));
}

migrateChangeFile("./changelogs/update/changelog-step-2.xml");
try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
ResultSet resultSet = connection.createStatement()
.executeQuery("SELECT COUNT(*) AS cnt FROM test WHERE token is NULL");
assertTrue(resultSet.next());
assertEquals(0, resultSet.getLong("cnt"));
}

try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
connection.createStatement().execute("DROP TABLE test");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<include file="create-table.xml" relativeToChangelogFile="true"/>
<include file="insert.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<include file="create-table.xml" relativeToChangelogFile="true"/>
<include file="insert.xml" relativeToChangelogFile="true"/>
<include file="update.xml" relativeToChangelogFile="true"/>
</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<changeSet id="create-table" author="author" context="all">
<sql>
create table test(
id Int32 NOT NULL,
code Text NOT NULL,
token bool,
PRIMARY KEY (id)
);
</sql>
<rollback>
drop table test;
</rollback>
</changeSet>
</databaseChangeLog>
17 changes: 17 additions & 0 deletions liquibase-dialect/src/test/resources/changelogs/update/insert.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<changeSet id="insert" author="author" context="all">
<sql>
insert into test(id, code, token)
values (1, 'A', null),
(2, 'A', null),
(3, 'A', null),
(4, 'B', true);
</sql>
</changeSet>
</databaseChangeLog>
16 changes: 16 additions & 0 deletions liquibase-dialect/src/test/resources/changelogs/update/update.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">

<changeSet id="update" author="author" context="all">
<sql>
update test set token= true where code = 'A';
</sql>
<rollback>
update test set token=NULL where code='A';
</rollback>
</changeSet>
</databaseChangeLog>

0 comments on commit 9c846af

Please sign in to comment.