Skip to content

Commit

Permalink
Tests compatible with H2 2.x (#386)
Browse files Browse the repository at this point in the history
* fix(test): Update `id` generation SQL

This has changed from `id INT AUTO_INCREMENT NOT NULL` to `id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL`

* fix(test): Update SQL to find created tables

The table `table_type` has changed to `BASE TABLE` and is shared with `information_schema` tables.
Selecting tables where `table_class = 'org.h2.mvstore.db.MVTable'` is a suitable replacement.

* fix(test): Update test dependency `com.h2database:h2` to `2.3.232`

* fix(test): Add `logback-test.xml` to `src/integration-test/resources`

Unsupported file extension in 'file:/home/github/grails/grails-database-migration/build/resources/integrationTest/logback.groovy'. Only .xml is supported

* chore: Update license header of changed source files

* use h2 version from grails-bom

---------

Co-authored-by: James Fredley <[email protected]>
  • Loading branch information
matrei and jamesfredley authored Oct 25, 2024
1 parent 77f66ab commit 3f91e70
Show file tree
Hide file tree
Showing 18 changed files with 97 additions and 66 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ dependencies {
testImplementation "org.grails.plugins:hibernate5"
testImplementation "org.grails:grails-gorm-testing-support"
testImplementation "org.grails:grails-web-testing-support"
testImplementation "com.h2database:h2:$h2Version"
testImplementation "com.h2database:h2"

documentation "org.apache.groovy:groovy"
documentation "org.apache.groovy:groovy-ant"
Expand Down
2 changes: 0 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ asciidoctorJvmVersion=4.0.3
grailsVersion=7.0.0-SNAPSHOT
grailsGradlePluginVersion=7.0.0-SNAPSHOT
groovyVersion=4.0.23
# Tests are built for this version
h2Version=1.4.200
liquibaseHibernate5Version=4.27.0

websiteUrl=https://grails-plugins.github.io/grails-database-migration
Expand Down
16 changes: 16 additions & 0 deletions src/integration-test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<charset>UTF-8</charset>
<pattern>%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%5p) %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n%wex</pattern>
</encoder>
</appender>

<root level="ERROR"><appender-ref ref="STDOUT"/></root>

</configuration>
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand Down Expand Up @@ -43,11 +43,11 @@ class DbmDiffCommandSpec extends ApplicationContextDatabaseMigrationCommandSpec
otherDbConnection = otherDbDataSource.connection
otherDbSql = new Sql(otherDbConnection)
otherDbSql.executeUpdate '''
CREATE TABLE book (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, CONSTRAINT PK_BOOK PRIMARY KEY (id))
CREATE TABLE book (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, title VARCHAR(255) NOT NULL, CONSTRAINT PK_BOOK PRIMARY KEY (id))
'''
sql.executeUpdate '''
CREATE TABLE book (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, price INT NOT NULL, CONSTRAINT PK_BOOK PRIMARY KEY (id));
CREATE TABLE author (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT PK_AUTHOR PRIMARY KEY (id));
CREATE TABLE book (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, title VARCHAR(255) NOT NULL, price INT NOT NULL, CONSTRAINT PK_BOOK PRIMARY KEY (id));
CREATE TABLE author (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT PK_AUTHOR PRIMARY KEY (id));
'''
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand All @@ -26,13 +26,15 @@ class DbmDropAllCommandSpec extends ApplicationContextDatabaseMigrationCommandSp

def "drops all database objects"() {
given:
sql.executeUpdate 'CREATE TABLE book (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, CONSTRAINT PK_BOOK PRIMARY KEY (id))'
assert sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'')
sql.executeUpdate 'CREATE TABLE book (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, title VARCHAR(255) NOT NULL, CONSTRAINT PK_BOOK PRIMARY KEY (id))'

expect:
sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'')

when:
command.handle(getExecutionContext())

then:
!sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'')
!sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'')
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand All @@ -26,8 +26,8 @@ class DbmGenerateChangelogCommandSpec extends ApplicationContextDatabaseMigratio

