Search in sources :

Example 51 with Jdbi

use of org.jdbi.v3.core.Jdbi in project presto by prestodb.

the class MySqlConnectionModule method createJdbi.

public static Jdbi createJdbi(ConnectionFactory connectionFactory, MySqlFunctionNamespaceManagerConfig config) {
    Jdbi jdbi = Jdbi.create(connectionFactory).installPlugin(new SqlObjectPlugin());
    jdbi.getConfig(FunctionNamespacesTableCustomizerFactory.Config.class).setTableName(config.getFunctionNamespacesTableName());
    jdbi.getConfig(SqlFunctionsTableCustomizerFactory.Config.class).setTableName(config.getFunctionsTableName());
    jdbi.getConfig(UserDefinedTypesTableCustomizerFactory.Config.class).setTableName(config.getUserDefinedTypesTableName());
    return jdbi;
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin)

Example 52 with Jdbi

use of org.jdbi.v3.core.Jdbi in project presto by prestodb.

the class H2ConnectionModule method setup.

@Override
protected void setup(Binder binder) {
    String databaseName = buildConfigObject(H2ConnectionConfig.class).getDatabaseName();
    Jdbi jdbi = createJdbi(() -> DriverManager.getConnection(getJdbcUrl(databaseName)), buildConfigObject(MySqlFunctionNamespaceManagerConfig.class));
    binder.bind(Jdbi.class).toInstance(jdbi);
    binder.bind(HandleLifecycleManager.class).in(SINGLETON);
    binder.bind(FunctionNamespaceDao.class).toProvider(new FunctionNamespaceDaoProvider()).in(SINGLETON);
    binder.bind(new TypeLiteral<Class<? extends FunctionNamespaceDao>>() {
    }).toInstance(H2FunctionNamespaceDao.class);
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) MySqlConnectionModule.createJdbi(com.facebook.presto.functionNamespace.mysql.MySqlConnectionModule.createJdbi) MySqlFunctionNamespaceManagerConfig(com.facebook.presto.functionNamespace.mysql.MySqlFunctionNamespaceManagerConfig) TypeLiteral(com.google.inject.TypeLiteral)

Example 53 with Jdbi

use of org.jdbi.v3.core.Jdbi in project Rosetta by HubSpot.

the class AbstractJdbiTest method setup.

@Before
public void setup() {
    jdbi = Jdbi.create("jdbc:h2:mem:rosetta;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=FALSE");
    jdbi.installPlugin(new SqlObjectPlugin());
    jdbi.registerRowMapper(new RosettaRowMapperFactory());
    jdbi.useHandle(handle -> {
        handle.execute("CREATE TABLE IF NOT EXISTS test_table (id INT, name VARCHAR(255) NOT NULL, PRIMARY KEY (id))");
        handle.execute("TRUNCATE TABLE test_table");
    });
}
Also used : SqlObjectPlugin(org.jdbi.v3.sqlobject.SqlObjectPlugin) Before(org.junit.Before)

Example 54 with Jdbi

use of org.jdbi.v3.core.Jdbi in project irontest by zheng-wang.

the class UpgradeActions method upgrade.

protected void upgrade(DefaultArtifactVersion systemDatabaseVersion, DefaultArtifactVersion jarFileVersion, String ironTestHome, String fullyQualifiedSystemDBURL, String user, String password) throws Exception {
    Formatter logFormatter = new LogFormatter();
    // set formatter for console logging
    LOGGER.getParent().getHandlers()[0].setFormatter(logFormatter);
    LOGGER.info("Upgrading Iron Test from v" + systemDatabaseVersion + " to v" + jarFileVersion + ".");
    // set up temp upgrade directory
    Path upgradeWorkspace = Files.createTempDirectory("irontest-upgrade-");
    Path logFilePath = Paths.get(upgradeWorkspace.toString(), "upgrade-from-v" + systemDatabaseVersion + "-to-v" + jarFileVersion + ".log");
    FileHandler logFileHandler = new FileHandler(logFilePath.toString());
    logFileHandler.setFormatter(logFormatter);
    LOGGER.addHandler(logFileHandler);
    LOGGER.info("Created temp upgrade directory " + upgradeWorkspace.toString());
    Path oldFolderInTempUpgradeDir = Paths.get(upgradeWorkspace.toString(), "old");
    Path newFolderInTempUpgradeDir = Paths.get(upgradeWorkspace.toString(), "new");
    Files.createDirectory(oldFolderInTempUpgradeDir);
    Files.createDirectory(newFolderInTempUpgradeDir);
    // system DB upgrade includes schema change and/or data migration
    boolean needsSystemDBUpgrade = upgradeSystemDBInTempDirIfNeeded(systemDatabaseVersion, jarFileVersion, ironTestHome, fullyQualifiedSystemDBURL, user, password, oldFolderInTempUpgradeDir, newFolderInTempUpgradeDir);
    boolean clearBrowserCacheNeeded = clearBrowserCacheIfNeeded(systemDatabaseVersion, jarFileVersion);
    // ------------------------- below steps will modify files in <IronTest_Home> -------------------------
    copyFilesToBeUpgraded(ironTestHome, systemDatabaseVersion, jarFileVersion);
    deleteOldJarsFromIronTestHome(ironTestHome);
    copyNewJarFromDistToIronTestHome(jarFileVersion, ironTestHome);
    // request user to execute pre system database change (upgrade, or simply version update) general manual upgrades if needed
    preSystemDBChangeGeneralManualUpgrades(systemDatabaseVersion, jarFileVersion);
    if (needsSystemDBUpgrade) {
        // copy files from the temp 'new' folder to <IronTest_Home>
        String systemDBFileName = getSystemDBFileName(fullyQualifiedSystemDBURL);
        Path ironTestHomeSystemDatabaseFolder = Paths.get(ironTestHome, "database");
        Path sourceFilePath = Paths.get(newFolderInTempUpgradeDir.toString(), "database", systemDBFileName);
        Path targetFilePath = Paths.get(ironTestHomeSystemDatabaseFolder.toString(), systemDBFileName);
        Files.copy(sourceFilePath, targetFilePath, StandardCopyOption.REPLACE_EXISTING);
        LOGGER.info("Copied " + sourceFilePath + " to " + targetFilePath + ".");
    } else {
        // only update version of system database under <IronTest_Home>
        Jdbi jdbi = Jdbi.create(fullyQualifiedSystemDBURL, user, password);
        updateVersionTableInSystemDatabase(jdbi, fullyQualifiedSystemDBURL, jarFileVersion);
    }
    String lineDelimiter = "------------------------------------------------------------------------";
    LOGGER.info(lineDelimiter);
    LOGGER.info("UPGRADE SUCCESS");
    LOGGER.info(lineDelimiter);
    LOGGER.info("You can start Iron Test now.");
    if (clearBrowserCacheNeeded) {
        LOGGER.info("If Iron Test page is already open, refresh the page (no need to restart browser).");
    }
    LOGGER.info(lineDelimiter);
    LOGGER.info("Refer to " + logFilePath + " for upgrade logs.");
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi) Formatter(java.util.logging.Formatter) FileHandler(java.util.logging.FileHandler)

