Class ADbMigration
- java.lang.Object
-
- com.activeviam.risk.ref.migration.ADbMigration
-
- All Implemented Interfaces:
AutoCloseable
- Direct Known Subclasses:
ActiveMonitorDbMigration
,RepositoryDbMigration
public abstract class ADbMigration extends Object implements AutoCloseable
Generic class that provides helper methods to migrate hibernate database to newer version.- Author:
- QuartetFS
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ADbMigration.DbType
List of considered data types.
-
Field Summary
Fields Modifier and Type Field Description protected Connection
connection
protected ADbMigration.DbType
dbType
protected static String
HIBERNATE_SEQUENCE
Name of the generic Hibernate sequence for ids
-
Constructor Summary
Constructors Constructor Description ADbMigration()
Constructor
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected String
changeColumnType(String table, String column, String newType)
Gets a SQL statement to change the type of given table columnvoid
close()
Closes the connection to the db.protected abstract Connection
createConnection()
Creates a connection to the database.protected String
createSequence(String name, int start)
Creates a sequence.protected String
getTypeForClob()
Gets the type name for CLOB, depending on the databose type.protected void
migrateHibernateIds(com.quartetfs.fwk.IPair<String,String>... idColumns)
Migrates all referenced id columns using the default hibernate sequence.protected void
onlyForH2()
Marks a migration operation as designed only for H2 databases.protected CallableStatement
prepareCall(String statement, String... args)
Creates aCallableStatement
object for calling database stored procedures.protected String
prepareSql(String statement, String... args)
Create the SQL statementprotected PreparedStatement
prepareStatement(String statement, String... args)
Creates aPreparedStatement
object for sending parameterized SQL statements to the database.protected String
renameColumnStatement(String table, String oldName, String newName)
Gets a SQL statement to rename a column of a particular table.protected String
renameTableStatement(String oldName, String newName)
Gets a SQL statement to rename a toble.protected String
unprefixTable(String table)
Removes newly prefixed tables to get the old name.
-
-
-
Field Detail
-
connection
protected Connection connection
-
dbType
protected ADbMigration.DbType dbType
-
HIBERNATE_SEQUENCE
protected static final String HIBERNATE_SEQUENCE
Name of the generic Hibernate sequence for ids- See Also:
- Constant Field Values
-
-
Method Detail
-
prepareSql
protected final String prepareSql(String statement, String... args)
Create the SQL statement- Parameters:
statement
- The sql statement templateargs
- The argument for the template- Returns:
- the parameterized sql statement
-
prepareStatement
protected PreparedStatement prepareStatement(String statement, String... args) throws SQLException
Creates aPreparedStatement
object for sending parameterized SQL statements to the database.This method will create the sql statement with the given
statement
template andargs
and create the appropriate PrepatedStatement object with the databse- Parameters:
statement
- The sql statement templateargs
- The argument for the template- Returns:
PreparedStatement
object- Throws:
SQLException
- if a database access error occurs or this method is called on a closed connection- See Also:
Connection.prepareCall(String)
-
prepareCall
protected CallableStatement prepareCall(String statement, String... args) throws SQLException
Creates aCallableStatement
object for calling database stored procedures.This method will create the sql statement with the given
statement
template andargs
and create the appropriate PrepatedStatement object with the databse- Parameters:
statement
- The sql statement templateargs
- The argument for the template- Returns:
- the
CallableStatement
object - Throws:
SQLException
- if a database access error occurs or this method is called on a closed connection
-
onlyForH2
protected void onlyForH2() throws MigrationException
Marks a migration operation as designed only for H2 databases.This occurs when performing complex operations, where only the sequence of operations is important.
- Throws:
MigrationException
- when called, stopping the migration
-
getTypeForClob
protected String getTypeForClob()
Gets the type name for CLOB, depending on the databose type.- Returns:
- type name
-
unprefixTable
protected String unprefixTable(String table)
Removes newly prefixed tables to get the old name.- Parameters:
table
- table name- Returns:
- old table name
-
createSequence
protected String createSequence(String name, int start)
Creates a sequence.- Parameters:
name
- sequence namestart
- sequence start value- Returns:
- SQL statement to create a sequence
-
migrateHibernateIds
protected void migrateHibernateIds(com.quartetfs.fwk.IPair<String,String>... idColumns) throws MigrationException, SQLException
Migrates all referenced id columns using the default hibernate sequence.This also removes the default value set for those columns.
- Parameters:
idColumns
- list of columns with ids, as(table, column)
- Throws:
MigrationException
- on any failureSQLException
- for invalid statement
-
renameTableStatement
protected String renameTableStatement(String oldName, String newName)
Gets a SQL statement to rename a toble.- Parameters:
oldName
- old table namenewName
- new table name- Returns:
- statement
-
renameColumnStatement
protected String renameColumnStatement(String table, String oldName, String newName)
Gets a SQL statement to rename a column of a particular table.- Parameters:
table
- table nameoldName
- old column namenewName
- new column name- Returns:
- statement
-
changeColumnType
protected String changeColumnType(String table, String column, String newType)
Gets a SQL statement to change the type of given table column- Parameters:
table
- table namecolumn
- column namenewType
- new type- Returns:
- statement
-
createConnection
protected abstract Connection createConnection()
Creates a connection to the database.- Returns:
- the connection
-
close
public void close() throws SQLException
Closes the connection to the db.- Specified by:
close
in interfaceAutoCloseable
- Throws:
SQLException
- if it fails to close the database
-
-