def setup() {
sql.executeUpdate '''
CREATE TABLE book (id INT AUTO_INCREMENT NOT NULL, title VARCHAR(255) NOT NULL, price INT NOT NULL, CONSTRAINT PK_BOOK PRIMARY KEY (id));
CREATE TABLE author (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT PK_AUTHOR PRIMARY KEY (id));
CREATE TABLE book (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, title VARCHAR(255) NOT NULL, price INT NOT NULL, CONSTRAINT PK_BOOK PRIMARY KEY (id));
CREATE TABLE author (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT PK_AUTHOR PRIMARY KEY (id));
'''
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand All @@ -26,7 +26,7 @@ class DbmGormDiffCommandSpec extends ApplicationContextDatabaseMigrationCommandS
}

def setup() {
sql.executeUpdate 'CREATE TABLE PUBLIC.author (id BIGINT AUTO_INCREMENT NOT NULL, version BIGINT NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT authorPK PRIMARY KEY (id));'
sql.executeUpdate 'CREATE TABLE PUBLIC.author (id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, version BIGINT NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT authorPK PRIMARY KEY (id));'
}

def "diffs GORM classes against a database and generates a changelog to STDOUT"() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2015-2024 original authors
*
* 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
*
* https://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.
*/
package org.grails.plugins.databasemigration.command

import grails.dev.commands.ApplicationCommand
Expand All @@ -20,7 +35,7 @@ class DbmPreviousChangesetSqlCommandSpec extends ApplicationContextDatabaseMigra

new DbmUpdateCommand(applicationContext: applicationContext).handle(getExecutionContext())

def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
assert tables as Set == ['book', 'author', 'databasechangeloglock', 'databasechangelog'] as Set
}

Expand All @@ -31,7 +46,7 @@ class DbmPreviousChangesetSqlCommandSpec extends ApplicationContextDatabaseMigra

then:
def output = output.toString()
output.contains('CREATE TABLE PUBLIC.book (id BIGINT AUTO_INCREMENT NOT NULL, version BIGINT NOT NULL, author_id BIGINT NOT NULL, title VARCHAR(255) NOT NULL, CONSTRAINT bookPK PRIMARY KEY (id));')
output.contains('CREATE TABLE PUBLIC.book (id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, version BIGINT NOT NULL, author_id BIGINT NOT NULL, title VARCHAR(255) NOT NULL, CONSTRAINT bookPK PRIMARY KEY (id));')
}

void "The last SQL change sets to a file given as arguments"() {
Expand All @@ -40,7 +55,7 @@ class DbmPreviousChangesetSqlCommandSpec extends ApplicationContextDatabaseMigra

then:
def output = outputFile.text
output.contains('CREATE TABLE PUBLIC.book (id BIGINT AUTO_INCREMENT NOT NULL, version BIGINT NOT NULL, author_id BIGINT NOT NULL, title VARCHAR(255) NOT NULL, CONSTRAINT bookPK PRIMARY KEY (id));')
output.contains('CREATE TABLE PUBLIC.book (id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, version BIGINT NOT NULL, author_id BIGINT NOT NULL, title VARCHAR(255) NOT NULL, CONSTRAINT bookPK PRIMARY KEY (id));')

}

Expand All @@ -50,7 +65,7 @@ class DbmPreviousChangesetSqlCommandSpec extends ApplicationContextDatabaseMigra

then:
def output = outputFile.text
output.contains('CREATE TABLE PUBLIC.author (id BIGINT AUTO_INCREMENT NOT NULL, version BIGINT NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT authorPK PRIMARY KEY (id));')
output.contains('CREATE TABLE PUBLIC.author (id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL, version BIGINT NOT NULL, name VARCHAR(255) NOT NULL, CONSTRAINT authorPK PRIMARY KEY (id));')

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand All @@ -32,7 +32,7 @@ class DbmRollbackCommandSpec extends ApplicationContextDatabaseMigrationCommandS
new DbmTagCommand(applicationContext: applicationContext).handle(getExecutionContext('test-tag'))
new DbmUpdateCommand(applicationContext: applicationContext).handle(getExecutionContext())

