@Test public void migrate_callsAppropriateMigrations_fullSet() { JobMigration migration1 = spy(new EmptyMigration(2)); JobMigration migration2 = spy(new EmptyMigration(3)); JobMigrator subject = new JobMigrator(1, 3, Arrays.asList(migration1, migration2)); int version = subject.migrate(simpleJobStorage(), mock(Data.Serializer.class)); assertEquals(3, version); verify(migration1).migrate(any()); verify(migration2).migrate(any()); }
int migrate(@NonNull JobStorage jobStorage, @NonNull Data.Serializer dataSerializer) { List<JobSpec> jobSpecs = jobStorage.getAllJobSpecs(); for (int i = lastSeenVersion; i < currentVersion; i++) { Log.i(TAG, "Migrating from " + i + " to " + (i + 1)); ListIterator<JobSpec> iter = jobSpecs.listIterator(); JobMigration migration = migrations.get(i + 1); assert migration != null; while (iter.hasNext()) { JobSpec jobSpec = iter.next(); Data data = dataSerializer.deserialize(jobSpec.getSerializedData()); JobData originalJobData = new JobData(jobSpec.getFactoryKey(), jobSpec.getQueueKey(), data); JobData updatedJobData = migration.migrate(originalJobData); JobSpec updatedJobSpec = new JobSpec(jobSpec.getId(), updatedJobData.getFactoryKey(), updatedJobData.getQueueKey(), jobSpec.getCreateTime(), jobSpec.getNextRunAttemptTime(), jobSpec.getRunAttempt(), jobSpec.getMaxAttempts(), jobSpec.getMaxBackoff(), jobSpec.getLifespan(), jobSpec.getMaxInstances(), dataSerializer.serialize(updatedJobData.getData()), jobSpec.getSerializedInputData(), jobSpec.isRunning(), jobSpec.isMemoryOnly()); iter.set(updatedJobSpec); } } jobStorage.updateJobs(jobSpecs); return currentVersion; }
JobMigrator { int migrate(@NonNull JobStorage jobStorage, @NonNull Data.Serializer dataSerializer) { List<JobSpec> jobSpecs = jobStorage.getAllJobSpecs(); for (int i = lastSeenVersion; i < currentVersion; i++) { Log.i(TAG, "Migrating from " + i + " to " + (i + 1)); ListIterator<JobSpec> iter = jobSpecs.listIterator(); JobMigration migration = migrations.get(i + 1); assert migration != null; while (iter.hasNext()) { JobSpec jobSpec = iter.next(); Data data = dataSerializer.deserialize(jobSpec.getSerializedData()); JobData originalJobData = new JobData(jobSpec.getFactoryKey(), jobSpec.getQueueKey(), data); JobData updatedJobData = migration.migrate(originalJobData); JobSpec updatedJobSpec = new JobSpec(jobSpec.getId(), updatedJobData.getFactoryKey(), updatedJobData.getQueueKey(), jobSpec.getCreateTime(), jobSpec.getNextRunAttemptTime(), jobSpec.getRunAttempt(), jobSpec.getMaxAttempts(), jobSpec.getMaxBackoff(), jobSpec.getLifespan(), jobSpec.getMaxInstances(), dataSerializer.serialize(updatedJobData.getData()), jobSpec.getSerializedInputData(), jobSpec.isRunning(), jobSpec.isMemoryOnly()); iter.set(updatedJobSpec); } } jobStorage.updateJobs(jobSpecs); return currentVersion; } }
JobMigrator { int migrate(@NonNull JobStorage jobStorage, @NonNull Data.Serializer dataSerializer) { List<JobSpec> jobSpecs = jobStorage.getAllJobSpecs(); for (int i = lastSeenVersion; i < currentVersion; i++) { Log.i(TAG, "Migrating from " + i + " to " + (i + 1)); ListIterator<JobSpec> iter = jobSpecs.listIterator(); JobMigration migration = migrations.get(i + 1); assert migration != null; while (iter.hasNext()) { JobSpec jobSpec = iter.next(); Data data = dataSerializer.deserialize(jobSpec.getSerializedData()); JobData originalJobData = new JobData(jobSpec.getFactoryKey(), jobSpec.getQueueKey(), data); JobData updatedJobData = migration.migrate(originalJobData); JobSpec updatedJobSpec = new JobSpec(jobSpec.getId(), updatedJobData.getFactoryKey(), updatedJobData.getQueueKey(), jobSpec.getCreateTime(), jobSpec.getNextRunAttemptTime(), jobSpec.getRunAttempt(), jobSpec.getMaxAttempts(), jobSpec.getMaxBackoff(), jobSpec.getLifespan(), jobSpec.getMaxInstances(), dataSerializer.serialize(updatedJobData.getData()), jobSpec.getSerializedInputData(), jobSpec.isRunning(), jobSpec.isMemoryOnly()); iter.set(updatedJobSpec); } } jobStorage.updateJobs(jobSpecs); return currentVersion; } JobMigrator(int lastSeenVersion, int currentVersion, @NonNull List<JobMigration> migrations); }
JobMigrator { int migrate(@NonNull JobStorage jobStorage, @NonNull Data.Serializer dataSerializer) { List<JobSpec> jobSpecs = jobStorage.getAllJobSpecs(); for (int i = lastSeenVersion; i < currentVersion; i++) { Log.i(TAG, "Migrating from " + i + " to " + (i + 1)); ListIterator<JobSpec> iter = jobSpecs.listIterator(); JobMigration migration = migrations.get(i + 1); assert migration != null; while (iter.hasNext()) { JobSpec jobSpec = iter.next(); Data data = dataSerializer.deserialize(jobSpec.getSerializedData()); JobData originalJobData = new JobData(jobSpec.getFactoryKey(), jobSpec.getQueueKey(), data); JobData updatedJobData = migration.migrate(originalJobData); JobSpec updatedJobSpec = new JobSpec(jobSpec.getId(), updatedJobData.getFactoryKey(), updatedJobData.getQueueKey(), jobSpec.getCreateTime(), jobSpec.getNextRunAttemptTime(), jobSpec.getRunAttempt(), jobSpec.getMaxAttempts(), jobSpec.getMaxBackoff(), jobSpec.getLifespan(), jobSpec.getMaxInstances(), dataSerializer.serialize(updatedJobData.getData()), jobSpec.getSerializedInputData(), jobSpec.isRunning(), jobSpec.isMemoryOnly()); iter.set(updatedJobSpec); } } jobStorage.updateJobs(jobSpecs); return currentVersion; } JobMigrator(int lastSeenVersion, int currentVersion, @NonNull List<JobMigration> migrations);  }
JobMigrator { int migrate(@NonNull JobStorage jobStorage, @NonNull Data.Serializer dataSerializer) { List<JobSpec> jobSpecs = jobStorage.getAllJobSpecs(); for (int i = lastSeenVersion; i < currentVersion; i++) { Log.i(TAG, "Migrating from " + i + " to " + (i + 1)); ListIterator<JobSpec> iter = jobSpecs.listIterator(); JobMigration migration = migrations.get(i + 1); assert migration != null; while (iter.hasNext()) { JobSpec jobSpec = iter.next(); Data data = dataSerializer.deserialize(jobSpec.getSerializedData()); JobData originalJobData = new JobData(jobSpec.getFactoryKey(), jobSpec.getQueueKey(), data); JobData updatedJobData = migration.migrate(originalJobData); JobSpec updatedJobSpec = new JobSpec(jobSpec.getId(), updatedJobData.getFactoryKey(), updatedJobData.getQueueKey(), jobSpec.getCreateTime(), jobSpec.getNextRunAttemptTime(), jobSpec.getRunAttempt(), jobSpec.getMaxAttempts(), jobSpec.getMaxBackoff(), jobSpec.getLifespan(), jobSpec.getMaxInstances(), dataSerializer.serialize(updatedJobData.getData()), jobSpec.getSerializedInputData(), jobSpec.isRunning(), jobSpec.isMemoryOnly()); iter.set(updatedJobSpec); } } jobStorage.updateJobs(jobSpecs); return currentVersion; } JobMigrator(int lastSeenVersion, int currentVersion, @NonNull List<JobMigration> migrations);   }