-
Notifications
You must be signed in to change notification settings - Fork 48
Add integration tests for CUBRID database support #128
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
219 changes: 219 additions & 0 deletions
219
...tabase-cubrid/src/test/java/org/flywaydb/community/database/cubrid/CubridSupportTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| /*- | ||
| * ========================LICENSE_START================================= | ||
| * flyway-database-cubrid | ||
| * ======================================================================== | ||
| * Copyright (C) 2010 - 2026 Red Gate Software Ltd | ||
| * ======================================================================== | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * =========================LICENSE_END================================== | ||
| */ | ||
| package org.flywaydb.community.database.cubrid; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.sql.Connection; | ||
| import java.sql.DriverManager; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.sql.Statement; | ||
| import java.time.Duration; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import org.flywaydb.core.Flyway; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.testcontainers.containers.GenericContainer; | ||
| import org.testcontainers.containers.wait.strategy.Wait; | ||
| import org.testcontainers.junit.jupiter.Container; | ||
| import org.testcontainers.junit.jupiter.Testcontainers; | ||
| import org.testcontainers.utility.DockerImageName; | ||
|
|
||
| @Testcontainers(disabledWithoutDocker = true) | ||
| class CubridSupportTest { | ||
| private static final int BROKER_PORT = 33000; | ||
| private static final String DATABASE_NAME = "cubrid"; | ||
| private static final String USER_SCHEMA = "public"; | ||
| private static final String INITIAL_MIGRATION_LOCATION = "cubrid_initial_migration"; | ||
| private static final String NEXT_MIGRATION_LOCATION = "cubrid_next_migration"; | ||
|
|
||
| @Container | ||
| static final GenericContainer<?> cubrid = new GenericContainer<>(DockerImageName.parse("cubrid/cubrid:11.4")) | ||
| .withEnv("CUBRID_DB", DATABASE_NAME) | ||
| .withExposedPorts(BROKER_PORT) | ||
| .waitingFor(Wait.forListeningPort().withStartupTimeout(Duration.ofMinutes(3))); | ||
|
|
||
| @BeforeAll | ||
| static void waitUntilJdbcIsReady() throws Exception { | ||
| long deadline = System.nanoTime() + Duration.ofMinutes(2).toNanos(); | ||
| Exception lastFailure = null; | ||
|
|
||
| while (System.nanoTime() < deadline) { | ||
| try (Connection connection = connection(); | ||
| Statement statement = connection.createStatement()) { | ||
| statement.execute("SELECT 1"); | ||
| return; | ||
| } catch (Exception e) { | ||
| lastFailure = e; | ||
| Thread.sleep(1_000); | ||
| } | ||
| } | ||
|
|
||
| if (lastFailure != null) { | ||
| throw lastFailure; | ||
| } | ||
| throw new IllegalStateException("CUBRID JDBC endpoint was not ready"); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| void resetDatabase() throws SQLException { | ||
| try (Connection connection = connection(); | ||
| Statement statement = connection.createStatement()) { | ||
| statement.execute("DROP TABLE IF EXISTS app_user"); | ||
| statement.execute("DROP TABLE IF EXISTS flyway_schema_history"); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void migrates() throws SQLException { | ||
| Flyway flyway = flyway(INITIAL_MIGRATION_LOCATION); | ||
|
|
||
| assertThat(tableNames()).doesNotContain("app_user", "flyway_schema_history"); | ||
|
|
||
| flyway.migrate(); | ||
|
|
||
| assertThat(tableNames()).contains("app_user", "flyway_schema_history"); | ||
| assertThat(countAppUserRows()).isEqualTo(2); | ||
| assertThat(historyDescriptions()).containsExactly("create app user", "insert app users"); | ||
| } | ||
|
|
||
| @Test | ||
| void doesNotApplyMigrationSeveralTimes() throws SQLException { | ||
| Flyway flyway = flyway(INITIAL_MIGRATION_LOCATION); | ||
|
|
||
| flyway.migrate(); | ||
| flyway.migrate(); | ||
| flyway.migrate(); | ||
|
|
||
| assertThat(countAppUserRows()).isEqualTo(2); | ||
| assertThat(historyDescriptions()).containsExactly("create app user", "insert app users"); | ||
| } | ||
|
|
||
| @Test | ||
| void appliesOnlyNewMigrationsWhenSomeWereAlreadyApplied() throws SQLException { | ||
| flyway(INITIAL_MIGRATION_LOCATION).migrate(); | ||
|
|
||
| assertThat(countAppUserRows()).isEqualTo(2); | ||
| assertThat(historyDescriptions()).containsExactly("create app user", "insert app users"); | ||
|
|
||
| flyway(NEXT_MIGRATION_LOCATION).migrate(); | ||
|
|
||
| assertThat(countAppUserRows()).isEqualTo(3); | ||
| assertThat(historyDescriptions()).containsExactly("create app user", "insert app users", "insert more app users"); | ||
| } | ||
|
|
||
| @Test | ||
| void validatesAppliedMigrations() { | ||
| Flyway flyway = flyway(INITIAL_MIGRATION_LOCATION); | ||
|
|
||
| flyway.migrate(); | ||
|
|
||
| flyway.validate(); | ||
| } | ||
|
|
||
| @Test | ||
| void setsBaseline() throws SQLException { | ||
| Flyway flyway = Flyway.configure() | ||
| .communityDBSupportEnabled(true) | ||
| .dataSource(jdbcUrl(), USER_SCHEMA, "") | ||
| .defaultSchema(USER_SCHEMA) | ||
| .locations(INITIAL_MIGRATION_LOCATION) | ||
| .baselineVersion("123") | ||
| .load(); | ||
|
|
||
| flyway.baseline(); | ||
|
|
||
| assertThat(tableNames()).contains("flyway_schema_history"); | ||
| assertThat(historyDescriptions()).containsExactly("<< Flyway Baseline >>"); | ||
| } | ||
|
|
||
| @Test | ||
| void cleansUserTables() throws SQLException { | ||
| Flyway flyway = Flyway.configure() | ||
| .communityDBSupportEnabled(true) | ||
| .dataSource(jdbcUrl(), USER_SCHEMA, "") | ||
| .defaultSchema(USER_SCHEMA) | ||
| .locations(INITIAL_MIGRATION_LOCATION) | ||
| .cleanDisabled(false) | ||
| .load(); | ||
|
|
||
| flyway.migrate(); | ||
| assertThat(tableNames()).contains("app_user", "flyway_schema_history"); | ||
|
|
||
| flyway.clean(); | ||
|
|
||
| assertThat(tableNames()).doesNotContain("app_user", "flyway_schema_history"); | ||
| } | ||
|
|
||
| private static Flyway flyway(String location) { | ||
| return Flyway.configure() | ||
| .communityDBSupportEnabled(true) | ||
| .dataSource(jdbcUrl(), USER_SCHEMA, "") | ||
| .defaultSchema(USER_SCHEMA) | ||
| .locations(location) | ||
|
ibi107 marked this conversation as resolved.
Outdated
|
||
| .load(); | ||
| } | ||
|
|
||
| private static Connection connection() throws SQLException { | ||
| return DriverManager.getConnection(jdbcUrl(), USER_SCHEMA, ""); | ||
| } | ||
|
|
||
| private static String jdbcUrl() { | ||
| return "jdbc:cubrid:" + cubrid.getHost() + ":" + cubrid.getMappedPort(BROKER_PORT) + ":" + DATABASE_NAME + ":::"; | ||
| } | ||
|
|
||
| private static Set<String> tableNames() throws SQLException { | ||
| return Set.copyOf(queryForStrings( | ||
| "SELECT class_name FROM db_class WHERE class_type = 'CLASS' AND is_system_class = 'NO'" | ||
| )); | ||
| } | ||
|
|
||
| private static List<String> historyDescriptions() throws SQLException { | ||
| return queryForStrings( | ||
| "SELECT description FROM flyway_schema_history ORDER BY installed_rank" | ||
| ); | ||
| } | ||
|
|
||
| private static int countAppUserRows() throws SQLException { | ||
| try (Connection connection = connection(); | ||
| Statement statement = connection.createStatement(); | ||
| ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM app_user")) { | ||
| resultSet.next(); | ||
| return resultSet.getInt(1); | ||
| } | ||
| } | ||
|
|
||
| private static List<String> queryForStrings(String sql) throws SQLException { | ||
| try (Connection connection = connection(); | ||
| Statement statement = connection.createStatement(); | ||
| ResultSet resultSet = statement.executeQuery(sql)) { | ||
| List<String> results = new ArrayList<>(); | ||
| while (resultSet.next()) { | ||
| results.add(resultSet.getString(1)); | ||
| } | ||
| return results; | ||
| } | ||
| } | ||
|
|
||
| } | ||
23 changes: 23 additions & 0 deletions
23
flyway-database-cubrid/src/test/resources/cubrid_initial_migration/V001__create_app_user.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --- | ||
| -- ========================LICENSE_START================================= | ||
| -- flyway-database-cubrid | ||
| -- ======================================================================== | ||
| -- Copyright (C) 2010 - 2026 Red Gate Software Ltd | ||
| -- ======================================================================== | ||
| -- Licensed under the Apache License, Version 2.0 (the "License"); | ||
| -- you may not use this file except in compliance with the License. | ||
| -- You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
| -- =========================LICENSE_END================================== | ||
| --- | ||
| CREATE TABLE app_user ( | ||
| id INT PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL | ||
| ); |
21 changes: 21 additions & 0 deletions
21
...ay-database-cubrid/src/test/resources/cubrid_initial_migration/V002__insert_app_users.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --- | ||
| -- ========================LICENSE_START================================= | ||
| -- flyway-database-cubrid | ||
| -- ======================================================================== | ||
| -- Copyright (C) 2010 - 2026 Red Gate Software Ltd | ||
| -- ======================================================================== | ||
| -- Licensed under the Apache License, Version 2.0 (the "License"); | ||
| -- you may not use this file except in compliance with the License. | ||
| -- You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
| -- =========================LICENSE_END================================== | ||
| --- | ||
| INSERT INTO app_user(id, name) VALUES (1, 'alice'); | ||
| INSERT INTO app_user(id, name) VALUES (2, 'bob'); |
23 changes: 23 additions & 0 deletions
23
flyway-database-cubrid/src/test/resources/cubrid_next_migration/V001__create_app_user.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --- | ||
| -- ========================LICENSE_START================================= | ||
| -- flyway-database-cubrid | ||
| -- ======================================================================== | ||
| -- Copyright (C) 2010 - 2026 Red Gate Software Ltd | ||
| -- ======================================================================== | ||
| -- Licensed under the Apache License, Version 2.0 (the "License"); | ||
| -- you may not use this file except in compliance with the License. | ||
| -- You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
| -- =========================LICENSE_END================================== | ||
| --- | ||
| CREATE TABLE app_user ( | ||
| id INT PRIMARY KEY, | ||
| name VARCHAR(100) NOT NULL | ||
| ); |
21 changes: 21 additions & 0 deletions
21
flyway-database-cubrid/src/test/resources/cubrid_next_migration/V002__insert_app_users.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| --- | ||
| -- ========================LICENSE_START================================= | ||
| -- flyway-database-cubrid | ||
| -- ======================================================================== | ||
| -- Copyright (C) 2010 - 2026 Red Gate Software Ltd | ||
| -- ======================================================================== | ||
| -- Licensed under the Apache License, Version 2.0 (the "License"); | ||
| -- you may not use this file except in compliance with the License. | ||
| -- You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
| -- =========================LICENSE_END================================== | ||
| --- | ||
| INSERT INTO app_user(id, name) VALUES (1, 'alice'); | ||
| INSERT INTO app_user(id, name) VALUES (2, 'bob'); |
20 changes: 20 additions & 0 deletions
20
...-database-cubrid/src/test/resources/cubrid_next_migration/V003__insert_more_app_users.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| --- | ||
| -- ========================LICENSE_START================================= | ||
| -- flyway-database-cubrid | ||
| -- ======================================================================== | ||
| -- Copyright (C) 2010 - 2026 Red Gate Software Ltd | ||
| -- ======================================================================== | ||
| -- Licensed under the Apache License, Version 2.0 (the "License"); | ||
| -- you may not use this file except in compliance with the License. | ||
| -- You may obtain a copy of the License at | ||
| -- | ||
| -- http://www.apache.org/licenses/LICENSE-2.0 | ||
| -- | ||
| -- Unless required by applicable law or agreed to in writing, software | ||
| -- distributed under the License is distributed on an "AS IS" BASIS, | ||
| -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| -- See the License for the specific language governing permissions and | ||
| -- limitations under the License. | ||
| -- =========================LICENSE_END================================== | ||
| --- | ||
| INSERT INTO app_user(id, name) VALUES (3, 'carol'); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.