def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
assert tables as Set == ['book', 'author', 'databasechangeloglock', 'databasechangelog'] as Set
}

Expand All @@ -41,7 +41,7 @@ class DbmRollbackCommandSpec extends ApplicationContextDatabaseMigrationCommandS
command.handle(getExecutionContext('test-tag'))

then:
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
tables as Set == ['author', 'databasechangeloglock', 'databasechangelog'] as Set
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand All @@ -30,7 +30,7 @@ class DbmRollbackCountCommandSpec extends ApplicationContextDatabaseMigrationCom

new DbmUpdateCommand(applicationContext: applicationContext).handle(getExecutionContext())

def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
assert tables as Set == ['book', 'author', 'databasechangeloglock', 'databasechangelog'] as Set
}

Expand All @@ -39,7 +39,7 @@ class DbmRollbackCountCommandSpec extends ApplicationContextDatabaseMigrationCom
command.handle(getExecutionContext('1'))

then:
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
tables as Set == ['author', 'databasechangeloglock', 'databasechangelog'] as Set
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand Down Expand Up @@ -34,7 +34,7 @@ class DbmRollbackCountSqlCommandSpec extends ApplicationContextDatabaseMigration

new DbmUpdateCommand(applicationContext: applicationContext).handle(getExecutionContext())

def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
assert tables as Set == ['book', 'author', 'databasechangeloglock', 'databasechangelog'] as Set
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand Down Expand Up @@ -36,7 +36,7 @@ class DbmRollbackSqlCommandSpec extends ApplicationContextDatabaseMigrationComma
new DbmTagCommand(applicationContext: applicationContext).handle(getExecutionContext('test-tag'))
new DbmUpdateCommand(applicationContext: applicationContext).handle(getExecutionContext())

def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
assert tables as Set == ['book', 'author', 'databasechangeloglock', 'databasechangelog'] as Set
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand All @@ -31,7 +31,7 @@ class DbmRollbackToDateCommandSpec extends ApplicationContextDatabaseMigrationCo
new DbmUpdateCommand(applicationContext: applicationContext).handle(getExecutionContext())
sql.executeUpdate('UPDATE PUBLIC.DATABASECHANGELOG SET DATEEXECUTED = \'2015-01-02 12:00:00\' WHERE ID = \'1\'')

def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
assert tables as Set == ['book', 'author', 'databasechangeloglock', 'databasechangelog'] as Set
}

Expand All @@ -40,7 +40,7 @@ class DbmRollbackToDateCommandSpec extends ApplicationContextDatabaseMigrationCo
command.handle(getExecutionContext(args as String[]))

then:
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
tables as Set == ['author', 'databasechangeloglock', 'databasechangelog'] as Set

where:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright 2015 original authors
* Copyright 2015-2024 original authors
*
* 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
* https://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,
Expand Down Expand Up @@ -35,7 +35,7 @@ class DbmRollbackToDateSqlCommandSpec extends ApplicationContextDatabaseMigratio
new DbmUpdateCommand(applicationContext: applicationContext).handle(getExecutionContext())
sql.executeUpdate('UPDATE PUBLIC.DATABASECHANGELOG SET DATEEXECUTED = \'2015-01-02 12:00:00\' WHERE ID = \'1\'')

def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_type = \'TABLE\'').collect { it.table_name.toLowerCase() }
def tables = sql.rows('SELECT table_name FROM information_schema.tables WHERE table_class = \'org.h2.mvstore.db.MVTable\'').collect { it.table_name.toLowerCase() }
assert tables as Set == ['book', 'author', 'databasechangeloglock', 'databasechangelog'] as Set
}

Expand Down
Loading

0 comments on commit 3f91e70

Please sign in to comment.