Search in sources :

Example 6 with DbExport

use of org.jumpmind.symmetric.io.data.DbExport in project symmetric-ds by JumpMind.

the class DbExportImportTest method exportThenImportCsvWithBackslashes.

@Test
public void exportThenImportCsvWithBackslashes() 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();
    DbImport importCsv = new DbImport(platform);
    importCsv.setFormat(DbImport.Format.SQL);
    importCsv.importTables(getClass().getResourceAsStream("/test-dbimport-1-backslashes.sql"));
    assertCountDbImportTableRecords(1);
    DbExport export = new DbExport(platform);
    export.setFormat(Format.CSV);
    export.setNoCreateInfo(true);
    export.setNoData(false);
    String csvOutput = export.exportTables(new String[] { table.getName() });
    ISqlTemplate sqlTemplate = platform.getSqlTemplate();
    List<Row> rowsBeforeImport = sqlTemplate.query(SELECT_FROM_TEST_DB_IMPORT_1_ORDER_BY_ID);
    recreateImportTable();
    importCsv.setFormat(DbImport.Format.CSV);
    importCsv.importTables(csvOutput, table.getName());
    compareRows(table, rowsBeforeImport, sqlTemplate.query(SELECT_FROM_TEST_DB_IMPORT_1_ORDER_BY_ID));
}
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) DbImport(org.jumpmind.symmetric.io.data.DbImport) Row(org.jumpmind.db.sql.Row) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 7 with DbExport

use of org.jumpmind.symmetric.io.data.DbExport in project symmetric-ds by JumpMind.

the class DbExportImportTest method testExportTimestampWithTimeZone.

@Test
public void testExportTimestampWithTimeZone() throws Exception {
    if (createAndFillTimestampWithTimeZoneTable()) {
        ISymmetricEngine engine = getSymmetricEngine();
        DbExport export = new DbExport(engine.getDatabasePlatform());
        export.setCompatible(Compatible.POSTGRES);
        export.setFormat(Format.SQL);
        String sql = export.exportTables(new String[] { TEST_TS_W_TZ });
        final String EXPECTED_POSTGRES = "insert into \"test_ts_w_tz\"(\"id\", \"tz\") (select 1,cast('1973-06-08 07:00:00.000000 -04:00' as timestamp with time zone) where (select distinct 1 from \"test_ts_w_tz\" where  \"id\" = 1) is null);";
        Assert.assertTrue("Expected the following sql:\n" + sql + "\n\n to contain:\n" + EXPECTED_POSTGRES, sql.contains(EXPECTED_POSTGRES));
        export.setCompatible(Compatible.ORACLE);
        sql = export.exportTables(new String[] { TEST_TS_W_TZ });
        final String EXPECTED_ORACLE = "insert into \"test_ts_w_tz\" (\"id\", \"tz\") values (1,TO_TIMESTAMP_TZ('1973-06-08 07:00:00.000000 -04:00', 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM'));";
        Assert.assertTrue("Expected the following sql:\n" + sql + "\n\n to contain:\n" + EXPECTED_ORACLE, sql.contains(EXPECTED_ORACLE));
    }
}
Also used : DbExport(org.jumpmind.symmetric.io.data.DbExport) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 8 with DbExport

use of org.jumpmind.symmetric.io.data.DbExport in project symmetric-ds by JumpMind.

the class DbExportCommand method executeWithOptions.

@Override
protected boolean executeWithOptions(CommandLine line) throws Exception {
    DbExport dbExport = new DbExport(getDatabasePlatform(false));
    if (line.hasOption(OPTION_DIR)) {
        String dir = line.getOptionValue(OPTION_DIR);
        if (new File(dir).exists()) {
            dbExport.setDir(line.getOptionValue(OPTION_DIR));
        } else {
            throw new ParseException(String.format("The directory you chose, {}, does not exist", dir));
        }
    }
    if (line.hasOption(OPTION_FORMAT)) {
        dbExport.setFormat(Format.valueOf(line.getOptionValue(OPTION_FORMAT).toUpperCase()));
        if (dbExport.getFormat() == Format.CSV && line.getArgs().length > 1 && StringUtils.isBlank(dbExport.getDir())) {
            throw new ParseException("When exporting multiple tables to CSV format you must designate a directory where the files will be written");
        }
    }
    if (line.hasOption(OPTION_COMPATIBLE)) {
        try {
            dbExport.setCompatible(Compatible.valueOf(line.getOptionValue(OPTION_COMPATIBLE).toUpperCase()));
        } catch (IllegalArgumentException ex) {
            throw new SymmetricException("Invalid compatible database option: %s", line.getOptionValue(OPTION_COMPATIBLE));
        }
    }
    if (line.hasOption(OPTION_ADD_DROP_TABLE)) {
        dbExport.setAddDropTable(true);
    }
    if (line.hasOption(OPTION_NO_CREATE_INFO)) {
        dbExport.setNoCreateInfo(true);
    }
    if (line.hasOption(OPTION_NO_INDICES)) {
        dbExport.setNoIndices(true);
    }
    if (line.hasOption(OPTION_NO_FOREIGN_KEYS)) {
        dbExport.setNoForeignKeys(true);
    }
    if (line.hasOption(OPTION_NO_DATA)) {
        dbExport.setNoData(true);
    }
    if (line.hasOption(OPTION_USE_VARIABLE_DATES)) {
        dbExport.setUseVariableForDates(true);
    }
    if (line.hasOption(OPTION_USE_JDBC_TIMESTAMP_FORMAT)) {
        dbExport.setUseJdbcTimestampFormat("true".equalsIgnoreCase(line.getOptionValue(OPTION_USE_JDBC_TIMESTAMP_FORMAT)));
    }
    if (line.hasOption(OPTION_NO_QUALIFIERS)) {
        dbExport.setUseQuotedIdentifiers(false);
    }
    if (line.hasOption(OPTION_COMMENTS)) {
        dbExport.setComments(true);
    }
    if (line.hasOption(OPTION_SCHEMA)) {
        dbExport.setSchema(line.getOptionValue(OPTION_SCHEMA));
    }
    if (line.hasOption(OPTION_CATALOG)) {
        dbExport.setCatalog(line.getOptionValue(OPTION_CATALOG));
    }
    if (line.hasOption(OPTION_WHERE)) {
        dbExport.setWhereClause(line.getOptionValue(OPTION_WHERE));
    }
    String[] args = line.getArgs();
    if (line.hasOption(OPTION_SQL)) {
        if (args.length != 1) {
            throw new ParseException("When specifying a SQL statement, a table name argument must be provided.");
        }
        dbExport.exportTable(System.out, args[0], line.getOptionValue(OPTION_SQL));
    } else if (args.length == 0) {
        dbExport.exportTables(System.out);
    } else {
        dbExport.exportTables(System.out, args);
    }
    return true;
}
Also used : DbExport(org.jumpmind.symmetric.io.data.DbExport) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 9 with DbExport

