mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
Fixes so that Unit Tests just use Flyway DB as well
This commit is contained in:
@@ -1614,15 +1614,20 @@ public class DatabaseManager
|
||||
flyway.setDataSource(dataSource);
|
||||
flyway.setEncoding("UTF-8");
|
||||
|
||||
// Migration scripts are based on DBMS Keyword. But, for H2 we use the Oracle scripts
|
||||
String scriptFolder = dbms_keyword;
|
||||
if(dbms_keyword.equals("h2"))
|
||||
scriptFolder = "oracle";
|
||||
|
||||
// Set location where Flyway will load DB scripts from (based on DB Type)
|
||||
// e.g. [dspace.dir]/etc/[dbtype]/
|
||||
String scriptPath = ConfigurationManager.getProperty("dspace.dir") +
|
||||
System.getProperty("file.separator") + "etc" +
|
||||
System.getProperty("file.separator") + dbms_keyword;
|
||||
|
||||
System.getProperty("file.separator") + "migrations" +
|
||||
System.getProperty("file.separator") + scriptFolder;
|
||||
|
||||
log.info("Loading Flyway DB scripts from " + scriptPath + " and Package 'org.dspace.storage.rdbms.migration.*'");
|
||||
flyway.setLocations("filesystem:" + scriptPath + ",classpath:org.dspace.storage.rdbms.migration");
|
||||
flyway.setLocations("filesystem:" + scriptPath, "classpath:org.dspace.storage.rdbms.migration");
|
||||
|
||||
// Get our Database migration status, so we know what to tell Flyway to do
|
||||
int status = getDbMigrationStatus(schema, connection, flyway);
|
||||
@@ -1719,6 +1724,7 @@ public class DatabaseManager
|
||||
// IF we get here, we have 4.0 or above compatible database and Flyway is already installed
|
||||
return STATUS_FLYWAY;
|
||||
}
|
||||
|
||||
/**
|
||||
* What is the name of our DBMS?
|
||||
*
|
||||
|
@@ -126,8 +126,6 @@ public class AbstractUnitTest
|
||||
|
||||
// Applies/initializes our mock database by invoking its constructor:
|
||||
new MockDatabaseManager();
|
||||
// Now, initialize our Database itself by populating it
|
||||
initializeDB();
|
||||
|
||||
// Also initialize these mock classes for general use
|
||||
new MockIndexEventConsumer();
|
||||
@@ -419,88 +417,4 @@ public class AbstractUnitTest
|
||||
if(c!=null)
|
||||
c = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the database tables by running the schema SQL specified
|
||||
* in the 'db.schema.path' system property
|
||||
*/
|
||||
private static void initializeDB() throws SQLException
|
||||
{
|
||||
if(!dbInitialized)
|
||||
{
|
||||
String schemaPath = "";
|
||||
Connection dbConnection = null;
|
||||
try
|
||||
{
|
||||
//preload the contents of the database
|
||||
String s = new String();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
schemaPath = System.getProperty("db.schema.path");
|
||||
if (schemaPath == null)
|
||||
throw new IllegalArgumentException(
|
||||
"System property db.schema.path must be defined");
|
||||
|
||||
log.debug("Preloading Unit Test database from " + schemaPath);
|
||||
|
||||
FileReader fr = new FileReader(new File(schemaPath));
|
||||
BufferedReader br = new BufferedReader(fr);
|
||||
|
||||
while((s = br.readLine()) != null)
|
||||
{
|
||||
//we skip white lines and comments
|
||||
if(!"".equals(s.trim()) && !s.trim().startsWith("--"))
|
||||
{
|
||||
sb.append(s);
|
||||
}
|
||||
}
|
||||
br.close();
|
||||
|
||||
//we use ";" as a delimiter for each request. This assumes no triggers
|
||||
//nor other calls besides CREATE TABLE, CREATE SEQUENCE and INSERT
|
||||
//exist in the file
|
||||
String[] stmts = sb.toString().split(";");
|
||||
|
||||
// Get a new database connection. This also initializes underlying DatabaseManager object
|
||||
dbConnection = DatabaseManager.getConnection();
|
||||
Statement st = dbConnection.createStatement();
|
||||
|
||||
for(int i = 0; i<stmts.length; i++)
|
||||
{
|
||||
// we ensure that there is no spaces before or after the request string
|
||||
// in order to not execute empty statements
|
||||
if(!stmts[i].trim().equals(""))
|
||||
{
|
||||
st.executeUpdate(stmts[i]);
|
||||
log.debug("Loading into database: "+stmts[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Commit all DB changes & mark DB as initialized
|
||||
dbConnection.commit();
|
||||
dbInitialized = true;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
// Re-throw as a SQL exception... but note that it's a problem with the Schema file
|
||||
throw new SQLException("Unable to create test database from file '" + schemaPath + "'", e);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (dbConnection!=null)
|
||||
{
|
||||
try
|
||||
{
|
||||
// close out our open DB connection
|
||||
dbConnection.close();
|
||||
}
|
||||
catch(SQLException se)
|
||||
{
|
||||
//ignore if we cannot close
|
||||
}
|
||||
dbConnection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -473,7 +473,7 @@ CREATE INDEX workspace_coll_fk_idx ON WorkspaceItem(collection_id);
|
||||
CREATE TABLE WorkflowItem
|
||||
(
|
||||
workflow_id INTEGER PRIMARY KEY,
|
||||
item_id INTEGER REFERENCES Item(item_id) UNIQUE,
|
||||
item_id INTEGER UNIQUE REFERENCES Item(item_id),
|
||||
collection_id INTEGER REFERENCES Collection(collection_id),
|
||||
state INTEGER,
|
||||
owner INTEGER REFERENCES EPerson(eperson_id),
|
||||
|
Reference in New Issue
Block a user