Search in sources :

Example 1 with DbFill

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);
    }
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DbExport(org.jumpmind.symmetric.io.data.DbExport) Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database) DbImport(org.jumpmind.symmetric.io.data.DbImport) DbFill(org.jumpmind.symmetric.io.data.DbFill) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 2 with DbFill

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
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DbExport(org.jumpmind.symmetric.io.data.DbExport) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) Table(org.jumpmind.db.model.Table) Database(org.jumpmind.db.model.Database) Row(org.jumpmind.db.sql.Row) DbImport(org.jumpmind.symmetric.io.data.DbImport) DbFill(org.jumpmind.symmetric.io.data.DbFill) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 3 with DbFill

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;
}
Also used : Table(org.jumpmind.db.model.Table) IParameterService(org.jumpmind.symmetric.service.IParameterService) DbFill(org.jumpmind.symmetric.io.data.DbFill)

Aggregations

Table (org.jumpmind.db.model.Table)3 DbFill (org.jumpmind.symmetric.io.data.DbFill)3 Database (org.jumpmind.db.model.Database)2 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)2 DbExport (org.jumpmind.symmetric.io.data.DbExport)2 DbImport (org.jumpmind.symmetric.io.data.DbImport)2 AbstractServiceTest (org.jumpmind.symmetric.service.impl.AbstractServiceTest)2 Test (org.junit.Test)2 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)1 Row (org.jumpmind.db.sql.Row)1 IParameterService (org.jumpmind.symmetric.service.IParameterService)1