From 1e8d689de6f2f55cb26e8f85d3631acc7c038a23 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Mon, 21 Nov 2011 18:26:32 +0000 Subject: [PATCH] ALF-11414: Sequences must be exported/imported (not directly supported by DatabaseMetadata) Supported by PostgreSQL - not yet verified against Oracle. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@32161 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../alfresco/util/schemacomp/ExportDb.java | 52 +++++++++++++------ .../util/schemacomp/ExportDbTest.java | 19 ++++--- 2 files changed, 46 insertions(+), 25 deletions(-) diff --git a/source/java/org/alfresco/util/schemacomp/ExportDb.java b/source/java/org/alfresco/util/schemacomp/ExportDb.java index 51ede40da2..0925f776f1 100644 --- a/source/java/org/alfresco/util/schemacomp/ExportDb.java +++ b/source/java/org/alfresco/util/schemacomp/ExportDb.java @@ -22,6 +22,7 @@ import java.lang.reflect.Field; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Map; import java.util.TreeMap; @@ -33,6 +34,7 @@ import org.alfresco.util.schemacomp.model.ForeignKey; import org.alfresco.util.schemacomp.model.Index; import org.alfresco.util.schemacomp.model.PrimaryKey; import org.alfresco.util.schemacomp.model.Schema; +import org.alfresco.util.schemacomp.model.Sequence; import org.alfresco.util.schemacomp.model.Table; import org.hibernate.dialect.Dialect; import org.hibernate.dialect.PostgreSQLDialect; @@ -183,26 +185,13 @@ public class ExportDb { final DatabaseMetaData dbmd = con.getMetaData(); - // Assume that if there are schemas, we want the one named after the connection user or the one called "dbo" (MS - // SQL hack) - String schemaName = null; - final ResultSet schemas = dbmd.getSchemas(); - while (schemas.next()) - { - final String thisSchema = schemas.getString("TABLE_SCHEM"); - if (thisSchema.equals(dbmd.getUserName()) || thisSchema.equalsIgnoreCase("dbo")) - { - schemaName = thisSchema; - break; - } - } - schemas.close(); + String schemaName = getSchemaName(dbmd); schema = new Schema(schemaName); final ResultSet tables = dbmd.getTables(null, schemaName, namePrefixFilter(), new String[] { - "TABLE", "VIEW" + "TABLE", "VIEW", "SEQUENCE" }); @@ -216,6 +205,13 @@ public class ExportDb continue; } + if (tables.getString("TABLE_TYPE").equals("SEQUENCE")) + { + Sequence sequence = new Sequence(tableName); + schema.add(sequence); + continue; + } + Table table = new Table(tableName); schema.add(table); @@ -332,6 +328,32 @@ public class ExportDb } + /** + * Assume that if there are schemas, we want the one named after the connection user + * or the one called "dbo" (MS SQL hack) + * + * @param dbmd + * @return The schema name, or null otherwise. + * @throws SQLException + */ + private String getSchemaName(final DatabaseMetaData dbmd) throws SQLException + { + String schemaName = null; + final ResultSet schemas = dbmd.getSchemas(); + while (schemas.next()) + { + final String thisSchema = schemas.getString("TABLE_SCHEM"); + if (thisSchema.equals(dbmd.getUserName()) || thisSchema.equalsIgnoreCase("dbo")) + { + schemaName = thisSchema; + break; + } + } + schemas.close(); + return schemaName; + } + + /** * Convert a boolean string as used in the database, to a boolean value. * diff --git a/source/java/org/alfresco/util/schemacomp/ExportDbTest.java b/source/java/org/alfresco/util/schemacomp/ExportDbTest.java index 998100a9fe..814da82957 100644 --- a/source/java/org/alfresco/util/schemacomp/ExportDbTest.java +++ b/source/java/org/alfresco/util/schemacomp/ExportDbTest.java @@ -99,7 +99,7 @@ public class ExportDbTest Table exampleTable = null; Table otherTable = null; - Sequence authoritySeq = null; + Sequence exampleSeq = null; for (DbObject dbo : schema) { @@ -111,17 +111,16 @@ public class ExportDbTest { otherTable = (Table) dbo; } - if (dbo.getName().equals("export_test_authority_seq")) + if (dbo.getName().equals("export_test_example_seq")) { - authoritySeq = (Sequence) dbo; + exampleSeq = (Sequence) dbo; } } checkResultsFiltered(schema, "export_test_"); checkExampleTable(schema, exampleTable); checkOtherTable(schema, otherTable); - // TODO: what to do about sequences? They can't easily be retrieved with JDBC's DatabaseMetaData - //checkAuthoritySequence(schema, authoritySeq); + checkExampleSequence(schema, exampleSeq); } @@ -166,8 +165,8 @@ public class ExportDbTest "DROP INDEX IF EXISTS export_test_idx_other_2", "CREATE INDEX export_test_idx_other_2 ON export_test_other (ex_id)", - "DROP SEQUENCE IF EXISTS export_test_authority_seq", - "CREATE SEQUENCE export_test_authority_seq START WITH 1 INCREMENT BY 1" + "DROP SEQUENCE IF EXISTS export_test_example_seq", + "CREATE SEQUENCE export_test_example_seq START WITH 1 INCREMENT BY 1" }; TransactionTemplate tt = new TransactionTemplate(tx); @@ -345,10 +344,10 @@ public class ExportDbTest } - public void checkAuthoritySequence(Schema schema, Sequence seq) + public void checkExampleSequence(Schema schema, Sequence seq) { - assertNotNull("Couldn't find sequence export_test_authority_seq", seq); + assertNotNull("Couldn't find sequence", seq); assertSame("Incorrect parent or no parent set", schema, seq.getParent()); - assertEquals("export_test_authority_seq", seq.getName()); + assertEquals("export_test_example_seq", seq.getName()); } }