use of org.jumpmind.symmetric.io.data.DbExport in project symmetric-ds by JumpMind.

the class DbExportImportTest method exportTestDatabaseSQL.

@Test
public void exportTestDatabaseSQL() throws Exception {
    ISymmetricEngine engine = getSymmetricEngine();
    Table[] tables = engine.getSymmetricDialect().readSymmetricSchemaFromXml().getTables();
    DbExport export = new DbExport(engine.getDatabasePlatform());
    export.setFormat(Format.SQL);
    export.setNoCreateInfo(false);
    export.setNoData(true);
    export.setSchema(getSymmetricEngine().getSymmetricDialect().getPlatform().getDefaultSchema());
    export.setCatalog(getSymmetricEngine().getSymmetricDialect().getPlatform().getDefaultCatalog());
    export.setCompatible(Compatible.H2);
    String output = export.exportTables(tables).toLowerCase();
    Assert.assertEquals(output, 46, StringUtils.countMatches(output, "create table "));
    if (engine.getDatabasePlatform().getName().equals(DatabaseNamesConstants.INFORMIX)) {
        return;
    }
    final int EXPECTED_VARCHAR_MAX_COUNT = engine.getDatabasePlatform().getName().equals(DatabaseNamesConstants.SQLITE) ? 295 : 50;
    final String EXPECTED_VARCHAR_MAX_STRING = "varchar(" + Integer.MAX_VALUE + ")";
    final int actualVarcharMaxCount = StringUtils.countMatches(output, EXPECTED_VARCHAR_MAX_STRING);
    String msg = String.format("Expected %s, but got %s in the following output %s", EXPECTED_VARCHAR_MAX_COUNT, actualVarcharMaxCount, output);
    Assert.assertEquals(msg, EXPECTED_VARCHAR_MAX_COUNT, actualVarcharMaxCount);
}
Also used : DbExport(org.jumpmind.symmetric.io.data.DbExport) Table(org.jumpmind.db.model.Table) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 10 with DbExport

use of org.jumpmind.symmetric.io.data.DbExport in project symmetric-ds by JumpMind.

the class DbExportImportTest method exportNullTimestampToCsv.

@Test
public void exportNullTimestampToCsv() throws Exception {
    ISymmetricEngine engine = getSymmetricEngine();
    IDatabasePlatform platform = engine.getDatabasePlatform();
    Table table = new Table("test_null_timestamp");
    table.addColumn(new Column("a", false, Types.TIMESTAMP, -1, -1));
    table.addColumn(new Column("b", false, Types.TIMESTAMP, -1, -1));
    platform.alterCaseToMatchDatabaseDefaultCase(table);
    platform.createTables(true, false, table);
    platform.getSqlTemplate().update("insert into test_null_timestamp values(null, null)");
    DbExport export = new DbExport(platform);
    export.setNoCreateInfo(true);
    export.setFormat(Format.CSV);
    String csv = export.exportTables(new Table[] { table });
    Assert.assertEquals("\"A\",\"B\"" + System.getProperty("line.separator") + ",", csv.trim().toUpperCase());
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DbExport(org.jumpmind.symmetric.io.data.DbExport) Table(org.jumpmind.db.model.Table) Column(org.jumpmind.db.model.Column) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Aggregations

DbExport (org.jumpmind.symmetric.io.data.DbExport)11 AbstractServiceTest (org.jumpmind.symmetric.service.impl.AbstractServiceTest)9 Test (org.junit.Test)9 Table (org.jumpmind.db.model.Table)7 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)6 Database (org.jumpmind.db.model.Database)4 DbImport (org.jumpmind.symmetric.io.data.DbImport)4 File (java.io.File)3 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)3 Row (org.jumpmind.db.sql.Row)2 DbFill (org.jumpmind.symmetric.io.data.DbFill)2 FileOutputStream (java.io.FileOutputStream)1 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ThreadInfo (java.lang.management.ThreadInfo)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Properties (java.util.Properties)1