Search in sources :

Example 1 with DbExport

use of org.jumpmind.symmetric.io.data.DbExport 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 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 3 with DbExport

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

the class DbExportImportTest method exportThenImportXml.

@Test
public void exportThenImportXml() 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();
    DbExport export = new DbExport(platform);
    export.setFormat(Format.XML);
    export.setNoCreateInfo(false);
    export.setNoData(true);
    export.setSchema(getSymmetricEngine().getSymmetricDialect().getPlatform().getDefaultSchema());
    export.setCatalog(getSymmetricEngine().getSymmetricDialect().getPlatform().getDefaultCatalog());
    export.exportTables(new String[] { table.getName() });
// System.out.println(output);
// TODO validate
}
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) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 4 with DbExport

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

the class DbExportImportTest method exportTableInAnotherSchemaOnH2.

@Test
public void exportTableInAnotherSchemaOnH2() throws Exception {
    if (getPlatform().getName().equals(DatabaseNamesConstants.H2)) {
        ISymmetricEngine engine = getSymmetricEngine();
        ISqlTemplate template = getPlatform().getSqlTemplate();
        template.update("CREATE SCHEMA IF NOT EXISTS A");
        template.update("CREATE TABLE IF NOT EXISTS A.TEST (ID INT, NOTES VARCHAR(100), PRIMARY KEY (ID))");
        template.update("DELETE FROM A.TEST");
        template.update("INSERT INTO A.TEST VALUES(1,'test')");
        DbExport export = new DbExport(engine.getDatabasePlatform());
        export.setSchema("A");
        export.setFormat(Format.SQL);
        export.setNoCreateInfo(false);
        export.setNoData(false);
        export.exportTables(new String[] { "TEST" }).toLowerCase();
    // TODO validate
    }
}
Also used : ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) DbExport(org.jumpmind.symmetric.io.data.DbExport) AbstractServiceTest(org.jumpmind.symmetric.service.impl.AbstractServiceTest) Test(org.junit.Test)

Example 5 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)

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