Search in sources :

Example 76 with Handle

use of jnc.platform.win32.Handle in project SpinalTap by airbnb.

the class MysqlSchemaDatabase method dropDatabase.

void dropDatabase(@NotNull final String database) {
    log.info("Dropping database: {}", database);
    try (Handle handle = jdbi.open()) {
        disableForeignKeyChecks(handle);
        MysqlSchemaUtil.VOID_RETRYER.call(() -> {
            MysqlSchemaUtil.executeSQL(handle, null, String.format(DROP_DATABASES_QUERY, MysqlSchemaUtil.escapeBackQuote(getSchemaDatabaseName(source, database))));
            return null;
        });
    } catch (Exception ex) {
        log.error(String.format("Failed to drop database %s. (Exception: %s)", database, ex));
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
}
Also used : Handle(org.skife.jdbi.v2.Handle)

Example 77 with Handle

use of jnc.platform.win32.Handle in project SpinalTap by airbnb.

the class MysqlSchemaStore method getAll.

@Override
public Table<String, String, TreeMap<Integer, MysqlTableSchema>> getAll() {
    Table<String, String, TreeMap<Integer, MysqlTableSchema>> allSchemaTable = Tables.newCustomTable(Maps.newHashMap(), Maps::newHashMap);
    List<String> allSchemaInfo;
    try (Handle handle = jdbi.open()) {
        allSchemaInfo = MysqlSchemaUtil.LIST_STRING_RETRYER.call(() -> handle.createQuery(String.format(GET_ALL_SCHEMA_QUERY, source)).map(StringColumnMapper.INSTANCE).list());
    } catch (Exception ex) {
        log.error(String.format("Failed to get all schema for source: %s", source), ex);
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
    allSchemaInfo.stream().map(MysqlSchemaStore::deserializeSchemaInfo).forEach(schemaInfo -> {
        String database = schemaInfo.getDatabase();
        String table = schemaInfo.getTable();
        int version = schemaInfo.getVersion();
        if (!allSchemaTable.contains(database, table)) {
            allSchemaTable.put(database, table, Maps.newTreeMap());
        }
        allSchemaTable.get(database, table).put(version, schemaInfo);
    });
    return allSchemaTable;
}
Also used : Maps(com.google.common.collect.Maps) TreeMap(java.util.TreeMap) Handle(org.skife.jdbi.v2.Handle)

Example 78 with Handle

use of jnc.platform.win32.Handle in project SpinalTap by airbnb.

the class MysqlSchemaStore method archive.

public void archive() {
    try (Handle handle = jdbi.open()) {
        String archiveTableName = String.format("%s_%s", source, new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
        MysqlSchemaUtil.VOID_RETRYER.call(() -> {
            handle.execute(String.format(ARCHIVE_SCHEMA_STORE_TABLE_QUERY, source, archiveDatabase, archiveTableName));
            return null;
        });
        log.info("Schema store for {} has been archived as {}", source, archiveTableName);
    } catch (Exception ex) {
        log.error(String.format("Failed to archive schema store for source: %s. (Exception: %s)", source, ex));
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) Handle(org.skife.jdbi.v2.Handle)

Example 79 with Handle

use of jnc.platform.win32.Handle in project SpinalTap by airbnb.

the class MysqlSchemaStore method put.

@Override
public void put(@NotNull final MysqlTableSchema schema) {
    try (Handle handle = jdbi.open()) {
        MysqlSchemaUtil.VOID_RETRYER.call(() -> {
            handle.insert(String.format(PUT_SCHEMA_QUERY, source), schema.getVersion(), schema.getSource(), schema.getDatabase(), schema.getTable(), schema.getBinlogFilePos().getFileName(), schema.getBinlogFilePos().getPosition(), OBJECT_MAPPER.writeValueAsString(schema));
            return null;
        });
        metrics.schemaStorePutSuccess(schema.getDatabase(), schema.getTable());
    } catch (Exception ex) {
        log.error("Failed to put schema {}.", schema);
        metrics.schemaStorePutFailure(schema.getDatabase(), schema.getTable(), ex);
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
}
Also used : Handle(org.skife.jdbi.v2.Handle)

Example 80 with Handle

use of jnc.platform.win32.Handle in project SpinalTap by airbnb.

the class MysqlSchemaStore method getLatest.

@Override
public MysqlTableSchema getLatest(@NotNull final String database, @NotNull final String table) {
    try (Handle handle = jdbi.open()) {
        String schemaInfo = MysqlSchemaUtil.STRING_RETRYER.call(() -> handle.createQuery(String.format(GET_LATEST_SCHEMA_QUERY, source)).bind("database", database).bind("table", table).map(StringColumnMapper.INSTANCE).first());
        metrics.schemaStoreGetSuccess(database, table);
        return deserializeSchemaInfo(schemaInfo);
    } catch (Exception ex) {
        log.error(String.format("Failed to get latest schema of database: %s table: %s. Does it exist?", database, table), ex);
        metrics.schemaStoreGetFailure(database, table, ex);
        Throwables.throwIfUnchecked(ex);
        throw new RuntimeException(ex);
    }
}
Also used : Handle(org.skife.jdbi.v2.Handle)

Aggregations

Handle (org.skife.jdbi.v2.Handle)103 DBI (org.skife.jdbi.v2.DBI)28 Before (org.junit.Before)21 IOException (java.io.IOException)18 List (java.util.List)17 DataSourceFactory (io.dropwizard.db.DataSourceFactory)15 DBIFactory (io.dropwizard.jdbi.DBIFactory)15 SQLException (java.sql.SQLException)15 Map (java.util.Map)14 Test (org.junit.Test)14 Test (org.testng.annotations.Test)14 DateTime (org.joda.time.DateTime)13 ArrayList (java.util.ArrayList)11 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)11 ResultSet (java.sql.ResultSet)10 ImmutableList (com.google.common.collect.ImmutableList)8 UUID (java.util.UUID)8 CallbackFailedException (org.skife.jdbi.v2.exceptions.CallbackFailedException)7 ImmutableSet (com.google.common.collect.ImmutableSet)6 Set (java.util.Set)6