All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ecyshor.cassmig.validation.MigrationValidator Maven / Gradle / Ivy

There is a newer version: 1.1.6
Show newest version
package com.ecyshor.cassmig.validation;

import com.ecyshor.cassmig.exception.InvalidDataException;
import com.ecyshor.cassmig.model.AppliedMigration;
import com.ecyshor.cassmig.model.MigrationComparator;
import com.ecyshor.cassmig.model.MigrationFile;

import java.util.Collections;
import java.util.List;

public class MigrationValidator {

	public void validateMigrations(List appliedMigrations, List migrationsFiles) {
		Collections.sort(appliedMigrations, MigrationComparator.getInstance());
		Collections.sort(migrationsFiles, MigrationComparator.getInstance());
		checkForMissingFiles(appliedMigrations, migrationsFiles);
		checkForInconsistencyInFiles(appliedMigrations, migrationsFiles);
	}

	private void checkForMissingFiles(List appliedMigrations, List migrationsFiles) {
		if (appliedMigrations.size() > migrationsFiles.size()) {
			throw new InvalidDataException("We found more applied migrations than existing files.");
		}
	}

	private void checkForInconsistencyInFiles(List appliedMigrations, List migrationsFiles) {
		for (int index = 0; index < appliedMigrations.size(); index++) {
			AppliedMigration appliedMigration = appliedMigrations.get(index);
			MigrationFile migrationFile = migrationsFiles.get(index);
			verifyConsistencyOfMigrationFile(appliedMigration, migrationFile);
		}
	}

	private void verifyConsistencyOfMigrationFile(AppliedMigration appliedMigration, MigrationFile migrationFile) {
		if (appliedMigration.getOrder() != migrationFile.getOrder() || !appliedMigration.getMd5Sum().equals(migrationFile.getMd5Sum())
				|| !appliedMigration.getSchema().equals(migrationFile.getSchema())) {
			throw new InvalidDataException("The applied migration " + appliedMigration +
					" is not consisntent to what we found in the files " + migrationFile);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy