public class DatabaseUtils extends Object
Currently, we use Flyway DB (http://flywaydb.org/) for database management.
DatabaseUtils| Modifier and Type | Field and Description |
|---|---|
static String |
DBMS_H2 |
static String |
DBMS_ORACLE |
static String |
DBMS_POSTGRES |
| Constructor and Description |
|---|
DatabaseUtils() |
| Modifier and Type | Method and Description |
|---|---|
static String |
canonicalize(Connection connection,
String dbIdentifier)
Return the canonical name for a database identifier based on whether this
database defaults to storing identifiers in uppercase or lowercase.
|
static void |
checkReindexDiscovery(IndexingService indexer)
Method to check whether we need to reindex in Discovery (i.e.
|
static void |
clearFlywayDBCache()
In case of a unit test the flyway db is cached to long leading to exceptions, we need to clear the object
|
static void |
executeSql(Connection connection,
String sqlToExecute)
Execute a block of SQL against the current database connection.
|
static Double |
getCurrentFlywayDSpaceState(Connection connection) |
static String |
getCurrentFlywayState(Connection connection) |
protected static DataSource |
getDataSource()
Get a reference to the configured DataSource (which can be used to
initialize the database using Flyway).
|
static String |
getDbType(Connection connection)
Determine the type of Database, based on the DB connection.
|
static boolean |
getReindexDiscovery()
Whether or not reindexing is required in Discovery.
|
static String |
getSchemaName(Connection connection)
Get the Database Schema Name in use by this Connection, so that it can
be used to limit queries in other methods (e.g.
|
static void |
main(String[] argv)
Commandline tools for managing database changes, etc.
|
static boolean |
sequenceExists(Connection connection,
String sequenceName) |
static void |
setReindexDiscovery(boolean reindex)
Whether or not to tell Discovery to reindex itself based on the updated
database.
|
static boolean |
tableColumnExists(Connection connection,
String tableName,
String columnName)
Determine if a particular database column exists in our database
|
static boolean |
tableExists(Connection connection,
String tableName)
Determine if a particular database table exists in our database
|
static boolean |
tableExists(Connection connection,
String tableName,
boolean caseSensitive)
Determine if a particular database table exists in our database
|
static void |
updateDatabase()
Ensures the current database is up-to-date with regards
to the latest DSpace DB schema.
|
protected static void |
updateDatabase(DataSource datasource,
Connection connection)
Ensures the current database is up-to-date with regards
to the latest DSpace DB schema.
|
protected static void |
updateDatabase(DataSource datasource,
Connection connection,
String targetVersion,
boolean outOfOrder)
Ensures the current database is up-to-date with regards
to the latest DSpace DB schema.
|
public static final String DBMS_POSTGRES
public static final String DBMS_ORACLE
public static final String DBMS_H2
public static void main(String[] argv)
argv - public static void updateDatabase()
throws SQLException
FlywayDB (http://flywaydb.org/) is used to perform database migrations. If a Flyway DB migration fails it will be rolled back to the last successful migration, and any errors will be logged.
SQLException - if database error
If database cannot be upgraded.protected static void updateDatabase(DataSource datasource, Connection connection) throws SQLException
FlywayDB (http://flywaydb.org/) is used to perform database migrations. If a Flyway DB migration fails it will be rolled back to the last successful migration, and any errors will be logged.
datasource - DataSource object (retrieved from DatabaseManager())connection - Database connectionSQLException - if database error
If database cannot be upgraded.protected static void updateDatabase(DataSource datasource, Connection connection, String targetVersion, boolean outOfOrder) throws SQLException
FlywayDB (http://flywaydb.org/) is used to perform database migrations. If a Flyway DB migration fails it will be rolled back to the last successful migration, and any errors will be logged.
datasource - DataSource object (retrieved from DatabaseManager())connection - Database connectiontargetVersion - If specified, only migrate the database to a particular *version* of DSpace. This is mostly just useful for testing.
If null, the database is migrated to the latest version.outOfOrder - If true, Flyway will run any lower version migrations that were previously "ignored".
If false, Flyway will only run new migrations with a higher version number.SQLException - if database error
If database cannot be upgraded.public static boolean tableExists(Connection connection, String tableName)
connection - Current Database ConnectiontableName - The name of the tablepublic static boolean tableExists(Connection connection, String tableName, boolean caseSensitive)
connection - Current Database ConnectiontableName - The name of the tablecaseSensitive - When "true", the case of the tableName will not be changed.
When "false, the name may be uppercased or lowercased based on DB type.public static boolean tableColumnExists(Connection connection, String tableName, String columnName)
connection - Current Database ConnectiontableName - The name of the tablecolumnName - The name of the column in the tablepublic static boolean sequenceExists(Connection connection, String sequenceName)
public static void executeSql(Connection connection, String sqlToExecute) throws SQLException
The SQL is executed using the Flyway SQL parser.
connection - Current Database ConnectionsqlToExecute - The actual SQL to execute as a StringSQLException - if database error
If a database error occurspublic static String getSchemaName(Connection connection) throws SQLException
connection - Current Database ConnectionSQLExceptionpublic static String canonicalize(Connection connection, String dbIdentifier) throws SQLException
connection - Current Database ConnectiondbIdentifier - Identifier to canonicalize (may be a table name, column name, etc)SQLExceptionpublic static void setReindexDiscovery(boolean reindex)
Whenever a DB migration occurs this is set to "true" to ensure the Discovery index is updated. When Discovery initializes it calls checkReindexDiscovery() to reindex if this flag is true.
Because the DB migration may be initialized by commandline or any one of the many DSpace webapps, setting this to "true" actually writes a temporary file which lets Solr know when reindex is needed.
reindex - true or falsepublic static boolean getReindexDiscovery()
Because the DB migration may be initialized by commandline or any one of the many DSpace webapps, this checks for the existence of a temporary file to know when Discovery/Solr needs reindexing.
public static void checkReindexDiscovery(IndexingService indexer)
This method is called by Discovery whenever it initializes a connection to Solr.
indexer - The actual indexer to use to reindex Discovery, if neededSolrServiceImplpublic static String getDbType(Connection connection) throws SQLException
connection - current DB ConnectionSQLException - if database errorprotected static DataSource getDataSource()
This is NOT public, as we discourage direct connections to the database which bypass Hibernate. Only Flyway should be allowed a direct connection.
public static void clearFlywayDBCache()
public static String getCurrentFlywayState(Connection connection) throws SQLException
SQLExceptionpublic static Double getCurrentFlywayDSpaceState(Connection connection) throws SQLException
SQLExceptionCopyright © 2016 DuraSpace. All rights reserved.