androidx.room.migration.Migration Java Examples
The following examples show how to use
androidx.room.migration.Migration.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MaterialisticDatabase.java From materialistic with Apache License 2.0 | 6 votes |
@VisibleForTesting protected static Builder<MaterialisticDatabase> setupBuilder(Builder<MaterialisticDatabase> builder) { return builder.addMigrations(new Migration(3, 4) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { database.execSQL(DbConstants.SQL_CREATE_SAVED_TABLE); database.execSQL(DbConstants.SQL_INSERT_FAVORITE_SAVED); database.execSQL(DbConstants.SQL_DROP_FAVORITE_TABLE); database.execSQL(DbConstants.SQL_CREATE_READ_TABLE); database.execSQL(DbConstants.SQL_INSERT_VIEWED_READ); database.execSQL(DbConstants.SQL_DROP_VIEWED_TABLE); database.execSQL(DbConstants.SQL_CREATE_READABLE_TABLE); database.execSQL(DbConstants.SQL_INSERT_READABILITY_READABLE); database.execSQL(DbConstants.SQL_DROP_READABILITY_TABLE); } }); }
Example #2
Source File: RoomOpenHelper.java From FairEmail with GNU General Public License v3.0 | 5 votes |
@Override public void onUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) { boolean migrated = false; if (mConfiguration != null) { List<Migration> migrations = mConfiguration.migrationContainer.findMigrationPath( oldVersion, newVersion); if (migrations != null) { mDelegate.onPreMigrate(db); for (Migration migration : migrations) { migration.migrate(db); } ValidationResult result = mDelegate.onValidateSchema(db); if (!result.isValid) { throw new IllegalStateException("Migration didn't properly handle: " + result.expectedFoundMsg); } mDelegate.onPostMigrate(db); updateIdentity(db); migrated = true; } } if (!migrated) { if (mConfiguration != null && !mConfiguration.isMigrationRequired(oldVersion, newVersion)) { mDelegate.dropAllTables(db); mDelegate.createAllTables(db); } else { throw new IllegalStateException("A migration from " + oldVersion + " to " + newVersion + " was required but not found. Please provide the " + "necessary Migration path via " + "RoomDatabase.Builder.addMigration(Migration ...) or allow for " + "destructive migrations via one of the " + "RoomDatabase.Builder.fallbackToDestructiveMigration* methods."); } } }
Example #3
Source File: RoomDatabase.java From FairEmail with GNU General Public License v3.0 | 5 votes |
private void addMigration(Migration migration) { final int start = migration.startVersion; final int end = migration.endVersion; TreeMap<Integer, Migration> targetMap = mMigrations.get(start); if (targetMap == null) { targetMap = new TreeMap<>(); mMigrations.put(start, targetMap); } Migration existing = targetMap.get(end); if (existing != null) { Log.w(Room.LOG_TAG, "Overriding migration " + existing + " with " + migration); } targetMap.put(end, migration); }
Example #4
Source File: RoomDatabase.java From FairEmail with GNU General Public License v3.0 | 5 votes |
/** * Finds the list of migrations that should be run to move from {@code start} version to * {@code end} version. * * @param start The current database version * @param end The target database version * @return An ordered list of {@link Migration} objects that should be run to migrate * between the given versions. If a migration path cannot be found, returns {@code null}. */ @SuppressWarnings("WeakerAccess") @Nullable public List<Migration> findMigrationPath(int start, int end) { if (start == end) { return Collections.emptyList(); } boolean migrateUp = end > start; List<Migration> result = new ArrayList<>(); return findUpMigrationPath(result, migrateUp, start, end); }
Example #5
Source File: RoomDatabase.java From FairEmail with GNU General Public License v3.0 | 5 votes |
private List<Migration> findUpMigrationPath(List<Migration> result, boolean upgrade, int start, int end) { while (upgrade ? start < end : start > end) { TreeMap<Integer, Migration> targetNodes = mMigrations.get(start); if (targetNodes == null) { return null; } // keys are ordered so we can start searching from one end of them. Set<Integer> keySet; if (upgrade) { keySet = targetNodes.descendingKeySet(); } else { keySet = targetNodes.keySet(); } boolean found = false; for (int targetVersion : keySet) { final boolean shouldAddToPath; if (upgrade) { shouldAddToPath = targetVersion <= end && targetVersion > start; } else { shouldAddToPath = targetVersion >= end && targetVersion < start; } if (shouldAddToPath) { result.add(targetNodes.get(targetVersion)); start = targetVersion; found = true; break; } } if (!found) { return null; } } return result; }
Example #6
Source File: DatabaseMigrationFactory.java From zephyr with MIT License | 5 votes |
@NonNull public static Migration[] getMigrations() { Migration[] roomMigrations = new Migration[MIGRATIONS.size()]; for (int x = 0; x < roomMigrations.length; x++) { IZephyrDatabaseMigration zephyrMigration = MIGRATIONS.get(x); roomMigrations[x] = new Migration(zephyrMigration.fromVersion(), zephyrMigration.toVersion()) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { zephyrMigration.migrate(database); } }; } return roomMigrations; }
Example #7
Source File: RoomDatabase.java From FairEmail with GNU General Public License v3.0 | 3 votes |
/** * Adds a migration to the builder. * <p> * Each Migration has a start and end versions and Room runs these migrations to bring the * database to the latest version. * <p> * If a migration item is missing between current version and the latest version, Room * will clear the database and recreate so even if you have no changes between 2 versions, * you should still provide a Migration object to the builder. * <p> * A migration can handle more than 1 version (e.g. if you have a faster path to choose when * going version 3 to 5 without going to version 4). If Room opens a database at version * 3 and latest version is >= 5, Room will use the migration object that can migrate from * 3 to 5 instead of 3 to 4 and 4 to 5. * * @param migrations The migration object that can modify the database and to the necessary * changes. * @return This {@link Builder} instance. */ @NonNull public Builder<T> addMigrations(@NonNull Migration... migrations) { if (mMigrationStartAndEndVersions == null) { mMigrationStartAndEndVersions = new HashSet<>(); } for (Migration migration : migrations) { mMigrationStartAndEndVersions.add(migration.startVersion); mMigrationStartAndEndVersions.add(migration.endVersion); } mMigrationContainer.addMigrations(migrations); return this; }
Example #8
Source File: RoomDatabase.java From FairEmail with GNU General Public License v3.0 | 2 votes |
/** * Adds the given migrations to the list of available migrations. If 2 migrations have the * same start-end versions, the latter migration overrides the previous one. * * @param migrations List of available migrations. */ public void addMigrations(@NonNull Migration... migrations) { for (Migration migration : migrations) { addMigration(migration); } }