Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update test example #155

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>
Loading