Example 55 with Jdbi

use of org.jdbi.v3.core.Jdbi in project irontest by zheng-wang.

the class UpgradeActions method upgradeSystemDBInTempDir.

private void upgradeSystemDBInTempDir(String ironTestHome, String fullyQualifiedSystemDBURL, String user, String password, List<ResourceFile> applicableSystemDBUpgrades, Path oldDir, Path newDir, DefaultArtifactVersion jarFileVersion) throws IOException {
    Path oldDatabaseFolder = Files.createDirectory(Paths.get(oldDir.toString(), "database"));
    Path newDatabaseFolder = Files.createDirectory(Paths.get(newDir.toString(), "database"));
    String systemDBFileName = getSystemDBFileName(fullyQualifiedSystemDBURL);
    Path sourceFile = Paths.get(ironTestHome, "database", systemDBFileName);
    Path targetOldFile = Paths.get(oldDatabaseFolder.toString(), systemDBFileName);
    Path targetNewFile = Paths.get(newDatabaseFolder.toString(), systemDBFileName);
    Files.copy(sourceFile, targetOldFile);
    LOGGER.info("Copied current system database to " + oldDatabaseFolder.toString());
    Files.copy(sourceFile, targetNewFile);
    LOGGER.info("Copied current system database to " + newDatabaseFolder.toString());
    String newSystemDBURL = "jdbc:h2:" + targetNewFile.toString().replace(".mv.db", "") + ";IFEXISTS=TRUE";
    Jdbi jdbi = Jdbi.create(newSystemDBURL, user, password);
    // run SQL scripts against the system database in the 'new' folder
    for (ResourceFile sqlFile : applicableSystemDBUpgrades) {
        String sqlScript = sqlFile.getResourceAsText();
        jdbi.withHandle(handle -> handle.createScript(sqlScript).execute());
        LOGGER.info("Executed SQL script " + sqlFile.getResourcePath() + " in " + newSystemDBURL + ".");
    }
    updateVersionTableInSystemDatabase(jdbi, newSystemDBURL, jarFileVersion);
}
Also used : Jdbi(org.jdbi.v3.core.Jdbi)

Aggregations

Jdbi (org.jdbi.v3.core.Jdbi)47 Test (org.junit.Test)32 Handle (org.jdbi.v3.core.Handle)12 Something (org.jdbi.v3.core.Something)6 Query (org.jdbi.v3.core.statement.Query)6 SqlObjectPlugin (org.jdbi.v3.sqlobject.SqlObjectPlugin)6 Before (org.junit.Before)6 SQLException (java.sql.SQLException)5 SomethingMapper (org.jdbi.v3.core.mapper.SomethingMapper)5 Update (org.jdbi.v3.core.statement.Update)3 BeforeClass (org.junit.BeforeClass)3 TypeLiteral (com.google.inject.TypeLiteral)2 JdbiFactory (io.dropwizard.jdbi3.JdbiFactory)2 Method (java.lang.reflect.Method)2 Stream (java.util.stream.Stream)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 SqlStatements (org.jdbi.v3.core.statement.SqlStatements)2 Test (org.junit.jupiter.api.Test)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 HealthCheckRegistry (com.codahale.metrics.health.HealthCheckRegistry)1