Package org.flywaydb.core

Examples of org.flywaydb.core.Flyway


            loadConfigurationFile(properties, args);
            overrideConfiguration(properties, args);

            loadJdbcDriversAndJavaMigrations(properties);

            Flyway flyway = new Flyway();
            flyway.configure(properties);
            RepeatableFlyway.configure(flyway, properties);

            int consoleWidth = PropertiesUtils.getIntProperty(properties, "flyway.consoleWidth", 80);

            for (String operation : operations) {
View Full Code Here


  private Map<String, Flyway> flyways() {
    Map<String, Flyway> flywayMap = Maps.newHashMap();
    Map<String, DbSource> dbSourceMap = dbConfig.getAllDbSources();
    DbSource dbSource = null;
    String migrationFilesLocation = null;
    Flyway flyway = null;
    for (String dbName : dbSourceMap.keySet()) {
      migrationFilesLocation = flywayPrefixToMigrationScript + dbName;
      if (dbConfig.migrationFileDirectoryExists(migrationFilesLocation)) {
        dbSource = dbSourceMap.get(dbName);
        flyway = new Flyway();
        flyway.setDataSource(dbSource.url, dbSource.user, dbSource.password);
        flyway.setLocations(migrationFilesLocation);
        if (dbConfig.isClean(dbName)) {
          flyway.setCleanOnValidationError(true);
        }
        if (dbConfig.initOnMigrate(dbName)) {
          flyway.setInitOnMigrate(true);
        }
        flywayMap.put(dbName, flyway);
      }
    }
    return flywayMap;
View Full Code Here

        }

        if ("true".equals(applicationProperties.getProperty("dbmigrate.clean"))) {
            logger.info("clean database");

            Flyway flyway = new Flyway();
            flyway.setDataSource(dataSource);
            flyway.clean();
        }

        Collection<DatabaseMigrateInfo> databaseMigrateInfos = new DatabaseMigrateInfoBuilder(
                applicationProperties).build();

        for (DatabaseMigrateInfo databaseMigrateInfo : databaseMigrateInfos) {
            if (!databaseMigrateInfo.isEnabled()) {
                logger.info("skip migrate : {}, {}, {}",
                        databaseMigrateInfo.getName(),
                        databaseMigrateInfo.getTable(),
                        databaseMigrateInfo.getLocation());

                continue;
            }

            logger.info("migrate : {}, {}, {}", databaseMigrateInfo.getName(),
                    databaseMigrateInfo.getTable(),
                    databaseMigrateInfo.getLocation());

            Flyway flyway = new Flyway();
            flyway.setPlaceholderPrefix("$${");
            flyway.setInitOnMigrate(true);
            flyway.setInitVersion("0");
            flyway.setDataSource(dataSource);
            flyway.setTable(databaseMigrateInfo.getTable());
            flyway.setLocations(new String[] { databaseMigrateInfo
                    .getLocation() });
            flyway.migrate();
        }
    }
