mirror of https://github.com/apache/openjpa.git
OPENJPA-2581 add MappingTool option to create DROP scripts for all tables in a DB.
Thanks sreiterer for the patch! Stefan will provide further integration tests tomorrow. git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@1677780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d38f10ad7f
commit
bf22072d43
|
@ -86,6 +86,7 @@ public class MappingTool
|
||||||
public static final String ACTION_REFRESH = "refresh";
|
public static final String ACTION_REFRESH = "refresh";
|
||||||
public static final String ACTION_BUILD_SCHEMA = "buildSchema";
|
public static final String ACTION_BUILD_SCHEMA = "buildSchema";
|
||||||
public static final String ACTION_DROP = "drop";
|
public static final String ACTION_DROP = "drop";
|
||||||
|
public static final String ACTION_DROP_SCHEMA = "dropSchema";
|
||||||
public static final String ACTION_VALIDATE = "validate";
|
public static final String ACTION_VALIDATE = "validate";
|
||||||
public static final String ACTION_EXPORT = "export";
|
public static final String ACTION_EXPORT = "export";
|
||||||
public static final String ACTION_IMPORT = "import";
|
public static final String ACTION_IMPORT = "import";
|
||||||
|
@ -95,6 +96,7 @@ public class MappingTool
|
||||||
ACTION_REFRESH,
|
ACTION_REFRESH,
|
||||||
ACTION_BUILD_SCHEMA,
|
ACTION_BUILD_SCHEMA,
|
||||||
ACTION_DROP,
|
ACTION_DROP,
|
||||||
|
ACTION_DROP_SCHEMA,
|
||||||
ACTION_VALIDATE,
|
ACTION_VALIDATE,
|
||||||
ACTION_EXPORT,
|
ACTION_EXPORT,
|
||||||
ACTION_IMPORT,
|
ACTION_IMPORT,
|
||||||
|
|
|
@ -64,6 +64,7 @@ public class SchemaTool {
|
||||||
|
|
||||||
public static final String ACTION_ADD = "add";
|
public static final String ACTION_ADD = "add";
|
||||||
public static final String ACTION_DROP = "drop";
|
public static final String ACTION_DROP = "drop";
|
||||||
|
public static final String ACTION_DROP_SCHEMA = "dropSchema";
|
||||||
public static final String ACTION_RETAIN = "retain";
|
public static final String ACTION_RETAIN = "retain";
|
||||||
public static final String ACTION_REFRESH = "refresh";
|
public static final String ACTION_REFRESH = "refresh";
|
||||||
public static final String ACTION_BUILD = "build";
|
public static final String ACTION_BUILD = "build";
|
||||||
|
@ -77,6 +78,7 @@ public class SchemaTool {
|
||||||
public static final String[] ACTIONS = new String[]{
|
public static final String[] ACTIONS = new String[]{
|
||||||
ACTION_ADD,
|
ACTION_ADD,
|
||||||
ACTION_DROP,
|
ACTION_DROP,
|
||||||
|
ACTION_DROP_SCHEMA,
|
||||||
ACTION_RETAIN,
|
ACTION_RETAIN,
|
||||||
ACTION_REFRESH,
|
ACTION_REFRESH,
|
||||||
ACTION_BUILD,
|
ACTION_BUILD,
|
||||||
|
@ -341,6 +343,8 @@ public class SchemaTool {
|
||||||
add();
|
add();
|
||||||
else if (ACTION_DROP.equals(_action))
|
else if (ACTION_DROP.equals(_action))
|
||||||
drop();
|
drop();
|
||||||
|
else if (ACTION_DROP_SCHEMA.equals(_action))
|
||||||
|
dropSchema();
|
||||||
else if (ACTION_RETAIN.equals(_action))
|
else if (ACTION_RETAIN.equals(_action))
|
||||||
retain();
|
retain();
|
||||||
else if (ACTION_REFRESH.equals(_action))
|
else if (ACTION_REFRESH.equals(_action))
|
||||||
|
@ -373,6 +377,14 @@ public class SchemaTool {
|
||||||
drop(getDBSchemaGroup(false), assertSchemaGroup());
|
drop(getDBSchemaGroup(false), assertSchemaGroup());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Drops all schema components in the schema repository.
|
||||||
|
*/
|
||||||
|
protected void dropSchema()
|
||||||
|
throws SQLException {
|
||||||
|
drop(getDBSchemaGroup(false), assertSchemaGroup(), false);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drops database components that are not mentioned in the schema
|
* Drops database components that are not mentioned in the schema
|
||||||
* repository.
|
* repository.
|
||||||
|
@ -414,7 +426,7 @@ public class SchemaTool {
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
SchemaGroup group = new SchemaGroup();
|
SchemaGroup group = new SchemaGroup();
|
||||||
group.addSchema();
|
group.addSchema();
|
||||||
add(group, assertSchemaGroup());
|
buildSchema(group, assertSchemaGroup(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -465,6 +477,11 @@ public class SchemaTool {
|
||||||
*/
|
*/
|
||||||
protected void add(SchemaGroup db, SchemaGroup repos)
|
protected void add(SchemaGroup db, SchemaGroup repos)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
|
|
||||||
|
buildSchema(db, repos, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void buildSchema(SchemaGroup db, SchemaGroup repos, boolean considerDatabaseState) throws SQLException {
|
||||||
// add sequences
|
// add sequences
|
||||||
Schema[] schemas = repos.getSchemas();
|
Schema[] schemas = repos.getSchemas();
|
||||||
Schema schema;
|
Schema schema;
|
||||||
|
@ -473,7 +490,7 @@ public class SchemaTool {
|
||||||
for (int i = 0; i < schemas.length; i++) {
|
for (int i = 0; i < schemas.length; i++) {
|
||||||
seqs = schemas[i].getSequences();
|
seqs = schemas[i].getSequences();
|
||||||
for (int j = 0; j < seqs.length; j++) {
|
for (int j = 0; j < seqs.length; j++) {
|
||||||
if (db.findSequence(schemas[i], seqs[j].getQualifiedPath()) !=
|
if (considerDatabaseState && db.findSequence(schemas[i], seqs[j].getQualifiedPath()) !=
|
||||||
null)
|
null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -490,7 +507,7 @@ public class SchemaTool {
|
||||||
|
|
||||||
// order is important in this method; start with columns
|
// order is important in this method; start with columns
|
||||||
Table[] tabs;
|
Table[] tabs;
|
||||||
Table dbTable;
|
Table dbTable = null;
|
||||||
Column[] cols;
|
Column[] cols;
|
||||||
Column col;
|
Column col;
|
||||||
DBIdentifier defaultSchemaName = DBIdentifier.newSchema(_dict.getDefaultSchemaName());
|
DBIdentifier defaultSchemaName = DBIdentifier.newSchema(_dict.getDefaultSchemaName());
|
||||||
|
@ -498,7 +515,9 @@ public class SchemaTool {
|
||||||
tabs = schemas[i].getTables();
|
tabs = schemas[i].getTables();
|
||||||
for (int j = 0; j < tabs.length; j++) {
|
for (int j = 0; j < tabs.length; j++) {
|
||||||
cols = tabs[j].getColumns();
|
cols = tabs[j].getColumns();
|
||||||
dbTable = db.findTable(schemas[i], tabs[j].getQualifiedPath(), defaultSchemaName);
|
if (considerDatabaseState) {
|
||||||
|
dbTable = db.findTable(schemas[i], tabs[j].getQualifiedPath(), defaultSchemaName);
|
||||||
|
}
|
||||||
for (int k = 0; k < cols.length; k++) {
|
for (int k = 0; k < cols.length; k++) {
|
||||||
if (dbTable != null) {
|
if (dbTable != null) {
|
||||||
DBIdentifier colName = cols[k].getIdentifier();
|
DBIdentifier colName = cols[k].getIdentifier();
|
||||||
|
@ -508,11 +527,11 @@ public class SchemaTool {
|
||||||
dbTable.importColumn(cols[k]);
|
dbTable.importColumn(cols[k]);
|
||||||
else
|
else
|
||||||
_log.warn(_loc.get("add-col", cols[k],
|
_log.warn(_loc.get("add-col", cols[k],
|
||||||
tabs[j]));
|
tabs[j]));
|
||||||
} else if (!cols[k].equalsColumn(col)) {
|
} else if (!cols[k].equalsColumn(col)) {
|
||||||
_log.warn(_loc.get("bad-col", new Object[]{
|
_log.warn(_loc.get("bad-col", new Object[]{
|
||||||
col, dbTable, col.getDescription(),
|
col, dbTable, col.getDescription(),
|
||||||
cols[k].getDescription() }));
|
cols[k].getDescription() }));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -526,16 +545,18 @@ public class SchemaTool {
|
||||||
tabs = schemas[i].getTables();
|
tabs = schemas[i].getTables();
|
||||||
for (int j = 0; j < tabs.length; j++) {
|
for (int j = 0; j < tabs.length; j++) {
|
||||||
pk = tabs[j].getPrimaryKey();
|
pk = tabs[j].getPrimaryKey();
|
||||||
dbTable = db.findTable(schemas[i], tabs[j].getQualifiedPath());
|
if (considerDatabaseState) {
|
||||||
|
dbTable = db.findTable(schemas[i], tabs[j].getQualifiedPath());
|
||||||
|
}
|
||||||
if (pk != null && !pk.isLogical() && dbTable != null) {
|
if (pk != null && !pk.isLogical() && dbTable != null) {
|
||||||
if (dbTable.getPrimaryKey() == null
|
if (dbTable.getPrimaryKey() == null
|
||||||
&& addPrimaryKey(pk))
|
&& addPrimaryKey(pk))
|
||||||
dbTable.importPrimaryKey(pk);
|
dbTable.importPrimaryKey(pk);
|
||||||
else if (dbTable.getPrimaryKey() == null)
|
else if (dbTable.getPrimaryKey() == null)
|
||||||
_log.warn(_loc.get("add-pk", pk, tabs[j]));
|
_log.warn(_loc.get("add-pk", pk, tabs[j]));
|
||||||
else if (!pk.equalsPrimaryKey(dbTable.getPrimaryKey()))
|
else if (!pk.equalsPrimaryKey(dbTable.getPrimaryKey()))
|
||||||
_log.warn(_loc.get("bad-pk",
|
_log.warn(_loc.get("bad-pk",
|
||||||
dbTable.getPrimaryKey(), dbTable));
|
dbTable.getPrimaryKey(), dbTable));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -546,7 +567,7 @@ public class SchemaTool {
|
||||||
for (int i = 0; i < schemas.length; i++) {
|
for (int i = 0; i < schemas.length; i++) {
|
||||||
tabs = schemas[i].getTables();
|
tabs = schemas[i].getTables();
|
||||||
for (int j = 0; j < tabs.length; j++) {
|
for (int j = 0; j < tabs.length; j++) {
|
||||||
if (db.findTable(schemas[i], tabs[j].getQualifiedPath()) != null)
|
if (considerDatabaseState && db.findTable(schemas[i], tabs[j].getQualifiedPath()) != null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (createTable(tabs[j])) {
|
if (createTable(tabs[j])) {
|
||||||
|
@ -572,7 +593,9 @@ public class SchemaTool {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
idxs = tabs[j].getIndexes();
|
idxs = tabs[j].getIndexes();
|
||||||
dbTable = db.findTable(schemas[i], tabs[j].getQualifiedPath());
|
if (considerDatabaseState) {
|
||||||
|
dbTable = db.findTable(schemas[i], tabs[j].getQualifiedPath());
|
||||||
|
}
|
||||||
for (int k = 0; k < idxs.length; k++) {
|
for (int k = 0; k < idxs.length; k++) {
|
||||||
if (dbTable != null) {
|
if (dbTable != null) {
|
||||||
idx = findIndex(dbTable, idxs[k]);
|
idx = findIndex(dbTable, idxs[k]);
|
||||||
|
@ -581,7 +604,7 @@ public class SchemaTool {
|
||||||
dbTable.importIndex(idxs[k]);
|
dbTable.importIndex(idxs[k]);
|
||||||
else
|
else
|
||||||
_log.warn(_loc.get("add-index", idxs[k],
|
_log.warn(_loc.get("add-index", idxs[k],
|
||||||
tabs[j]));
|
tabs[j]));
|
||||||
} else if (!idxs[k].equalsIndex(idx))
|
} else if (!idxs[k].equalsIndex(idx))
|
||||||
_log.warn(_loc.get("bad-index", idx, dbTable));
|
_log.warn(_loc.get("bad-index", idx, dbTable));
|
||||||
}
|
}
|
||||||
|
@ -601,7 +624,9 @@ public class SchemaTool {
|
||||||
uniques = tabs[j].getUniques();
|
uniques = tabs[j].getUniques();
|
||||||
if (uniques == null || uniques.length == 0)
|
if (uniques == null || uniques.length == 0)
|
||||||
continue;
|
continue;
|
||||||
dbTable = db.findTable(tabs[j]);
|
if (considerDatabaseState) {
|
||||||
|
dbTable = db.findTable(tabs[j]);
|
||||||
|
}
|
||||||
if (dbTable == null)
|
if (dbTable == null)
|
||||||
continue;
|
continue;
|
||||||
for (int k = 0; k < uniques.length; k++) {
|
for (int k = 0; k < uniques.length; k++) {
|
||||||
|
@ -622,7 +647,9 @@ public class SchemaTool {
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
fks = tabs[j].getForeignKeys();
|
fks = tabs[j].getForeignKeys();
|
||||||
dbTable = db.findTable(schemas[i],tabs[j].getQualifiedPath());
|
if (considerDatabaseState) {
|
||||||
|
dbTable = db.findTable(schemas[i], tabs[j].getQualifiedPath());
|
||||||
|
}
|
||||||
for (int k = 0; k < fks.length; k++) {
|
for (int k = 0; k < fks.length; k++) {
|
||||||
if (!fks[k].isLogical() && dbTable != null) {
|
if (!fks[k].isLogical() && dbTable != null) {
|
||||||
fk = findForeignKey(dbTable, fks[k]);
|
fk = findForeignKey(dbTable, fks[k]);
|
||||||
|
@ -631,7 +658,7 @@ public class SchemaTool {
|
||||||
dbTable.importForeignKey(fks[k]);
|
dbTable.importForeignKey(fks[k]);
|
||||||
else
|
else
|
||||||
_log.warn(_loc.get("add-fk",
|
_log.warn(_loc.get("add-fk",
|
||||||
fks[k], tabs[j]));
|
fks[k], tabs[j]));
|
||||||
} else if (!fks[k].equalsForeignKey(fk))
|
} else if (!fks[k].equalsForeignKey(fk))
|
||||||
_log.warn(_loc.get("bad-fk", fk, dbTable));
|
_log.warn(_loc.get("bad-fk", fk, dbTable));
|
||||||
}
|
}
|
||||||
|
@ -767,10 +794,16 @@ public class SchemaTool {
|
||||||
dropTables(drops, db);
|
dropTables(drops, db);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void drop(SchemaGroup db, SchemaGroup repos)
|
||||||
|
throws SQLException {
|
||||||
|
|
||||||
|
drop(db, repos, true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Drops all database components in the given repository schema.
|
* Drops all database components in the given repository schema.
|
||||||
*/
|
*/
|
||||||
protected void drop(SchemaGroup db, SchemaGroup repos)
|
private void drop(SchemaGroup db, SchemaGroup repos, boolean considerDatabaseState)
|
||||||
throws SQLException {
|
throws SQLException {
|
||||||
// drop sequences
|
// drop sequences
|
||||||
Schema[] schemas = repos.getSchemas();
|
Schema[] schemas = repos.getSchemas();
|
||||||
|
@ -806,6 +839,12 @@ public class SchemaTool {
|
||||||
for (int j = 0; j < tabs.length; j++) {
|
for (int j = 0; j < tabs.length; j++) {
|
||||||
if (!isDroppable(tabs[j]))
|
if (!isDroppable(tabs[j]))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if (!considerDatabaseState) {
|
||||||
|
drops.add(tabs[j]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
dbTable = db.findTable(tabs[j]);
|
dbTable = db.findTable(tabs[j]);
|
||||||
if (dbTable == null)
|
if (dbTable == null)
|
||||||
continue;
|
continue;
|
||||||
|
@ -872,28 +911,30 @@ public class SchemaTool {
|
||||||
// drop the tables we calculated above
|
// drop the tables we calculated above
|
||||||
dropTables(drops, db);
|
dropTables(drops, db);
|
||||||
|
|
||||||
// columns
|
if (considerDatabaseState) {
|
||||||
Column[] cols;
|
// columns
|
||||||
Column col;
|
Column[] cols;
|
||||||
for (int i = 0; i < schemas.length; i++) {
|
Column col;
|
||||||
tabs = schemas[i].getTables();
|
for (int i = 0; i < schemas.length; i++) {
|
||||||
for (int j = 0; j < tabs.length; j++) {
|
tabs = schemas[i].getTables();
|
||||||
if (!isDroppable(tabs[j]))
|
for (int j = 0; j < tabs.length; j++) {
|
||||||
continue;
|
if (!isDroppable(tabs[j]))
|
||||||
cols = tabs[j].getColumns();
|
|
||||||
dbTable = db.findTable(tabs[j]);
|
|
||||||
for (int k = 0; k < cols.length; k++) {
|
|
||||||
col = null;
|
|
||||||
if (dbTable != null)
|
|
||||||
col = dbTable.getColumn(cols[k].getIdentifier());
|
|
||||||
if (dbTable == null || col == null)
|
|
||||||
continue;
|
continue;
|
||||||
|
cols = tabs[j].getColumns();
|
||||||
if (dropColumn(cols[k])) {
|
dbTable = db.findTable(tabs[j]);
|
||||||
|
for (int k = 0; k < cols.length; k++) {
|
||||||
|
col = null;
|
||||||
if (dbTable != null)
|
if (dbTable != null)
|
||||||
dbTable.removeColumn(col);
|
col = dbTable.getColumn(cols[k].getIdentifier());
|
||||||
else
|
if (dbTable == null || col == null)
|
||||||
_log.warn(_loc.get("drop-col", cols[k], tabs[j]));
|
continue;
|
||||||
|
|
||||||
|
if (dropColumn(cols[k])) {
|
||||||
|
if (dbTable != null)
|
||||||
|
dbTable.removeColumn(col);
|
||||||
|
else
|
||||||
|
_log.warn(_loc.get("drop-col", cols[k], tabs[j]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ public class OpenJpaSchemaMojo extends AbstractOpenJpaMappingToolMojo {
|
||||||
* <li>add (see MappingTool#ACTION_ADD)</li>
|
* <li>add (see MappingTool#ACTION_ADD)</li>
|
||||||
* <li>refresh (see MappingTool#ACTION_REFRESH)</li>
|
* <li>refresh (see MappingTool#ACTION_REFRESH)</li>
|
||||||
* <li>drop (see MappingTool#ACTION_DROP)</li>
|
* <li>drop (see MappingTool#ACTION_DROP)</li>
|
||||||
|
* <li>dropSchema (see MappingTool#ACTION_DROP_SCHEMA)</li>
|
||||||
* <li>buildSchema (see MappingTool#ACTION_BUILD_SCHEMA)</li>
|
* <li>buildSchema (see MappingTool#ACTION_BUILD_SCHEMA)</li>
|
||||||
* <li>import (see MappingTool#ACTION_IMPORT)</li>
|
* <li>import (see MappingTool#ACTION_IMPORT)</li>
|
||||||
* <li>export (see MappingTool#ACTION_EXPORT)</li>
|
* <li>export (see MappingTool#ACTION_EXPORT)</li>
|
||||||
|
|
|
@ -42,6 +42,7 @@ public class OpenJpaSqlMojo extends AbstractOpenJpaMappingToolMojo {
|
||||||
* <li>add (see MappingTool#ACTION_ADD)</li>
|
* <li>add (see MappingTool#ACTION_ADD)</li>
|
||||||
* <li>refresh (see MappingTool#ACTION_REFRESH)</li>
|
* <li>refresh (see MappingTool#ACTION_REFRESH)</li>
|
||||||
* <li>drop (see MappingTool#ACTION_DROP)</li>
|
* <li>drop (see MappingTool#ACTION_DROP)</li>
|
||||||
|
* <li>dropSchema (see MappingTool#ACTION_DROP_SCHEMA)</li>
|
||||||
* <li>buildSchema (see MappingTool#ACTION_BUILD_SCHEMA)</li>
|
* <li>buildSchema (see MappingTool#ACTION_BUILD_SCHEMA)</li>
|
||||||
* <li>import (see MappingTool#ACTION_IMPORT)</li>
|
* <li>import (see MappingTool#ACTION_IMPORT)</li>
|
||||||
* <li>export (see MappingTool#ACTION_EXPORT)</li>
|
* <li>export (see MappingTool#ACTION_EXPORT)</li>
|
||||||
|
|
Loading…
Reference in New Issue