Class DefaultDbMigration

  • All Implemented Interfaces:
    DbMigration

    public class DefaultDbMigration
    extends Object
    implements DbMigration
    Generates DB Migration xml and sql scripts.

    Reads the prior migrations and compares with the current model of the EbeanServer and generates a migration 'diff' in the form of xml document with the logical schema changes and a series of sql scripts to apply, rollback the applied changes if necessary and drop objects (drop tables, drop columns).

    This does not run the migration or ddl scripts but just generates them.

    
    
           DbMigration migration = DbMigration.create();
           migration.setPathToResources("src/main/resources");
           migration.setPlatform(Platform.POSTGRES);
    
           migration.generateMigration();
    
     
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultDbMigration()
      Create for offline migration generation.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addDatabasePlatform​(io.ebean.config.dbplatform.DatabasePlatform databasePlatform, String prefix)
      Add a databasePlatform to write the migration DDL.
      void addPlatform​(io.ebean.annotation.Platform platform)
      Add a platform to write the migration DDL.
      void addPlatform​(io.ebean.annotation.Platform platform, String prefix)
      Add a platform to write with a given prefix.
      String generateInitMigration()
      Generate an "init" migration which has all changes.
      String generateMigration()
      Generate the next migration xml file and associated apply and rollback sql scripts.
      List<String> getPendingDrops()
      Return the versions containing pending drops.
      void setAddForeignKeySkipCheck​(boolean addForeignKeySkipCheck)
      Set to true if ALTER TABLE ADD FOREIGN KEY should be generated with an option to skip validation.
      void setApplyPrefix​(String applyPrefix)
      Set the prefix for the version.
      void setGeneratePendingDrop​(String generatePendingDrop)
      Generate a migration for the version specified that contains pending drops.
      void setHeader​(String header)
      Set the header that is included in the generated DDL script.
      void setIncludeBuiltInPartitioning​(boolean includeBuiltInPartitioning)
      Set this to false to exclude the builtin support for table partitioning (with @DbPartition).
      void setIncludeGeneratedFileComment​(boolean includeGeneratedFileComment)
      Set to true to include a generated header comment in the DDL script.
      void setIncludeIndex​(boolean includeIndex)
      Set to include generation of the index migration file.
      void setLockTimeout​(int seconds)
      Set the lock timeout to be included with the DDL generation.
      void setLogToSystemOut​(boolean logToSystemOut)
      Set logging to System out (defaults to true).
      void setMigrationPath​(String migrationPath)
      Set the path where migrations are generated to (which defaults to "dbmigration").
      void setName​(String name)
      Set the name of the migration to be generated.
      void setPathToResources​(String pathToResources)
      Set the path from the current working directory to the application resources.
      void setPlatform​(io.ebean.annotation.Platform platform)
      Set the specific platform to generate DDL for.
      void setPlatform​(io.ebean.config.dbplatform.DatabasePlatform databasePlatform)
      Set the specific platform to generate DDL for.
      void setServer​(io.ebean.Database database)
      Set the server to use to determine the current model.
      void setServerConfig​(io.ebean.config.DatabaseConfig config)
      Set the DatabaseConfig to use.
      void setStrictMode​(boolean strictMode)
      Set to false in order to turn off strict mode.
      void setVersion​(String version)
      Set the version of the migration to be generated.
    • Constructor Detail

      • DefaultDbMigration

        public DefaultDbMigration()
        Create for offline migration generation.
    • Method Detail

      • setMigrationPath

        public void setMigrationPath​(String migrationPath)
        Description copied from interface: DbMigration
        Set the path where migrations are generated to (which defaults to "dbmigration").

        Normally we only use this when we use Ebean to generate the database migrations and then use some other tool like FlywayDB to run the migrations.

        Example: with setMigrationPath("db/migration") ... the migrations are generated into src/resources/db/migration.

        Note that if Ebean migration runner is used we should not use this method but instead set the migrationPath via a property such that both the migration generator and migration runner both use the same path.

        Specified by:
        setMigrationPath in interface DbMigration
        Parameters:
        migrationPath - The path that migrations are generated into.
      • setServer

        public void setServer​(io.ebean.Database database)
        Description copied from interface: DbMigration
        Set the server to use to determine the current model. Usually this is not called explicitly.
        Specified by:
        setServer in interface DbMigration
      • setServerConfig

        public void setServerConfig​(io.ebean.config.DatabaseConfig config)
        Description copied from interface: DbMigration
        Set the DatabaseConfig to use. Usually this is not called explicitly.
        Specified by:
        setServerConfig in interface DbMigration
      • setStrictMode

        public void setStrictMode​(boolean strictMode)
        Description copied from interface: DbMigration
        Set to false in order to turn off strict mode.

        Strict mode checks that a column changed to non-null on an existing table via DB migration has a default value specified. Set this to false if that isn't the case but it is known that all the existing rows have a value specified (there are no existing null values for the column).

        Specified by:
        setStrictMode in interface DbMigration
      • setAddForeignKeySkipCheck

        public void setAddForeignKeySkipCheck​(boolean addForeignKeySkipCheck)
        Description copied from interface: DbMigration
        Set to true if ALTER TABLE ADD FOREIGN KEY should be generated with an option to skip validation.

        Currently this is only useful for Postgres DDL adding the NOT VALID option.

        Specified by:
        setAddForeignKeySkipCheck in interface DbMigration
      • setLockTimeout

        public void setLockTimeout​(int seconds)
        Description copied from interface: DbMigration
        Set the lock timeout to be included with the DDL generation.

        Currently this is only useful for Postgres migrations adding a set lock_timeout statement to the generated database migration.

        Specified by:
        setLockTimeout in interface DbMigration
      • setGeneratePendingDrop

        public void setGeneratePendingDrop​(String generatePendingDrop)
        Description copied from interface: DbMigration
        Generate a migration for the version specified that contains pending drops.
        Specified by:
        setGeneratePendingDrop in interface DbMigration
        Parameters:
        generatePendingDrop - The version of a prior migration that holds pending drops.
      • setIncludeIndex

        public void setIncludeIndex​(boolean includeIndex)
        Description copied from interface: DbMigration
        Set to include generation of the index migration file.

        When true this generates a idx_<platform>.migrations file. This can be used by the migration runner to improve performance of running migrations, especially when no migration changes have occurred.

        Specified by:
        setIncludeIndex in interface DbMigration
      • setPlatform

        public void setPlatform​(io.ebean.annotation.Platform platform)
        Set the specific platform to generate DDL for.

        If not set this defaults to the platform of the default server.

        Specified by:
        setPlatform in interface DbMigration
      • setPlatform

        public void setPlatform​(io.ebean.config.dbplatform.DatabasePlatform databasePlatform)
        Set the specific platform to generate DDL for.

        If not set this defaults to the platform of the default server.

        Specified by:
        setPlatform in interface DbMigration
      • addPlatform

        public void addPlatform​(io.ebean.annotation.Platform platform)
        Description copied from interface: DbMigration
        Add a platform to write the migration DDL.

        Use this when you want to generate sql scripts for multiple database platforms from the migration (e.g. generate migration sql for MySql, Postgres and Oracle).

        Specified by:
        addPlatform in interface DbMigration
      • addDatabasePlatform

        public void addDatabasePlatform​(io.ebean.config.dbplatform.DatabasePlatform databasePlatform,
                                        String prefix)
        Description copied from interface: DbMigration
        Add a databasePlatform to write the migration DDL.

        Use this when you want to add preconfigured database platforms.

        Specified by:
        addDatabasePlatform in interface DbMigration
      • generateMigration

        public String generateMigration()
                                 throws IOException
        Generate the next migration xml file and associated apply and rollback sql scripts.

        This does not run the migration or ddl scripts but just generates them.

        Example: Run for a single specific platform

        
        
               DbMigration migration = DbMigration.create();
               migration.setPathToResources("src/main/resources");
               migration.setPlatform(Platform.ORACLE);
        
               migration.generateMigration();
        
         

        Example: Run migration generating DDL for multiple platforms

        
        
               DbMigration migration = DbMigration.create();
               migration.setPathToResources("src/main/resources");
        
               migration.addPlatform(Platform.POSTGRES);
               migration.addPlatform(Platform.MYSQL);
               migration.addPlatform(Platform.ORACLE);
        
               migration.generateMigration();
        
         
        Specified by:
        generateMigration in interface DbMigration
        Returns:
        the generated migration or null
        Throws:
        IOException