use of org.jumpmind.symmetric.io.data.DbFill in project symmetric-ds by JumpMind.
the class DbExportImportTest method exportUniqueKeywordSqliteTable.
@Test
public void exportUniqueKeywordSqliteTable() throws Exception {
String dbName = engine.getDatabasePlatform().getName();
if (dbName.equals(DatabaseNamesConstants.SQLITE)) {
ISymmetricEngine engine = getSymmetricEngine();
DbImport dbImport = new DbImport(engine.getDatabasePlatform());
dbImport.setFormat(DbImport.Format.XML);
dbImport.setDropIfExists(true);
dbImport.setAlterCaseToMatchDatabaseDefaultCase(true);
dbImport.importTables(getClass().getResourceAsStream("/test-dbimport-unique.xml"));
IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
Database testTables = platform.readDatabaseFromXml("/test-dbimport-unique.xml", true);
Table table = testTables.findTable("test_db_import_unique", false);
Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
Assert.assertEquals(table.getColumnWithName("string_required_value").isUnique(), true);
final int RECORD_COUNT = 100;
DbFill fill = new DbFill(platform);
fill.setRecordCount(RECORD_COUNT);
fill.fillTables(table.getName());
Assert.assertEquals(RECORD_COUNT, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
DbExport export = new DbExport(platform);
export.setFormat(Format.XML);
export.setNoCreateInfo(false);
export.setNoData(true);
String xmlOutput = export.exportTables(new String[] { table.getName() });
dbImport.importTables(xmlOutput);
table = testTables.findTable("test_db_import_unique", false);
Assert.assertEquals(0, platform.getSqlTemplate().queryForInt("select count(*) from " + table.getName()));
Assert.assertEquals(table.getColumnWithName("string_required_value").isUnique(), true);
}
}
use of org.jumpmind.symmetric.io.data.DbFill in project symmetric-ds by JumpMind.
the class DbExportImportTest method exportThenImportCsv.
@Test
public void exportThenImportCsv() throws Exception {
ISymmetricEngine engine = getSymmetricEngine();
IDatabasePlatform platform = engine.getSymmetricDialect().getPlatform();
Database testTables = platform.readDatabaseFromXml("/test-dbimport.xml", true);
Table table = testTables.findTable("test_db_import_1", false);
recreateImportTable();
final int RECORD_COUNT = 100;
DbFill fill = new DbFill(platform);
fill.setRecordCount(RECORD_COUNT);
fill.fillTables(table.getName());
DbExport export = new DbExport(platform);
export.setFormat(Format.CSV);
export.setNoCreateInfo(true);
export.setNoData(false);
String csvOutput = export.exportTables(new String[] { table.getName() });
logger.info(csvOutput);
ISqlTemplate sqlTemplate = platform.getSqlTemplate();
List<Row> rowsBeforeImport = sqlTemplate.query(SELECT_FROM_TEST_DB_IMPORT_1_ORDER_BY_ID);
recreateImportTable();
DbImport importCsv = new DbImport(platform);
importCsv.setFormat(DbImport.Format.CSV);
importCsv.importTables(csvOutput, table.getName());
Assert.assertEquals(RECORD_COUNT, sqlTemplate.queryForInt("select count(*) from " + table.getName()));
compareRows(table, rowsBeforeImport, sqlTemplate.query(SELECT_FROM_TEST_DB_IMPORT_1_ORDER_BY_ID));
// TODO test error
// TODO test replace
// TODO test ignore
// TODO test force
}
use of org.jumpmind.symmetric.io.data.DbFill in project symmetric-ds by JumpMind.
the class DbFillCommand method executeWithOptions.
@Override
protected boolean executeWithOptions(CommandLine line) throws Exception {
DbFill dbFill = new DbFill(getDatabasePlatform(false));
if (line.hasOption(OPTION_SCHEMA)) {
dbFill.setSchema(line.getOptionValue(OPTION_SCHEMA));
}
if (line.hasOption(OPTION_CATALOG)) {
dbFill.setCatalog(line.getOptionValue(OPTION_CATALOG));
}
if (line.hasOption(OPTION_COUNT)) {
dbFill.setRecordCount(Integer.parseInt(line.getOptionValue(OPTION_COUNT)));
}
if (line.hasOption(OPTION_CASCADE)) {
dbFill.setCascading(true);
}
if (line.hasOption(OPTION_CASCADE_SELECT)) {
dbFill.setCascadingSelect(true);
}
if (line.hasOption(OPTION_INTERVAL)) {
dbFill.setInterval(Integer.parseInt(line.getOptionValue(OPTION_INTERVAL)));
}
if (line.hasOption(OPTION_WEIGHTS)) {
int[] dmlWeight = { 0, 0, 0 };
String[] strWeight = line.getOptionValue(OPTION_WEIGHTS).split(",");
if (strWeight != null && strWeight.length == 3) {
for (int i = 0; i < 3; i++) {
dmlWeight[i] = new Integer(strWeight[i]);
}
dbFill.setDmlWeight(dmlWeight);
}
}
if (line.hasOption(OPTION_DEBUG)) {
dbFill.setDebug(true);
}
if (line.hasOption(OPTION_VERBOSE_CONSOLE)) {
dbFill.setVerbose(true);
}
String[] ignore = null;
if (line.hasOption(OPTION_IGNORE_TABLES)) {
ignore = line.getOptionValue(OPTION_IGNORE_TABLES).split(",");
}
String[] prefixed = null;
if (line.hasOption(OPTION_PREFIXED_TABLES)) {
prefixed = line.getOptionValue(OPTION_PREFIXED_TABLES).split(",");
}
if (line.hasOption(OPTION_CONTINUE)) {
dbFill.setContinueOnError(true);
}
if (line.hasOption(OPTION_PRINT)) {
dbFill.setPrint(true);
}
if (line.hasOption(OPTION_RAND)) {
dbFill.setUseRandomCount(true);
}
if (line.hasOption(OPTION_REPEAT)) {
dbFill.setRepeat(Integer.parseInt(line.getOptionValue(OPTION_REPEAT)));
}
if (line.hasOption(OPTION_COMMIT)) {
dbFill.setMaxRowsCommit(Integer.parseInt(line.getOptionValue(OPTION_COMMIT)));
}
if (line.hasOption(OPTION_COMMIT_DELAY)) {
dbFill.setCommitDelay(Integer.parseInt(line.getOptionValue(OPTION_COMMIT_DELAY)));
}
if (line.hasOption(OPTION_ROLLBACK)) {
dbFill.setPercentRollback(Integer.parseInt(line.getOptionValue(OPTION_ROLLBACK)));
}
// Ignore the Symmetric config tables.
getSymmetricEngine();
IParameterService parameterService = engine.getParameterService();
String cfgPrefix = parameterService.getString(ParameterConstants.RUNTIME_CONFIG_TABLE_PREFIX);
dbFill.setIgnore((String[]) ArrayUtils.add(ignore, cfgPrefix));
dbFill.setPrefixed(prefixed);
Map<String, int[]> tableProperties = parseTableProperties();
// If tables are provided in the property file, ignore the tables provided at the command line.
String[] tableNames = null;
if (tableProperties.size() != 0) {
tableNames = tableProperties.keySet().toArray(new String[0]);
} else {
tableNames = line.getArgs();
}
if (!dbFill.getPrint()) {
dbFill.fillTables(tableNames, tableProperties);
} else {
for (String tableName : tableNames) {
Table table = platform.readTableFromDatabase(dbFill.getCatalogToUse(), dbFill.getSchemaToUse(), tableName);
if (table != null) {
for (int i = 0; i < dbFill.getRecordCount(); i++) {
for (int j = 0; j < dbFill.getInsertWeight(); j++) {
String sql = dbFill.createDynamicRandomInsertSql(table);
System.out.println(sql);
}
for (int j = 0; j < dbFill.getUpdateWeight(); j++) {
String sql = dbFill.createDynamicRandomUpdateSql(table);
System.out.println(sql);
}
for (int j = 0; j < dbFill.getDeleteWeight(); j++) {
String sql = dbFill.createDynamicRandomDeleteSql(table);
System.out.println(sql);
}
}
}
}
}
return true;
}
Aggregations