View Full Code Here

            // Get configured DB URL for reporting below
            String url = ConfigurationManager.getProperty("db.url");

            // Point Flyway API to our database
            Flyway flyway = setupFlyway(dataSource);

            // "test" = Test Database Connection
            if(argv[0].equalsIgnoreCase("test"))
            {
                // Try to connect to the database
                System.out.println("\nAttempting to connect to database using these configurations: ");
                System.out.println(" - URL: " + url);
                System.out.println(" - Driver: " + ConfigurationManager.getProperty("db.driver"));
                System.out.println(" - Username: " + ConfigurationManager.getProperty("db.username"));
                System.out.println(" - Password: [hidden]");
                System.out.println(" - Schema: " + ConfigurationManager.getProperty("db.schema"));
                System.out.println("\nTesting connection...");
                try
                {
                    // Just do a high level test by getting our configured DataSource and attempting to connect to it
                    // NOTE: We specifically do NOT call DatabaseManager.getConnection() because that will attempt
                    // a full initialization of DatabaseManager & also cause database migrations/upgrades to occur
                    Connection connection = dataSource.getConnection();
                    connection.close();
                }
                catch (SQLException sqle)
                {
                    System.err.println("\nError: ");
                    System.err.println(" - " + sqle);
                    System.err.println("\nPlease see the DSpace documentation for assistance.\n");
                    System.exit(1);
                }

                System.out.println("Connected successfully!\n");
            }
            // "info" = Basic Database Information
            else if(argv[0].equalsIgnoreCase("info"))
            {
                // Get basic Database info
                Connection connection = dataSource.getConnection();
                DatabaseMetaData meta = connection.getMetaData();
                System.out.println("\nDatabase URL: " + url);
                System.out.println("Database Schema: " + getSchemaName(connection));
                System.out.println("Database Software: " + meta.getDatabaseProductName() + " version " + meta.getDatabaseProductVersion());
                System.out.println("Database Driver: " + meta.getDriverName() + " version " + meta.getDriverVersion());

                // Get info table from Flyway
                System.out.println("\n" + MigrationInfoDumper.dumpToAsciiTable(flyway.info().all()));

                // If Flyway is NOT yet initialized, also print the determined version information
                // NOTE: search is case sensitive, as flyway table name is ALWAYS lowercase,
                // See: http://flywaydb.org/documentation/faq.html#case-sensitive
                if(!tableExists(connection, flyway.getTable(), true))
                {
                    System.out.println("\nNOTE: This database is NOT yet initialized for auto-migrations (via Flyway).");
                    // Determine which version of DSpace this looks like
                    String dbVersion = determineDBVersion(connection);
                    if (dbVersion!=null)
                    {
                        System.out.println("\nYour database looks to be compatible with DSpace version " + dbVersion);
                        System.out.println("All upgrades *after* version " + dbVersion + " will be run during the next migration.");
                        System.out.println("\nIf you'd like to upgrade now, simply run 'dspace database migrate'.");
                    }
                }
                connection.close();
            }
            // "migrate" = Manually run any outstanding Database migrations (if any)
            else if(argv[0].equalsIgnoreCase("migrate"))
            {
                System.out.println("\nDatabase URL: " + url);

                // "migrate" allows for an OPTIONAL second argument:
                //    - "ignored" = Also run any previously "ignored" migrations during the migration
                //    - [version] = ONLY run migrations up to a specific DSpace version (ONLY FOR TESTING)
                if(argv.length==2)
                {
                    if(argv[1].equalsIgnoreCase("ignored"))
                    {
                        System.out.println("Migrating database to latest version AND running previously \"Ignored\" migrations... (Check logs for details)");
                        Connection connection = dataSource.getConnection();
                        // Update the database to latest version, but set "outOfOrder=true"
                        // This will ensure any old migrations in the "ignored" state are now run
                        updateDatabase(dataSource, connection, null, true);
                        connection.close();
                    }
                    else
                    {
                        // Otherwise, we assume "argv[1]" is a valid migration version number
                        // This is only for testing! Never specify for Production!
                        System.out.println("Migrating database ONLY to version " + argv[1] + " ... (Check logs for details)");
                        System.out.println("\nWARNING: It is highly likely you will see errors in your logs when the Metadata");
                        System.out.println("or Bitstream Format Registry auto-update. This is because you are attempting to");
                        System.out.println("use an OLD version " + argv[1] + " Database with a newer DSpace API. NEVER do this in a");
                        System.out.println("PRODUCTION scenario. The resulting old DB is only useful for migration testing.\n");
                        Connection connection = dataSource.getConnection();
                        // Update the database, to the version specified.
                        updateDatabase(dataSource, connection, argv[1], false);
                        connection.close();
                    }
                }
                else
                {
                    System.out.println("Migrating database to latest version... (Check logs for details)");
                    // NOTE: This looks odd, but all we really need to do is ensure the
                    // DatabaseManager auto-initializes. It'll take care of the migration itself.
                    // Asking for our DB Name will ensure DatabaseManager.initialize() is called.
                    DatabaseManager.getDbName();
                }
                System.out.println("Done.");
            }
            // "repair" = Run Flyway repair script
            else if(argv[0].equalsIgnoreCase("repair"))
            {
                System.out.println("\nDatabase URL: " + url);
                System.out.println("Attempting to repair any previously failed migrations via FlywayDB... (Check logs for details)");
                flyway.repair();
                System.out.println("Done.");
            }
            // "clean" = Run Flyway clean script
            else if(argv[0].equalsIgnoreCase("clean"))
            {
View Full Code Here

        if (flywaydb==null)
        {
            try(Connection connection = datasource.getConnection())
            {
                // Initialize Flyway DB API (http://flywaydb.org/), used to perform DB migrations
                flywaydb = new Flyway();
                flywaydb.setDataSource(datasource);
                flywaydb.setEncoding("UTF-8");

                // Migration scripts are based on DBMS Keyword (see full path below)
                DatabaseMetaData meta = connection.getMetaData();
View Full Code Here

            throws SQLException
    {
        try
        {
            // Setup Flyway API against our database
            Flyway flyway = setupFlyway(datasource);

            // Set whethe Flyway will run migrations "out of order". By default, this is false,
            // and Flyway ONLY runs migrations that have a higher version number.
            flyway.setOutOfOrder(outOfOrder);

            // If a target version was specified, tell Flyway to ONLY migrate to that version
            // (i.e. all later migrations are left as "pending"). By default we always migrate to latest version.
            if(!StringUtils.isBlank(targetVersion))
            {
                flyway.setTarget(targetVersion);
            }

            // Does the necessary Flyway table ("schema_version") exist in this database?
            // If not, then this is the first time Flyway has run, and we need to initialize
            // NOTE: search is case sensitive, as flyway table name is ALWAYS lowercase,
            // See: http://flywaydb.org/documentation/faq.html#case-sensitive
            if(!tableExists(connection, flyway.getTable(), true))
            {
                // Try to determine our DSpace database version, so we know what to tell Flyway to do
                String dbVersion = determineDBVersion(connection);

                // If this is a fresh install, dbVersion will be null
                if (dbVersion==null)
                {
                    // Initialize the Flyway database table with defaults (version=1)
                    flyway.init();
                }
                else
                {
                    // Otherwise, pass our determined DB version to Flyway to initialize database table
                    flyway.setInitVersion(dbVersion);
                    flyway.setInitDescription("Initializing from DSpace " + dbVersion + " database schema");
                    flyway.init();
                }
            }

            // Determine pending Database migrations
            MigrationInfo[] pending = flyway.info().pending();

            // As long as there are pending migrations, log them and run migrate()
            if (pending!=null && pending.length>0)
            {
                log.info("Pending DSpace database schema migrations:");
                for (MigrationInfo info : pending)
                {
                    log.info("\t" + info.getVersion() + " " + info.getDescription() + " " + info.getType() + " " + info.getState());
                }

                // Run all pending Flyway migrations to ensure the DSpace Database is up to date
                flyway.migrate();

                // Flag that Discovery will need reindexing, since database was updated
                setReindexDiscovery(true);
            }
            else
View Full Code Here

    }

    @Bean(initMethod = "migrate")
    @ConfigurationProperties(prefix = "flyway")
    public Flyway flyway() {
      Flyway flyway = new Flyway();
      if (this.properties.isCreateDataSource()) {
        flyway.setDataSource(this.properties.getUrl(), this.properties.getUser(),
            this.properties.getPassword(), this.properties.getInitSqls()
                .toArray(new String[0]));
      }
      else if (this.flywayDataSource != null) {
        flyway.setDataSource(this.flywayDataSource);
      }
      else {
        flyway.setDataSource(this.dataSource);
      }
      return flyway;
    }
View Full Code Here

  public void createDataSource() throws Exception {
    EnvironmentTestUtils.addEnvironment(this.context,
        "flyway.url:jdbc:hsqldb:mem:flywaytest", "flyway.user:sa");
    registerAndRefresh(EmbeddedDataSourceConfiguration.class,
        FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    Flyway flyway = this.context.getBean(Flyway.class);
    assertNotNull(flyway.getDataSource());
  }
View Full Code Here

  @Test
  public void flywayDataSource() throws Exception {
    registerAndRefresh(FlywayDataSourceConfiguration.class,
        EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class,
        PropertyPlaceholderAutoConfiguration.class);
    Flyway flyway = this.context.getBean(Flyway.class);
    assertNotNull(flyway.getDataSource());
  }
View Full Code Here

  @Test
  public void defaultFlyway() throws Exception {
    registerAndRefresh(EmbeddedDataSourceConfiguration.class,
        FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    Flyway flyway = this.context.getBean(Flyway.class);
    assertEquals("[classpath:db/migration]", Arrays.asList(flyway.getLocations())
        .toString());
  }
View Full Code Here

TOP

Related Classes of org.flywaydb.core.Flyway

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.