Skip to content

Commit

Permalink
0003864: Snapshot table definitions from different catalog schema
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Jan 25, 2019
1 parent b6d9e04 commit a30c716
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 8 deletions.
Expand Up @@ -59,6 +59,7 @@
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
import org.jumpmind.db.model.CatalogSchema;
import org.jumpmind.db.model.Table;
import org.jumpmind.db.sql.ISqlTemplate;
import org.jumpmind.db.sql.Row;
Expand Down Expand Up @@ -135,16 +136,16 @@ public static File createSnapshot(ISymmetricEngine engine) {
log.warn("Failed to copy " + serviceConfFile.getName() + " to the snapshot directory", e);
}

TreeSet<Table> tables = new TreeSet<Table>();
FileOutputStream fos = null;
try {
HashMap<CatalogSchema, List<Table>> catalogSchemas = new HashMap<CatalogSchema, List<Table>>();
ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
List<TriggerHistory> triggerHistories = triggerRouterService.getActiveTriggerHistories();
for (TriggerHistory triggerHistory : triggerHistories) {
Table table = engine.getDatabasePlatform().getTableFromCache(triggerHistory.getSourceCatalogName(),
triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(), false);
if (table != null && !table.getName().toUpperCase().startsWith(engine.getSymmetricDialect().getTablePrefix().toUpperCase())) {
tables.add(table);
addTableToMap(catalogSchemas, new CatalogSchema(table.getCatalog(), table.getSchema()), table);
}
}

Expand All @@ -153,15 +154,35 @@ public static File createSnapshot(ISymmetricEngine engine) {
Table table = engine.getDatabasePlatform().getTableFromCache(trigger.getSourceCatalogName(), trigger.getSourceSchemaName(),
trigger.getSourceTableName(), false);
if (table != null) {
tables.add(table);
addTableToMap(catalogSchemas, new CatalogSchema(table.getCatalog(), table.getSchema()), table);
}
}

fos = new FileOutputStream(new File(tmpDir, "table-definitions.xml"));
DbExport export = new DbExport(engine.getDatabasePlatform());
export.setFormat(Format.XML);
export.setNoData(true);
export.exportTables(fos, tables.toArray(new Table[tables.size()]));
for (CatalogSchema catalogSchema : catalogSchemas.keySet()) {
DbExport export = new DbExport(engine.getDatabasePlatform());
boolean isDefaultCatalog = StringUtils.equalsIgnoreCase(catalogSchema.getCatalog(), engine.getDatabasePlatform().getDefaultCatalog());
boolean isDefaultSchema = StringUtils.equalsIgnoreCase(catalogSchema.getSchema(), engine.getDatabasePlatform().getDefaultSchema());

if (isDefaultCatalog && isDefaultSchema) {
fos = new FileOutputStream(new File(tmpDir, "table-definitions.xml"));
} else {
String extra = "";
if (!isDefaultCatalog && catalogSchema.getCatalog() != null) {
extra += catalogSchema.getCatalog() + "-";
export.setCatalog(catalogSchema.getCatalog());
}
if (!isDefaultSchema && catalogSchema.getSchema() != null) {
extra += catalogSchema.getSchema();
export.setSchema(catalogSchema.getSchema());
}
fos = new FileOutputStream(new File(tmpDir, "table-definitions-" + extra + ".xml"));
}

List<Table> tables = catalogSchemas.get(catalogSchema);
export.setFormat(Format.XML);
export.setNoData(true);
export.exportTables(fos, tables.toArray(new Table[tables.size()]));
}
} catch (Exception e) {
log.warn("Failed to export table definitions", e);
} finally {
Expand Down Expand Up @@ -699,6 +720,15 @@ public static File createThreadsFile(String parent, boolean isFiltered) {
return file;
}

private static void addTableToMap(HashMap<CatalogSchema, List<Table>> catalogSchemas, CatalogSchema catalogSchema, Table table) {
List<Table> tables = catalogSchemas.get(catalogSchema);
if (tables == null) {
tables = new ArrayList<Table>();
catalogSchemas.put(catalogSchema, tables);
}
tables.add(table);
}

static class SortedProperties extends Properties {
private static final long serialVersionUID = 1L;

Expand Down
@@ -0,0 +1,80 @@
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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 org.jumpmind.db.model;

public class CatalogSchema {

private String catalog;

private String schema;

public CatalogSchema(String catalog, String schema) {
this.catalog = catalog;
this.schema = schema;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((catalog == null) ? 0 : catalog.hashCode());
result = prime * result + ((schema == null) ? 0 : schema.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CatalogSchema other = (CatalogSchema) obj;
if (catalog == null) {
if (other.catalog != null)
return false;
} else if (!catalog.equals(other.catalog))
return false;
if (schema == null) {
if (other.schema != null)
return false;
} else if (!schema.equals(other.schema))
return false;
return true;
}

public String getCatalog() {
return catalog;
}

public void setCatalog(String catalog) {
this.catalog = catalog;
}

public String getSchema() {
return schema;
}

public void setSchema(String schema) {
this.schema = schema;
}
}

0 comments on commit a30c716

Please sign in to comment.