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

BUGFIX: Fix H2 auto increment syntax #2477

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ public String getAutoIncrementClause(final BigInteger startWith, final BigIntege

if (generateIncrementBy) {
if (generateStartWith) {
autoIncrementClause += ", ";
autoIncrementClause += getAutoIncrementSeparator() + " ";
}

autoIncrementClause += String.format(getAutoIncrementByClause(), (incrementBy == null) ? defaultAutoIncrementBy : incrementBy);
Expand Down Expand Up @@ -601,6 +601,10 @@ protected String getAutoIncrementOpening() {
return " (";
}

protected String getAutoIncrementSeparator() {
return ",";
}

protected String getAutoIncrementClosing() {
return ")";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import liquibase.statement.DatabaseFunction;
import liquibase.util.ISODateFormat;
import liquibase.util.JdbcUtil;
import liquibase.util.StringUtil;

import java.lang.reflect.Method;
import java.sql.Connection;
Expand Down Expand Up @@ -290,18 +291,18 @@ public boolean supportsInitiallyDeferrableColumns() {
}

@Override
protected String getAutoIncrementClause() {
return "AUTO_INCREMENT";
protected String getAutoIncrementSeparator() {
return "";
}

@Override
protected String getAutoIncrementStartWithClause() {
return "%d";
}
protected String getAutoIncrementClause(final String generationType, final Boolean defaultOnNull) {
if (StringUtil.isEmpty(generationType)) {
return super.getAutoIncrementClause();
}

@Override
protected String getAutoIncrementByClause() {
return "%d";
String autoIncrementClause = "GENERATED %s AS IDENTITY"; // %s -- [ ALWAYS | BY DEFAULT ]
return String.format(autoIncrementClause, generationType);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public void testAutoIncrementH2Database() throws Exception {

Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);

assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT AUTO_INCREMENT)", generatedSql[0].toSql());
assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT GENERATED BY DEFAULT AS IDENTITY)", generatedSql[0].toSql());
}
}
}
Expand All @@ -539,7 +539,7 @@ public void testAutoIncrementStartWithH2Database() throws Exception {

Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);

assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT AUTO_INCREMENT (0))", generatedSql[0].toSql());
assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 0))", generatedSql[0].toSql());
}
}
}
Expand All @@ -557,7 +557,7 @@ public void testAutoIncrementStartWithIncrementByH2Database() throws Exception {

Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);

assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT AUTO_INCREMENT (0, 10))", generatedSql[0].toSql());
assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT GENERATED BY DEFAULT AS IDENTITY (START WITH 0 INCREMENT BY 10))", generatedSql[0].toSql());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
-- Change Parameter: columnDataType=int
-- Change Parameter: columnName=id
-- Change Parameter: tableName=person
ALTER TABLE person ALTER COLUMN id int AUTO_INCREMENT;
ALTER TABLE person ALTER COLUMN id int GENERATED BY DEFAULT AS IDENTITY;
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public void noSchema() throws Exception {
assertCorrect("alter table [table_name] modify column_name integer generated by default as identity", PostgresDatabase.class, CockroachDatabase.class, EnterpriseDBDatabase.class);
assertCorrect("alter table table_name modify column_name int auto_increment", MySQLDatabase.class);
assertCorrect("ALTER TABLE [table_name] ALTER COLUMN [column_name] SET GENERATED BY DEFAULT AS IDENTITY", DB2Database.class);
assertCorrect("alter table table_name alter column column_name int generated by default as identity", HsqlDatabase.class);
assertCorrect("alter table table_name alter column column_name int auto_increment", H2Database.class);
assertCorrect("alter table table_name alter column column_name int generated by default as identity", HsqlDatabase.class, H2Database.class);

assertCorrect("ALTER TABLE [table_name] MODIFY [column_name] serial", InformixDatabase.class);
assertCorrect("ALTER TABLE [table_name] ALTER [column_name] SET DEFAULT AUTOINCREMENT", SybaseASADatabase.class);
Expand Down