Skip to content

Commit

Permalink
tests: add table with compound foreign key
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Sep 16, 2023
1 parent d979b68 commit f86c7d7
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ void it_should_extract_sql_scripts_from_annotation() {

assertThat(sqlScripts).isNotEmpty().hasSize(2);
assertThat(sqlScripts.get(0).getQueries()).isNotEmpty().containsExactly(
"DROP TABLE IF EXISTS users_movies_events;",
"DROP TABLE IF EXISTS users_movies;",
"DROP TABLE IF EXISTS movies;",
"DROP TABLE IF EXISTS users;"
Expand All @@ -183,6 +184,13 @@ void it_should_extract_sql_scripts_from_annotation() {
" PRIMARY KEY (user_id, movie_id), " +
" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, " +
" FOREIGN KEY (movie_id) REFERENCES movies(id) ON DELETE CASCADE " +
");",
"CREATE TABLE users_movies_events ( " +
" user_id INT, " +
" movie_id INT, " +
" id INT PRIMARY KEY, " +
" event VARCHAR(200), " +
" FOREIGN KEY (user_id, movie_id) REFERENCES users_movies (user_id, movie_id) ON DELETE CASCADE " +
");"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ void it_should_extract_sql_scripts_from_class_context() {
assertThat(ctx).isNotNull();
assertThat(ctx.getInitScripts()).isNotEmpty().hasSize(2);
assertThat(ctx.getInitScripts().get(0).getQueries()).isNotEmpty().containsExactly(
"DROP TABLE IF EXISTS users_movies_events;",
"DROP TABLE IF EXISTS users_movies;",
"DROP TABLE IF EXISTS movies;",
"DROP TABLE IF EXISTS users;"
Expand All @@ -87,6 +88,13 @@ void it_should_extract_sql_scripts_from_class_context() {
" PRIMARY KEY (user_id, movie_id), " +
" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, " +
" FOREIGN KEY (movie_id) REFERENCES movies(id) ON DELETE CASCADE " +
");",
"CREATE TABLE users_movies_events ( " +
" user_id INT, " +
" movie_id INT, " +
" id INT PRIMARY KEY, " +
" event VARCHAR(200), " +
" FOREIGN KEY (user_id, movie_id) REFERENCES users_movies (user_id, movie_id) ON DELETE CASCADE " +
");"
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,13 @@ private static void verifyParsedQueries(List<String> queries) {
" PRIMARY KEY (user_id, movie_id), " +
" FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, " +
" FOREIGN KEY (movie_id) REFERENCES movies(id) ON DELETE CASCADE " +
");",
"CREATE TABLE users_movies_events ( " +
" user_id INT, " +
" movie_id INT, " +
" id INT PRIMARY KEY, " +
" event VARCHAR(200), " +
" FOREIGN KEY (user_id, movie_id) REFERENCES users_movies (user_id, movie_id) ON DELETE CASCADE " +
");"
);
}
Expand All @@ -294,6 +301,16 @@ private static void verifyExecutedQueries(Connection connection, PreparedStateme
");"
);

verifyQueryExecution(inOrder, connection, statement,
"CREATE TABLE users_movies_events ( " +
" user_id INT, " +
" movie_id INT, " +
" id INT PRIMARY KEY, " +
" event VARCHAR(200), " +
" FOREIGN KEY (user_id, movie_id) REFERENCES users_movies (user_id, movie_id) ON DELETE CASCADE " +
");"
);

inOrder.verifyNoMoreInteractions();
}

Expand Down
1 change: 1 addition & 0 deletions src/test/resources/liquibase/changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
<include file="changesets/01-create-users.xml" relativeToChangelogFile="true"/>
<include file="changesets/02-create-movies.xml" relativeToChangelogFile="true"/>
<include file="changesets/03-create-users-movies.xml" relativeToChangelogFile="true"/>
<include file="changesets/04-create-users-movies-events.xml" relativeToChangelogFile="true"/>

</databaseChangeLog>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!--
The MIT License (MIT)
Copyright (c) 2015-2021 Mickael Jeanroy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->

<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">

<changeSet id="create-table-users-movies-events" author="mjeanroy">
<createTable tableName="users_movies_events">
<column name="id" type="INT" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="event" type="VARCHAR(200)"/>
<column name="user_id" type="INT">
<constraints nullable="false"/>
</column>
<column name="movie_id" type="INT">
<constraints nullable="false"/>
</column>
</createTable>
</changeSet>

<changeSet id="add-table-users-movies-events-fk" author="mjeanroy">
<addForeignKeyConstraint
constraintName="fk_users_movies_events_user_id_movie_id"
baseTableName="users_movies_events"
baseColumnNames="user_id,movie_id"
referencedTableName="users_movies"
referencedColumnNames="user_id,movie_id"
/>
</changeSet>

</databaseChangeLog>
1 change: 1 addition & 0 deletions src/test/resources/sql/drop.sql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
--
-- Drop tables if it already exist.
--
DROP TABLE IF EXISTS users_movies_events;
DROP TABLE IF EXISTS users_movies;
DROP TABLE IF EXISTS movies;
DROP TABLE IF EXISTS users;
9 changes: 9 additions & 0 deletions src/test/resources/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,19 @@
--
CREATE TABLE users (id INT PRIMARY KEY, name varchar(100));
CREATE TABLE movies (id INT PRIMARY KEY, title varchar(100), synopsys varchar(200));

CREATE TABLE users_movies (
user_id INT,
movie_id INT,
PRIMARY KEY (user_id, movie_id),
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (movie_id) REFERENCES movies(id) ON DELETE CASCADE
);

CREATE TABLE users_movies_events (
user_id INT,
movie_id INT,
id INT PRIMARY KEY,
event VARCHAR(200),
FOREIGN KEY (user_id, movie_id) REFERENCES users_movies (user_id, movie_id) ON DELETE CASCADE
);

0 comments on commit f86c7d7

Please sign in to comment.