com.ecyshor.cassmig.migration.MigrationExtractor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-migration Show documentation
Show all versions of cassandra-migration Show documentation
Java schema migration library for Cassandra
package com.ecyshor.cassmig.migration;
import com.ecyshor.cassmig.MigrationFileTransformer;
import com.ecyshor.cassmig.files.FileLoader;
import com.ecyshor.cassmig.model.ExternalMigrationConfig;
import com.ecyshor.cassmig.model.MigrationComparator;
import com.ecyshor.cassmig.model.MigrationFile;
import com.ecyshor.cassmig.model.ModuleLoadingConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
public class MigrationExtractor {
private static final Logger LOGGER = LoggerFactory.getLogger(MigrationExtractor.class);
private MigrationFileTransformer migrationFileTransformer;
private FileLoader fileLoader;
private FileLoader externalFileLoader;
public MigrationExtractor(MigrationFileTransformer migrationFileTransformer,
FileLoader fileLoader,
FileLoader externalFileLoader) {
this.migrationFileTransformer = migrationFileTransformer;
this.fileLoader = fileLoader;
this.externalFileLoader = externalFileLoader;
}
public List getMigrationFiles(String path)
throws IOException {
return getMigrationFiles(new ModuleLoadingConfig(path));
}
public List getMigrationFiles(ModuleLoadingConfig config)
throws IOException {
LOGGER.info("Finding migration files in config {}.", config);
List files = fileLoader.loadFiles(config);
return getMigrationFiles(files);
}
public List getMigrationFiles(ExternalMigrationConfig externalMigration) {
LOGGER.info("Finding migration files in config {}.", externalMigration);
List files = externalFileLoader.loadFiles(externalMigration);
List migrationFiles = getMigrationFiles(files);
addSchemaToMigrationFiles(migrationFiles, externalMigration);
return migrationFiles;
}
private void addSchemaToMigrationFiles(List migrationFiles, ExternalMigrationConfig externalMigration) {
String schema = externalMigration.getPackagePrefix() + externalMigration.getMigrationFilesPrefix();
for (MigrationFile migrationFile : migrationFiles) {
migrationFile.setSchema(schema);
}
}
private List getMigrationFiles(List files) {
List migrationFiles = migrationFileTransformer.transformFilesToMigrations(files);
Collections.sort(migrationFiles, MigrationComparator.getInstance());
return migrationFiles;
}
}