-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supporting postgres in-memory db translation for HSQL
- Loading branch information
1 parent
c4a58ea
commit a1f2cfe
Showing
26 changed files
with
679 additions
and
403 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
27 changes: 27 additions & 0 deletions
27
...main/java/com/gs/obevo/db/impl/platforms/postgresql/PostgreSqlToH2TranslationDialect.java
This file contains 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,27 @@ | ||
/** | ||
* Copyright 2017 Goldman Sachs. | ||
* 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. | ||
*/ | ||
package com.gs.obevo.db.impl.platforms.postgresql; | ||
|
||
import com.gs.obevo.db.impl.platforms.DefaultDbTranslationDialect; | ||
import org.eclipse.collections.api.list.ImmutableList; | ||
import org.eclipse.collections.impl.factory.Lists; | ||
|
||
public class PostgreSqlToH2TranslationDialect extends DefaultDbTranslationDialect { | ||
@Override | ||
public ImmutableList<String> getInitSqls() { | ||
return Lists.immutable.with("SET MODE PostgreSQL"); | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
...in/java/com/gs/obevo/db/impl/platforms/postgresql/PostgreSqlToHsqlTranslationDialect.java
This file contains 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,92 @@ | ||
/** | ||
* Copyright 2017 Goldman Sachs. | ||
* 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. | ||
*/ | ||
package com.gs.obevo.db.impl.platforms.postgresql; | ||
|
||
import java.sql.Connection; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import com.gs.obevo.api.platform.ChangeType; | ||
import com.gs.obevo.db.impl.core.jdbc.JdbcHelper; | ||
import com.gs.obevo.db.impl.platforms.DefaultDbTranslationDialect; | ||
import com.gs.obevo.db.impl.platforms.sqltranslator.InMemoryTranslator; | ||
import com.gs.obevo.db.impl.platforms.sqltranslator.PostColumnSqlTranslator; | ||
import com.gs.obevo.db.impl.platforms.sqltranslator.PreParsedSqlTranslator; | ||
import com.gs.obevo.db.impl.platforms.sqltranslator.SqlTranslatorConfigHelper; | ||
import com.gs.obevo.db.sqlparser.syntaxparser.CreateTable; | ||
import com.gs.obevo.db.sqlparser.syntaxparser.CreateTableColumn; | ||
import com.gs.obevo.impl.PrepareDbChange; | ||
import org.eclipse.collections.api.list.ImmutableList; | ||
import org.eclipse.collections.api.set.ImmutableSet; | ||
import org.eclipse.collections.impl.factory.Lists; | ||
import org.eclipse.collections.impl.factory.Sets; | ||
|
||
public class PostgreSqlToHsqlTranslationDialect extends DefaultDbTranslationDialect { | ||
private final PostColumnSqlTranslator replaceNextvalWithIdentity = new PostColumnSqlTranslator() { | ||
private final Pattern defaultPattern = Pattern.compile("(?i)default\\s+nextval.*", Pattern.DOTALL); | ||
|
||
@Override | ||
public String handlePostColumnText(String postColumnText, CreateTableColumn column, CreateTable table) { | ||
Matcher defaultMatcher = defaultPattern.matcher(postColumnText); | ||
if (defaultMatcher.find()) { | ||
postColumnText = defaultMatcher.replaceFirst("IDENTITY"); | ||
} | ||
|
||
return postColumnText; | ||
} | ||
}; | ||
|
||
private final PreParsedSqlTranslator substituteCreateOrReplace = new PreParsedSqlTranslator() { | ||
Pattern pattern = Pattern.compile("(?i)^\\s*create\\s+or\\s+replace\\s+", Pattern.DOTALL); | ||
@Override | ||
public String preprocessSql(String sql) { | ||
Matcher matcher = pattern.matcher(sql); | ||
if (matcher.find()) { | ||
sql = matcher.replaceFirst("create "); | ||
} | ||
return sql; | ||
} | ||
}; | ||
|
||
@Override | ||
public ImmutableList<String> getInitSqls() { | ||
return Lists.immutable.with( | ||
"SET DATABASE SQL SYNTAX PGS TRUE" | ||
, "SET DATABASE TRANSACTION CONTROL MVCC" | ||
); | ||
} | ||
|
||
@Override | ||
public ImmutableList<PrepareDbChange> getAdditionalTranslators() { | ||
SqlTranslatorConfigHelper configHelper = SqlTranslatorConfigHelper.createInMemoryDefault(); | ||
|
||
configHelper.getPreParsedSqlTranslators() | ||
.with(substituteCreateOrReplace); | ||
configHelper.getPostColumnSqlTranslators() | ||
.with(replaceNextvalWithIdentity); | ||
return Lists.immutable.<PrepareDbChange>with(new InMemoryTranslator(configHelper)); | ||
} | ||
|
||
@Override | ||
public void initSchema(JdbcHelper jdbc, Connection conn) { | ||
updateAndIgnoreException(conn, jdbc, "create type int4 as integer"); | ||
} | ||
|
||
@Override | ||
public ImmutableSet<String> getDisabledChangeTypeNames() { | ||
return Sets.immutable.of(ChangeType.FUNCTION_STR, ChangeType.SP_STR); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...rc/test/java/com/gs/obevo/db/impl/platforms/postgresql/PostgreSqlInMemConversionTest.java
This file contains 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,53 @@ | ||
/** | ||
* Copyright 2017 Goldman Sachs. | ||
* 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. | ||
*/ | ||
package com.gs.obevo.db.impl.platforms.postgresql; | ||
|
||
import com.gs.obevo.db.api.platform.DbDeployerAppContext; | ||
import com.gs.obevo.db.impl.platforms.h2.H2DbPlatform; | ||
import com.gs.obevo.db.impl.platforms.hsql.HsqlDbPlatform; | ||
import com.gs.obevo.db.unittest.UnitTestDbBuilder; | ||
import org.junit.Test; | ||
|
||
public class PostgreSqlInMemConversionTest { | ||
@Test | ||
public void testInMemoryHsql() { | ||
DbDeployerAppContext context = UnitTestDbBuilder.newBuilder() | ||
.setSourcePath("platforms/postgresql/example1/step1/system-config-inmem.xml") | ||
.setReferenceEnvName("unittestrefhsql") | ||
.setDbPlatform(new HsqlDbPlatform()) | ||
.setDbServer("mydb2testHsql") | ||
.buildContext(); | ||
context.setupEnvInfra(); | ||
context.cleanAndDeploy(); | ||
|
||
// TODO add assertions | ||
} | ||
|
||
// Implementation for H2 TBD | ||
// @Test | ||
// public void testInMemoryH2() { | ||
// DbDeployerAppContext context = UnitTestDbBuilder.newBuilder() | ||
// .setSourcePath("platforms/postgresql/example1/step1/system-config-inmem.xml") | ||
// .setReferenceEnvName("unittestrefh2") | ||
// .setDbPlatform(new H2DbPlatform()) | ||
// .setDbServer("mydb2testH2") | ||
// .buildContext(); | ||
// context.setupEnvInfra(); | ||
// context.cleanAndDeploy(); | ||
// | ||
// // TODO add assertions | ||
// } | ||
} |
58 changes: 29 additions & 29 deletions
58
...stgresql/src/test/resources/platforms/postgresql/example1/step1/schema1/table/TABLE_A.ddl
This file contains 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 |
---|---|---|
@@ -1,29 +1,29 @@ | ||
//// CHANGE name=chng1 | ||
CREATE TABLE TABLE_A ( | ||
A_ID INT NOT NULL, | ||
B_ID INT NOT NULL, | ||
STRING_FIELD VARCHAR(30) NULL, | ||
TIMESTAMP_FIELD TIMESTAMP NULL, | ||
PRIMARY KEY (A_ID) | ||
) | ||
GO | ||
|
||
//// CHANGE name=emptychange | ||
//// CHANGE name=emptychange2 | ||
|
||
|
||
//// CHANGE FK name=chng2 | ||
ALTER TABLE TABLE_A ADD FOREIGN KEY (B_ID) REFERENCES TABLE_B(B_ID) | ||
GO | ||
//// CHANGE name=chng3 | ||
ALTER TABLE TABLE_A ADD COLUMN C_ID INT NULL | ||
GO | ||
//// CHANGE name=extra1 | ||
ALTER TABLE TABLE_A ADD COLUMN EXTRA1 INT NULL | ||
GO | ||
//// CHANGE name=extra2 | ||
ALTER TABLE TABLE_A ADD COLUMN EXTRA2 INT NULL | ||
GO | ||
//// CHANGE name=extra3 | ||
ALTER TABLE TABLE_A ADD COLUMN EXTRA3 INT NULL | ||
GO | ||
//// CHANGE name=chng1 | ||
CREATE TABLE TABLE_A ( | ||
A_ID INT NOT NULL, | ||
B_ID INT NOT NULL, | ||
STRING_FIELD VARCHAR(30) NULL, | ||
TIMESTAMP_FIELD TIMESTAMP NULL, | ||
PRIMARY KEY (A_ID) | ||
) WITHOUT OIDS | ||
GO | ||
|
||
//// CHANGE name=emptychange | ||
//// CHANGE name=emptychange2 | ||
|
||
|
||
//// CHANGE FK name=chng2 | ||
ALTER TABLE TABLE_A ADD FOREIGN KEY (B_ID) REFERENCES TABLE_B(B_ID) | ||
GO | ||
//// CHANGE name=chng3 | ||
ALTER TABLE TABLE_A ADD COLUMN C_ID INT NULL | ||
GO | ||
//// CHANGE name=extra1 | ||
ALTER TABLE TABLE_A ADD COLUMN EXTRA1 INT NULL | ||
GO | ||
//// CHANGE name=extra2 | ||
ALTER TABLE TABLE_A ADD COLUMN EXTRA2 INT NULL | ||
GO | ||
//// CHANGE name=extra3 | ||
ALTER TABLE TABLE_A ADD COLUMN EXTRA3 INT NULL | ||
GO |
10 changes: 5 additions & 5 deletions
10
...stgresql/src/test/resources/platforms/postgresql/example1/step1/schema1/table/TABLE_B.ddl
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
//// CHANGE name=chng1 | ||
CREATE TABLE TABLE_B ( | ||
B_ID INT NOT NULL, | ||
PRIMARY KEY (B_ID) | ||
) | ||
//// CHANGE name=chng1 | ||
CREATE TABLE TABLE_B ( | ||
B_ID INT NOT NULL, | ||
PRIMARY KEY (B_ID) | ||
) WITHOUT OIDS |
This file contains 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
16 changes: 8 additions & 8 deletions
16
...sql/src/test/resources/platforms/postgresql/example1/step1/schema1/table/TAB_WITH_SEQ.ddl
This file contains 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
//// METADATA excludePlatforms=REDSHIFT | ||
//// CHANGE name=chng1 | ||
CREATE TABLE TAB_WITH_SEQ ( | ||
ID int4 NOT NULL DEFAULT nextval('MYSEQ1'::regclass), | ||
FIELD1 INT NULL, | ||
PRIMARY KEY (ID) | ||
) | ||
GO | ||
//// METADATA excludePlatforms=REDSHIFT | ||
//// CHANGE name=chng1 | ||
CREATE TABLE TAB_WITH_SEQ ( | ||
ID int4 NOT NULL DEFAULT nextval('MYSEQ1'::regclass), | ||
FIELD1 INT NULL, | ||
PRIMARY KEY (ID) | ||
) WITHOUT OIDS | ||
GO |
3 changes: 3 additions & 0 deletions
3
...rc/test/resources/platforms/postgresql/example1/step1/schema1/usertype/usertype0.hsql.sql
This file contains 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,3 @@ | ||
//// METADATA includePlatforms="HSQL" | ||
CREATE TYPE usertype0 AS VARCHAR(1); | ||
GO |
1 change: 1 addition & 0 deletions
1
...sql/src/test/resources/platforms/postgresql/example1/step1/schema1/usertype/usertype0.sql
This file contains 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//// METADATA excludePlatforms="HSQL" | ||
CREATE TYPE usertype0 AS ENUM ( | ||
'1', | ||
'2', | ||
|
27 changes: 27 additions & 0 deletions
27
...postgresql/src/test/resources/platforms/postgresql/example1/step1/system-config-inmem.xml
This file contains 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,27 @@ | ||
<!-- | ||
Copyright 2017 Goldman Sachs. | ||
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. | ||
--> | ||
<dbSystemConfig type="POSTGRESQL" sourceDirs="platforms/postgresql/example1/step1"> | ||
<schemas> | ||
<schema name="schema1" /> | ||
<schema name="schema2" /> | ||
</schemas> | ||
<environments> | ||
<dbEnvironment name="unittestrefh2" cleanBuildAllowed="true" /> | ||
<dbEnvironment name="unittestrefhsql" cleanBuildAllowed="true" /> | ||
</environments> | ||
</dbSystemConfig> |
Oops, something went wrong.