Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System/Environment variables #144

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#
# Copyright 2010-2019 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

FROM openjdk:8
RUN ["mkdir", "-p", "/opt/migrations"]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2017 the original author or authors.
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package org.apache.ibatis.migration;

import org.apache.ibatis.migration.environment.Environment;
import org.apache.ibatis.migration.options.SelectedPaths;

public interface FileMigrationLoaderFactory {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2018 the original author or authors.
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,7 +43,7 @@
import org.apache.ibatis.migration.Change;
import org.apache.ibatis.migration.ConnectionProvider;
import org.apache.ibatis.migration.DataSourceConnectionProvider;
import org.apache.ibatis.migration.Environment;
import org.apache.ibatis.migration.environment.Environment;
import org.apache.ibatis.migration.FileMigrationLoader;
import org.apache.ibatis.migration.FileMigrationLoaderFactory;
import org.apache.ibatis.migration.MigrationException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.migration.environment;

enum ENVIRONMENT_KEY {
time_zone,
delimiter,
script_char_set,
full_line_delimiter,
send_full_script,
auto_commit,
remove_crs,
ignore_warnings,
driver_path,
driver,
url,
username,
password,
hook_before_up,
hook_before_each_up,
hook_after_each_up,
hook_after_up,
hook_before_down,
hook_before_each_down,
hook_after_each_down,
hook_after_down,
hook_before_new,
hook_after_new
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2018 the original author or authors.
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -13,61 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ibatis.migration;
package org.apache.ibatis.migration.environment;

import org.apache.ibatis.migration.MigrationException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map.Entry;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Environment {

public static final String CHANGELOG = "changelog";

private enum SETTING_KEY {
time_zone,
delimiter,
script_char_set,
full_line_delimiter,
send_full_script,
auto_commit,
remove_crs,
ignore_warnings,
driver_path,
driver,
url,
username,
password,
hook_before_up,
hook_before_each_up,
hook_after_each_up,
hook_after_up,
hook_before_down,
hook_before_each_down,
hook_after_each_down,
hook_after_down,
hook_before_new,
hook_after_new
}

private static final List<String> SETTING_KEYS;

static {
ArrayList<String> list = new ArrayList<String>();
SETTING_KEY[] keys = SETTING_KEY.values();
for (SETTING_KEY key : keys) {
list.add(key.name());
}
SETTING_KEYS = Collections.unmodifiableList(list);
}

private final String timeZone;
private final String delimiter;
private final String scriptCharset;
Expand All @@ -94,62 +58,51 @@ private enum SETTING_KEY {
private final String hookBeforeNew;
private final String hookAfterNew;

private final Properties variables = new Properties();
private final Properties userVariables;

public Environment(File file) {
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
Properties prop = new Properties();
try (FileInputStream inputStream = new FileInputStream(file)) {
final Properties prop = new Properties();
prop.load(inputStream);

this.timeZone = prop.getProperty(SETTING_KEY.time_zone.name(), "GMT+0:00");
this.delimiter = prop.getProperty(SETTING_KEY.delimiter.name(), ";");
this.scriptCharset = prop.getProperty(SETTING_KEY.script_char_set.name(), Charset.defaultCharset().name());
this.fullLineDelimiter = Boolean.valueOf(prop.getProperty(SETTING_KEY.full_line_delimiter.name()));
this.sendFullScript = Boolean.valueOf(prop.getProperty(SETTING_KEY.send_full_script.name()));
this.autoCommit = Boolean.valueOf(prop.getProperty(SETTING_KEY.auto_commit.name()));
this.removeCrs = Boolean.valueOf(prop.getProperty(SETTING_KEY.remove_crs.name()));
this.ignoreWarnings = Boolean.valueOf(prop.getProperty(SETTING_KEY.ignore_warnings.name(), "true"));

this.driverPath = prop.getProperty(SETTING_KEY.driver_path.name());
this.driver = prop.getProperty(SETTING_KEY.driver.name());
this.url = prop.getProperty(SETTING_KEY.url.name());
this.username = prop.getProperty(SETTING_KEY.username.name());
this.password = prop.getProperty(SETTING_KEY.password.name());

this.hookBeforeUp = prop.getProperty(SETTING_KEY.hook_before_up.name());
this.hookBeforeEachUp = prop.getProperty(SETTING_KEY.hook_before_each_up.name());
this.hookAfterEachUp = prop.getProperty(SETTING_KEY.hook_after_each_up.name());
this.hookAfterUp = prop.getProperty(SETTING_KEY.hook_after_up.name());
this.hookBeforeDown = prop.getProperty(SETTING_KEY.hook_before_down.name());
this.hookBeforeEachDown = prop.getProperty(SETTING_KEY.hook_before_each_down.name());
this.hookAfterEachDown = prop.getProperty(SETTING_KEY.hook_after_each_down.name());
this.hookAfterDown = prop.getProperty(SETTING_KEY.hook_after_down.name());

this.hookBeforeNew = prop.getProperty(SETTING_KEY.hook_before_new.name());
this.hookAfterNew = prop.getProperty(SETTING_KEY.hook_after_new.name());

// User defined variables.
Set<Entry<Object, Object>> entries = prop.entrySet();
for (Entry<Object, Object> entry : entries) {
String key = (String) entry.getKey();
if (!SETTING_KEYS.contains(key)) {
variables.put(key, entry.getValue());
}
}
resolveProperties(prop);

this.timeZone = prop.getProperty(ENVIRONMENT_KEY.time_zone.name(), "GMT+0:00");
this.delimiter = prop.getProperty(ENVIRONMENT_KEY.delimiter.name(), ";");
this.scriptCharset = prop.getProperty(ENVIRONMENT_KEY.script_char_set.name(), Charset.defaultCharset().name());
this.fullLineDelimiter = Boolean.valueOf(prop.getProperty(ENVIRONMENT_KEY.full_line_delimiter.name()));
this.sendFullScript = Boolean.valueOf(prop.getProperty(ENVIRONMENT_KEY.send_full_script.name()));
this.autoCommit = Boolean.valueOf(prop.getProperty(ENVIRONMENT_KEY.auto_commit.name()));
this.removeCrs = Boolean.valueOf(prop.getProperty(ENVIRONMENT_KEY.remove_crs.name()));
this.ignoreWarnings = Boolean.valueOf(prop.getProperty(ENVIRONMENT_KEY.ignore_warnings.name(), "true"));

this.driverPath = prop.getProperty(ENVIRONMENT_KEY.driver_path.name());
this.driver = prop.getProperty(ENVIRONMENT_KEY.driver.name());
this.url = prop.getProperty(ENVIRONMENT_KEY.url.name());
this.username = prop.getProperty(ENVIRONMENT_KEY.username.name());
this.password = prop.getProperty(ENVIRONMENT_KEY.password.name());

this.hookBeforeUp = prop.getProperty(ENVIRONMENT_KEY.hook_before_up.name());
this.hookBeforeEachUp = prop.getProperty(ENVIRONMENT_KEY.hook_before_each_up.name());
this.hookAfterEachUp = prop.getProperty(ENVIRONMENT_KEY.hook_after_each_up.name());
this.hookAfterUp = prop.getProperty(ENVIRONMENT_KEY.hook_after_up.name());
this.hookBeforeDown = prop.getProperty(ENVIRONMENT_KEY.hook_before_down.name());
this.hookBeforeEachDown = prop.getProperty(ENVIRONMENT_KEY.hook_before_each_down.name());
this.hookAfterEachDown = prop.getProperty(ENVIRONMENT_KEY.hook_after_each_down.name());
this.hookAfterDown = prop.getProperty(ENVIRONMENT_KEY.hook_after_down.name());

this.hookBeforeNew = prop.getProperty(ENVIRONMENT_KEY.hook_before_new.name());
this.hookAfterNew = prop.getProperty(ENVIRONMENT_KEY.hook_after_new.name());

this.userVariables = new Properties();

final Set<String> envKeys = Stream.of(ENVIRONMENT_KEY.values()).map(Enum::name).collect(Collectors.toSet());
userVariables.putAll(prop.keySet().stream().map(Object::toString).filter(k -> !envKeys.contains(k))
.collect(Collectors.toMap(Function.identity(), prop::getProperty)));
} catch (FileNotFoundException e) {
throw new MigrationException("Environment file missing: " + file.getAbsolutePath());
} catch (IOException e) {
throw new MigrationException("Error loading environment properties. Cause: " + e, e);
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// ignore
}
}
}
}

Expand Down Expand Up @@ -246,6 +199,21 @@ public String getHookAfterNew() {
}

public Properties getVariables() {
return variables;
return userVariables;
}

private static void resolveProperties(final Properties properties) {
final Map<String, String> env = System.getenv();
final Properties sysProps = System.getProperties();
final Map<String, String> systemProperties = Stream.of(ENVIRONMENT_KEY.values()).map(Enum::name)
.filter(sysProps::containsKey).collect(Collectors.toMap(Object::toString, sysProps::getProperty));

properties.putAll(systemProperties);

final Map<String, String> environmentProperties = Stream.of(ENVIRONMENT_KEY.values())
.map(k -> k.name().toUpperCase()).filter(env::containsKey)
.collect(Collectors.toMap(String::toLowerCase, env::get));

properties.putAll(environmentProperties);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2017 the original author or authors.
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,7 @@
import java.util.Arrays;
import java.util.Properties;

import org.apache.ibatis.migration.Environment;
import org.apache.ibatis.migration.environment.Environment;
import org.apache.ibatis.migration.MigrationException;
import org.apache.ibatis.migration.options.SelectedPaths;

Expand Down
8 changes: 2 additions & 6 deletions src/test/java/org/apache/ibatis/migration/MigratorTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2010-2018 the original author or authors.
* Copyright 2010-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -60,11 +60,7 @@ public void shouldRunThroughFullMigrationUseCaseInOneTestToEnsureOrder() throws
// which is why they're executed from this single test. Perhaps there's a better way.

exit.expectSystemExit();
exit.checkAssertionAfterwards(new Assertion() {
public void checkAssertion() {
assertEquals("", out.getLog());
}
});
exit.checkAssertionAfterwards(() -> assertEquals("", out.getLog()));

testBootstrapCommand();
testStatusContainsNoPendingEntriesUsingStatusShorthand();
Expand Down