Example #1
0
 /**
  * Runs all the migrations in the configuration which version key is not already present in the
  * database. The migration will get run in the same order they are in the list.
  *
  * @param configuration Configuration with the database and all the migrations.
  */
 public static void migrate(Configuration configuration) {
   Migrate4Droid.configuration = configuration;
   ensureMigrationDao();
   List<Migration> migrations = configuration.getMigrations();
   List<String> actual = migrationDao.getAllMigrations();
   for (Migration m : migrations) if (!actual.contains(m.getMigration())) runMigration(m);
 }
Example #2
0
 /**
  * Register in the database all of the migrations in the <code>config</code> parameter as run.
  *
  * @param config Configuration with the migrations that need to be marked as run.
  */
 public static void markAsMigrated(Configuration config) {
   configuration = config;
   ensureMigrationDao();
   List<String> actual = migrationDao.getAllMigrations();
   for (Migration m : config.getMigrations())
     if (!actual.contains(m.getMigration())) markAsMigrated(m);
 }
Example #3
0
 /**
  * Moves the database to the <code>migration</code> specified, using the migrations in the <code>
  * configuration</code>, iterating them and checking:
  *
  * <ul>
  *   <li>If the migration's key is smaller than or equal to the desired <code>migration</code>'s
  *       key parameterized and it's not present in the database, the migration will get upgraded.
  *       That is, its method {@link Migration#up()} will be called. The order will be the same as
  *       the migrations are in the <code>configuration</code>.
  *   <li>If the migration's key is greater than the desired <code>migration</code>'s key
  *       parameterized and it IS present in the database, the migration will get downgraded. That
  *       is, its method {@link Migration#down()} will be called. The order will be the opposite
  *       the migrations are in the <code>configuration</code>.
  * </ul>
  *
  * @param configuration Configuration with the database and all the migrations that have to be
  *     considered during the migration.
  * @param migration String key of the migration the database is desired to be.
  */
 public static void migrate(Configuration configuration, String migration) {
   Migrate4Droid.configuration = configuration;
   ensureMigrationDao();
   List<Migration> migrations = configuration.getMigrations();
   List<Migration> downs = new ArrayList<Migration>();
   List<String> actual = migrationDao.getAllMigrations();
   for (Migration m : migrations) {
     String mig = m.getMigration();
     if (mig.compareTo(migration) <= 0 && !actual.contains(mig)) runMigration(m);
     else if (mig.compareTo(migration) > 0 && actual.contains(mig)) downs.add(0, m);
   }
   for (Migration m : downs) runMigration(m, false);
 }