From c0d752b44597d07c55f708858df0144700d85075 Mon Sep 17 00:00:00 2001 From: rob bygrave Date: Thu, 20 Feb 2020 16:51:07 +1300 Subject: [PATCH] #1941 - @History it seems does not work well when it got generated with ebean.allQuotedIdentifiers enabled --- .../platform/SqlServerHistoryDdl.java | 15 +++++++++------ .../platform/SqlServerHistoryDdlTest.java | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerHistoryDdlTest.java diff --git a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerHistoryDdl.java b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerHistoryDdl.java index 9f28a88c9..f5d907748 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerHistoryDdl.java +++ b/src/main/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerHistoryDdl.java @@ -31,11 +31,14 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl { enableSystemVersioning(writer, baseTable); } - private String getHistoryTable(String baseTable) { - String historyTable = baseTable + "_history"; // history must contain schema, otherwise you'll get - // Setting SYSTEM_VERSIONING to ON failed because history table 'xxx_history' is not specified in two-part name format. + String getHistoryTable(String baseTable) { + String historyTable = baseTable + "_history"; + if (baseTable.startsWith("[")) { + historyTable = historyTable.replace("]", "") + "]"; + } if (historyTable.indexOf('.') == -1) { - historyTable = "dbo." +historyTable; // so add the default schema, if none was specified. + // history must contain schema, add the default schema if none was specified + historyTable = "dbo." + historyTable; } return historyTable; } @@ -86,10 +89,10 @@ public class SqlServerHistoryDdl implements PlatformHistoryDdl { DdlBuffer apply = writer.applyHistoryView(); String baseTableName = baseTable.getBaseTable(); apply.append("-- alter table ").append(baseTableName).append(" set (system_versioning = off (history_table=") - .append(getHistoryTable(baseTableName)).append("))").endOfStatement(); + .append(getHistoryTable(baseTableName)).append("))").endOfStatement(); apply.append("-- history migration goes here").newLine(); apply.append("-- alter table ").append(baseTableName).append(" set (system_versioning = on (history_table=") - .append(getHistoryTable(baseTableName)).append("))").endOfStatement(); + .append(getHistoryTable(baseTableName)).append("))").endOfStatement(); } } diff --git a/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerHistoryDdlTest.java b/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerHistoryDdlTest.java new file mode 100644 index 000000000..f1ac32365 --- /dev/null +++ b/src/test/java/io/ebeaninternal/dbmigration/ddlgeneration/platform/SqlServerHistoryDdlTest.java @@ -0,0 +1,17 @@ +package io.ebeaninternal.dbmigration.ddlgeneration.platform; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SqlServerHistoryDdlTest { + + @Test + public void getHistoryTable() { + + SqlServerHistoryDdl ddl = new SqlServerHistoryDdl(); + assertThat(ddl.getHistoryTable("foo")).isEqualTo("dbo.foo_history"); + assertThat(ddl.getHistoryTable("bar.foo")).isEqualTo("bar.foo_history"); + assertThat(ddl.getHistoryTable("[Foo]")).isEqualTo("dbo.[Foo_history]"); + } +}