Class SchemaBootstrap

  • All Implemented Interfaces:
    java.util.EventListener, org.springframework.beans.factory.Aware, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener

    public class SchemaBootstrap
    extends org.springframework.extensions.surf.util.AbstractLifecycleBean
    Bootstraps the schema and schema update. The schema is considered missing if the applied patch table is not present, and the schema is considered empty if the applied patch table is empty.
    Author:
    Derek Hulley
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  SchemaBootstrap.UnclosableConnection
      A Connection wrapper that delegates all calls to the wrapped class except for the close methods, which are ignored.
    • Constructor Summary

      Constructors 
      Constructor Description
      SchemaBootstrap()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addPostCreateScriptUrl​(java.lang.String postUpdateScriptUrl)
      Register a new script for execution after the Hibernate schema creation phase.
      void addPostUpdateScriptPatch​(SchemaUpgradeScriptPatch scriptPatch)
      Register a new SQL-based patch for consideration against the instance (after Hibernate execution)
      void addPreCreateScriptUrl​(java.lang.String preCreateScriptUrl)
      Register a new script for execution when creating a clean schema.
      void addPreUpdateScriptPatch​(SchemaUpgradeScriptPatch scriptPatch)
      Register a new SQL-based patch for consideration against the instance (before Hibernate execution)
      void addUpdateActivitiScriptPatch​(SchemaUpgradeScriptPatch scriptPatch)
      Register a new SQL-based patch for consideration against the Activiti instance
      java.util.List<java.io.File> dumpSchema()
      Produces schema dump in XML format: this is performed pre- and post-upgrade (i.e.
      java.util.List<java.io.File> dumpSchema​(java.lang.String[] dbPrefixes)
      Produces schema dump in XML format: this is performed pre- and post-upgrade (i.e.
      static int getMaxStringLength()  
      void onBootstrap​(org.springframework.context.ApplicationEvent event)  
      protected void onShutdown​(org.springframework.context.ApplicationEvent event)  
      void setAppliedPatchDAO​(AppliedPatchDAO appliedPatchDAO)  
      void setDatabaseMetaDataHelper​(DatabaseMetaDataHelper databaseMetaDataHelper)
      Defines the DatabaseMetaDataHelper to be used
      void setDataSource​(javax.sql.DataSource dataSource)  
      void setDbSchemaName​(java.lang.String dbSchemaName)
      Set db.schema.name to be used
      void setDescriptorService​(DescriptorService descriptorService)
      Provide a reference to the DescriptorService, used to provide information about the repository such as the database schema version number.
      void setDialect​(Dialect dialect)
      Sets the previously auto-detected Hibernate dialect.
      void setGlobalProperties​(java.util.Properties globalProperties)
      Sets the properties map from which we look up some configuration settings.
      void setMaximumStringLength​(int maximumStringLength)
      Optionally override the system's default maximum string length.
      static void setMaxStringLength​(int length, Dialect dialect)  
      void setSchemaOuputFilename​(java.lang.String schemaOuputFilename)
      Set this to output the full database creation script
      void setSchemaReferenceUrls​(java.util.List<java.lang.String> schemaReferenceUrls)
      Specifies the schema reference files that will be used to validate the repository schema whenever changes have been made.
      void setSchemaUpdateLockRetryCount​(int schemaUpdateLockRetryCount)
      Set the number times that the DB must be checked for the presence of the table indicating that a schema change is in progress.
      void setSchemaUpdateLockRetryWaitSeconds​(int schemaUpdateLockRetryWaitSeconds)
      Set the wait time (seconds) between checks for the schema update lock.
      void setStopAfterSchemaBootstrap​(boolean stopAfterSchemaBootstrap)
      Set whether this component should terminate the bootstrap process after running all the usual checks and scripts.
      void setUpdateSchema​(boolean updateSchema)
      Set whether to modify the schema or not.
      static java.lang.String trimStringForTextFields​(java.lang.String value)
      Truncates or returns a string that will fit into the string columns in the schema.
      int validateSchema​(java.lang.String outputFileNameTemplate, java.io.PrintWriter out)
      Collate differences and validation problems with the schema with respect to an appropriate reference schema.
      • Methods inherited from class org.springframework.extensions.surf.util.AbstractLifecycleBean

        getApplicationContext, onApplicationEvent, setApplicationContext
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • DEFAULT_LOCK_RETRY_COUNT

        public static final int DEFAULT_LOCK_RETRY_COUNT
        See Also:
        Constant Field Values
      • DEFAULT_LOCK_RETRY_WAIT_SECONDS

        public static final int DEFAULT_LOCK_RETRY_WAIT_SECONDS
        See Also:
        Constant Field Values
      • DEFAULT_MAX_STRING_LENGTH

        public static final int DEFAULT_MAX_STRING_LENGTH
        See Also:
        Constant Field Values
      • DEFAULT_MAX_STRING_LENGTH_NDB

        public static final int DEFAULT_MAX_STRING_LENGTH_NDB
        See Also:
        Constant Field Values
    • Constructor Detail

      • SchemaBootstrap

        public SchemaBootstrap()
    • Method Detail

      • getMaxStringLength

        public static final int getMaxStringLength()
        Returns:
        Returns the maximum number of characters that a string field can be
      • trimStringForTextFields

        public static final java.lang.String trimStringForTextFields​(java.lang.String value)
        Truncates or returns a string that will fit into the string columns in the schema. Text fields can either cope with arbitrarily long text fields or have the default limit, DEFAULT_MAX_STRING_LENGTH.
        Parameters:
        value - the string to check
        Returns:
        Returns a string that is short enough for getMaxStringLength()
        Since:
        3.2
      • setDescriptorService

        public void setDescriptorService​(DescriptorService descriptorService)
        Provide a reference to the DescriptorService, used to provide information about the repository such as the database schema version number.
        Parameters:
        descriptorService - the descriptorService to set
      • setDatabaseMetaDataHelper

        public void setDatabaseMetaDataHelper​(DatabaseMetaDataHelper databaseMetaDataHelper)
        Defines the DatabaseMetaDataHelper to be used
        Parameters:
        databaseMetaDataHelper - DatabaseMetaDataHelper
      • setDialect

        public void setDialect​(Dialect dialect)
        Sets the previously auto-detected Hibernate dialect.
        Parameters:
        dialect - the dialect
      • setDataSource

        public void setDataSource​(javax.sql.DataSource dataSource)
      • setAppliedPatchDAO

        public void setAppliedPatchDAO​(AppliedPatchDAO appliedPatchDAO)
      • setSchemaOuputFilename

        public void setSchemaOuputFilename​(java.lang.String schemaOuputFilename)
        Set this to output the full database creation script
        Parameters:
        schemaOuputFilename - the name of a file to dump the schema to, or null to ignore
      • setUpdateSchema

        public void setUpdateSchema​(boolean updateSchema)
        Set whether to modify the schema or not. Either way, the schema will be validated.
        Parameters:
        updateSchema - true to update and validate the schema, otherwise false to just validate the schema. Default is true.
      • setStopAfterSchemaBootstrap

        public void setStopAfterSchemaBootstrap​(boolean stopAfterSchemaBootstrap)
        Set whether this component should terminate the bootstrap process after running all the usual checks and scripts. This has the additional effect of dumping a final schema structure file just before exiting.

        WARNING: USE FOR DEBUG AND UPGRADE TESTING ONLY

        Parameters:
        stopAfterSchemaBootstrap - true to terminate (with exception) after running all the usual schema updates and checks.
      • setSchemaReferenceUrls

        public void setSchemaReferenceUrls​(java.util.List<java.lang.String> schemaReferenceUrls)
        Specifies the schema reference files that will be used to validate the repository schema whenever changes have been made. The database dialect placeholder will be resolved so that the correct reference files are loaded for the current database type (e.g. PostgreSQL)
        Parameters:
        schemaReferenceUrls - the schemaReferenceUrls to set
      • setSchemaUpdateLockRetryCount

        public void setSchemaUpdateLockRetryCount​(int schemaUpdateLockRetryCount)
        Set the number times that the DB must be checked for the presence of the table indicating that a schema change is in progress.
        Parameters:
        schemaUpdateLockRetryCount - the number of times to retry (default 24)
      • setSchemaUpdateLockRetryWaitSeconds

        public void setSchemaUpdateLockRetryWaitSeconds​(int schemaUpdateLockRetryWaitSeconds)
        Set the wait time (seconds) between checks for the schema update lock.
        Parameters:
        schemaUpdateLockRetryWaitSeconds - the number of seconds between checks (default 5 seconds)
      • setMaximumStringLength

        public void setMaximumStringLength​(int maximumStringLength)
        Optionally override the system's default maximum string length. Some databases have limitations on how long the string_value columns can be while other do not. Some parts of the persistence have alternatives when the string values exceed this length while others do not. Either way, it is possible to adjust the text column sizes and adjust this value manually to override the default associated with the database being used.

        The system - as of V2.1.2 - will attempt to adjust the maximum string length size automatically and therefore this method is not normally required. But it is possible to manually override the value if, for example, the system doesn't guess the correct maximum length or if the dialect is not explicitly catered for.

        All negative or zero values are ignored and the system defaults to its best guess based on the dialect being used.

        Parameters:
        maximumStringLength - the maximum length of the string_value columns
      • setDbSchemaName

        public void setDbSchemaName​(java.lang.String dbSchemaName)
        Set db.schema.name to be used
      • setGlobalProperties

        public void setGlobalProperties​(java.util.Properties globalProperties)
        Sets the properties map from which we look up some configuration settings.
        Parameters:
        globalProperties - the global properties
      • addPreCreateScriptUrl

        public void addPreCreateScriptUrl​(java.lang.String preCreateScriptUrl)
        Register a new script for execution when creating a clean schema. The order of registration determines the order of execution.
        Parameters:
        preCreateScriptUrl - the script URL, possibly containing the ${db.script.dialect} placeholder
      • addPostCreateScriptUrl

        public void addPostCreateScriptUrl​(java.lang.String postUpdateScriptUrl)
        Register a new script for execution after the Hibernate schema creation phase. The order of registration determines the order of execution.
        Parameters:
        postUpdateScriptUrl - the script URL, possibly containing the ${db.script.dialect} placeholder
      • addPreUpdateScriptPatch

        public void addPreUpdateScriptPatch​(SchemaUpgradeScriptPatch scriptPatch)
        Register a new SQL-based patch for consideration against the instance (before Hibernate execution)
        Parameters:
        scriptPatch - the patch that will be examined for execution
      • addPostUpdateScriptPatch

        public void addPostUpdateScriptPatch​(SchemaUpgradeScriptPatch scriptPatch)
        Register a new SQL-based patch for consideration against the instance (after Hibernate execution)
        Parameters:
        scriptPatch - the patch that will be examined for execution
      • addUpdateActivitiScriptPatch

        public void addUpdateActivitiScriptPatch​(SchemaUpgradeScriptPatch scriptPatch)
        Register a new SQL-based patch for consideration against the Activiti instance
        Parameters:
        scriptPatch - the patch that will be examined for execution
      • onBootstrap

        public void onBootstrap​(org.springframework.context.ApplicationEvent event)
        Specified by:
        onBootstrap in class org.springframework.extensions.surf.util.AbstractLifecycleBean
      • validateSchema

        public int validateSchema​(java.lang.String outputFileNameTemplate,
                                  java.io.PrintWriter out)
        Collate differences and validation problems with the schema with respect to an appropriate reference schema.
        Parameters:
        outputFileNameTemplate - String
        out - PrintWriter
        Returns:
        the number of potential problems found.
      • dumpSchema

        public java.util.List<java.io.File> dumpSchema()
        Produces schema dump in XML format: this is performed pre- and post-upgrade (i.e. if changes are made to the schema) and can made upon demand via JMX.
        Returns:
        List of output files.
      • dumpSchema

        public java.util.List<java.io.File> dumpSchema​(java.lang.String[] dbPrefixes)
        Produces schema dump in XML format: this is performed pre- and post-upgrade (i.e. if changes are made to the schema) and can made upon demand via JMX.
        Parameters:
        dbPrefixes - Array of database object prefixes to produce the dump for, e.g. "alf_".
        Returns:
        List of output files.
      • onShutdown

        protected void onShutdown​(org.springframework.context.ApplicationEvent event)
        Specified by:
        onShutdown in class org.springframework.extensions.surf.util.AbstractLifecycleBean