use of liquibase.Scope in project liquibase by liquibase.
the class LiquibaseCommandLine method configureScope.
/**
* Configures the system, and returns values to add to Scope.
*
* @return values to set in the scope
*/
private Map<String, Object> configureScope() throws Exception {
Map<String, Object> returnMap = new HashMap<>();
final ClassLoader classLoader = configureClassLoader();
returnMap.putAll(configureLogging());
returnMap.putAll(configureResourceAccessor(classLoader));
ConsoleUIService ui = null;
List<UIService> uiServices = Scope.getCurrentScope().getServiceLocator().findInstances(UIService.class);
for (UIService uiService : uiServices) {
if (uiService instanceof ConsoleUIService) {
ui = (ConsoleUIService) uiService;
break;
}
}
if (ui == null) {
ui = new ConsoleUIService();
}
ui.setAllowPrompt(true);
ui.setOutputStream(System.err);
returnMap.put(Scope.Attr.ui.name(), ui);
returnMap.put(LiquibaseCommandLineConfiguration.ARGUMENT_CONVERTER.getKey(), (LiquibaseCommandLineConfiguration.ArgumentConverter) argument -> "--" + StringUtil.toKabobCase(argument));
return returnMap;
}
use of liquibase.Scope in project liquibase by liquibase.
the class DiffToChangeLog method print.
public void print(String changeLogFile, ChangeLogSerializer changeLogSerializer) throws ParserConfigurationException, IOException, DatabaseException {
this.changeSetPath = changeLogFile;
File file = new File(changeLogFile);
final Map<String, Object> newScopeObjects = new HashMap<>();
File objectsDir = null;
if (changeLogFile.toLowerCase().endsWith("sql")) {
DeprecatedConfigurationValueProvider.setData("liquibase.pro.sql.inline", "true");
} else if (this.diffResult.getComparisonSnapshot() instanceof EmptyDatabaseSnapshot) {
objectsDir = new File(file.getParentFile(), "objects");
} else {
objectsDir = new File(file.getParentFile(), "objects-" + new Date().getTime());
}
if (objectsDir != null) {
if (objectsDir.exists()) {
throw new UnexpectedLiquibaseException("The generatechangelog command would overwrite your existing stored logic files. To run this command please remove or rename the '" + objectsDir.getCanonicalPath() + "' dir in your local project directory");
}
newScopeObjects.put(EXTERNAL_FILE_DIR_SCOPE_KEY, objectsDir);
}
newScopeObjects.put(DIFF_OUTPUT_CONTROL_SCOPE_KEY, diffOutputControl);
try {
//
// Get a Database instance and save it in the scope for later use
//
DatabaseSnapshot snapshot = diffResult.getReferenceSnapshot();
Database database = determineDatabase(diffResult.getReferenceSnapshot());
if (database == null) {
database = determineDatabase(diffResult.getComparisonSnapshot());
}
newScopeObjects.put(DIFF_SNAPSHOT_DATABASE, database);
Scope.child(newScopeObjects, new Scope.ScopedRunner() {
@Override
public void run() {
try {
if (!file.exists()) {
// print changeLog only if there are available changeSets to print instead of printing it always
printNew(changeLogSerializer, file);
} else {
Scope.getCurrentScope().getLog(getClass()).info(file + " exists, appending");
ByteArrayOutputStream out = new ByteArrayOutputStream();
print(new PrintStream(out, true, GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()), changeLogSerializer);
String xml = new String(out.toByteArray(), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
String innerXml = xml.replaceFirst("(?ms).*<databaseChangeLog[^>]*>", "");
innerXml = innerXml.replaceFirst(DATABASE_CHANGE_LOG_CLOSING_XML_TAG, "");
innerXml = innerXml.trim();
if ("".equals(innerXml)) {
Scope.getCurrentScope().getLog(getClass()).info("No changes found, nothing to do");
return;
}
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw")) {
String line;
long offset = 0;
boolean foundEndTag = false;
while ((line = randomAccessFile.readLine()) != null) {
int index = line.indexOf(DATABASE_CHANGE_LOG_CLOSING_XML_TAG);
if (index >= 0) {
foundEndTag = true;
break;
} else {
offset = randomAccessFile.getFilePointer();
}
}
String lineSeparator = GlobalConfiguration.OUTPUT_LINE_SEPARATOR.getCurrentValue();
if (foundEndTag) {
randomAccessFile.seek(offset);
randomAccessFile.writeBytes(" ");
randomAccessFile.write(innerXml.getBytes(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()));
randomAccessFile.writeBytes(lineSeparator);
randomAccessFile.writeBytes(DATABASE_CHANGE_LOG_CLOSING_XML_TAG + lineSeparator);
} else {
randomAccessFile.seek(0);
long length = randomAccessFile.length();
randomAccessFile.seek(length);
randomAccessFile.write(xml.getBytes(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()));
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
} catch (Exception e) {
// rethrow known exceptions. TODO: Fix this up with final Scope API
final Throwable cause = e.getCause();
if (cause instanceof ParserConfigurationException) {
throw (ParserConfigurationException) cause;
}
if (cause instanceof IOException) {
throw (IOException) cause;
}
if (cause instanceof DatabaseException) {
throw (DatabaseException) cause;
}
throw new RuntimeException(e);
}
}
Aggregations