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
This commit is contained in:
Matt Ward
2011-11-21 18:26:32 +00:00
parent 772056a903
commit 1e8d689de6
2 changed files with 46 additions and 25 deletions

View File

@@ -22,6 +22,7 @@ import java.lang.reflect.Field;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; 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.Index;
import org.alfresco.util.schemacomp.model.PrimaryKey; import org.alfresco.util.schemacomp.model.PrimaryKey;
import org.alfresco.util.schemacomp.model.Schema; import org.alfresco.util.schemacomp.model.Schema;
import org.alfresco.util.schemacomp.model.Sequence;
import org.alfresco.util.schemacomp.model.Table; import org.alfresco.util.schemacomp.model.Table;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.PostgreSQLDialect; import org.hibernate.dialect.PostgreSQLDialect;
@@ -183,26 +185,13 @@ public class ExportDb
{ {
final DatabaseMetaData dbmd = con.getMetaData(); 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 String schemaName = getSchemaName(dbmd);
// 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();
schema = new Schema(schemaName); schema = new Schema(schemaName);
final ResultSet tables = dbmd.getTables(null, schemaName, namePrefixFilter(), new String[] final ResultSet tables = dbmd.getTables(null, schemaName, namePrefixFilter(), new String[]
{ {
"TABLE", "VIEW" "TABLE", "VIEW", "SEQUENCE"
}); });
@@ -216,6 +205,13 @@ public class ExportDb
continue; continue;
} }
if (tables.getString("TABLE_TYPE").equals("SEQUENCE"))
{
Sequence sequence = new Sequence(tableName);
schema.add(sequence);
continue;
}
Table table = new Table(tableName); Table table = new Table(tableName);
schema.add(table); 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. * Convert a boolean string as used in the database, to a boolean value.
* *

View File

@@ -99,7 +99,7 @@ public class ExportDbTest
Table exampleTable = null; Table exampleTable = null;
Table otherTable = null; Table otherTable = null;
Sequence authoritySeq = null; Sequence exampleSeq = null;
for (DbObject dbo : schema) for (DbObject dbo : schema)
{ {
@@ -111,17 +111,16 @@ public class ExportDbTest
{ {
otherTable = (Table) dbo; 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_"); checkResultsFiltered(schema, "export_test_");
checkExampleTable(schema, exampleTable); checkExampleTable(schema, exampleTable);
checkOtherTable(schema, otherTable); checkOtherTable(schema, otherTable);
// TODO: what to do about sequences? They can't easily be retrieved with JDBC's DatabaseMetaData checkExampleSequence(schema, exampleSeq);
//checkAuthoritySequence(schema, authoritySeq);
} }
@@ -166,8 +165,8 @@ public class ExportDbTest
"DROP INDEX IF EXISTS export_test_idx_other_2", "DROP INDEX IF EXISTS export_test_idx_other_2",
"CREATE INDEX export_test_idx_other_2 ON export_test_other (ex_id)", "CREATE INDEX export_test_idx_other_2 ON export_test_other (ex_id)",
"DROP SEQUENCE IF EXISTS export_test_authority_seq", "DROP SEQUENCE IF EXISTS export_test_example_seq",
"CREATE SEQUENCE export_test_authority_seq START WITH 1 INCREMENT BY 1" "CREATE SEQUENCE export_test_example_seq START WITH 1 INCREMENT BY 1"
}; };
TransactionTemplate tt = new TransactionTemplate(tx); 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()); assertSame("Incorrect parent or no parent set", schema, seq.getParent());
assertEquals("export_test_authority_seq", seq.getName()); assertEquals("export_test_example_seq", seq.getName());
} }
} }