// 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"))
{