-
Notifications
You must be signed in to change notification settings - Fork 78
Allow more flexible environment settings #146
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
224aac4
Read environment settings from system properties or environment varia…
harawata cd8a4d8
Replace variable expression `${}` in environment properties file
harawata 69c6b9a
Read user defined variables from environment variables and system pro…
harawata bf593c4
Add a test for environment variables.
harawata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/test/java/org/apache/ibatis/migration/system_property/SystemPropertyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* 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.system_property; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.io.File; | ||
|
||
import org.apache.ibatis.io.Resources; | ||
import org.apache.ibatis.migration.Migrator; | ||
import org.apache.ibatis.migration.utils.TestUtil; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.contrib.java.lang.system.Assertion; | ||
import org.junit.contrib.java.lang.system.EnvironmentVariables; | ||
import org.junit.contrib.java.lang.system.ExpectedSystemExit; | ||
import org.junit.contrib.java.lang.system.RestoreSystemProperties; | ||
import org.junit.contrib.java.lang.system.SystemOutRule; | ||
|
||
public class SystemPropertyTest { | ||
|
||
@Rule | ||
public final ExpectedSystemExit exit = ExpectedSystemExit.none(); | ||
|
||
@Rule | ||
public final SystemOutRule out = new SystemOutRule().enableLog(); | ||
|
||
@Rule | ||
public final RestoreSystemProperties restoreSysProps = new RestoreSystemProperties(); | ||
|
||
@Rule | ||
public final EnvironmentVariables envVars = new EnvironmentVariables(); | ||
|
||
private static File dir; | ||
|
||
@BeforeClass | ||
public static void init() throws Exception { | ||
dir = Resources.getResourceAsFile("org/apache/ibatis/migration/system_property/testdir"); | ||
} | ||
|
||
@Before | ||
public void beforeEachTest() { | ||
exit.expectSystemExit(); | ||
exit.checkAssertionAfterwards(new Assertion() { | ||
public void checkAssertion() { | ||
assertEquals("", out.getLog()); | ||
} | ||
}); | ||
out.clearLog(); | ||
} | ||
|
||
@After | ||
public void afterEachTest() { | ||
out.clearLog(); | ||
System.exit(0); | ||
} | ||
|
||
@Test | ||
public void testEnvironmentVariables() throws Exception { | ||
envVars.set("MIGRATIONS_DRIVER", "org.hsqldb.jdbcDriver"); | ||
envVars.set("username", "Pocahontas"); | ||
envVars.set("var1", "Variable 1"); | ||
envVars.set("MIGRATIONS_VAR3", "Variable 3"); | ||
envVars.set("migrations_var4", "Variable 4"); | ||
envVars.set("MIGRATIONS_VAR5", "Variable 5"); | ||
|
||
assertEnvironment(); | ||
} | ||
|
||
@Test | ||
public void testSystemProperties() throws Exception { | ||
System.setProperty("MIGRATIONS_DRIVER", "org.hsqldb.jdbcDriver"); | ||
System.setProperty("username", "Pocahontas"); | ||
System.setProperty("var1", "Variable 1"); | ||
System.setProperty("MIGRATIONS_VAR3", "Variable 3"); | ||
System.setProperty("migrations_var4", "Variable 4"); | ||
System.setProperty("MIGRATIONS_VAR5", "Variable 5"); | ||
// Set duplicate env vars to assert priority | ||
envVars.set("MIGRATIONS_DRIVER", "bogus_driver"); | ||
envVars.set("MIGRATIONS_VAR3", "bogus_var3"); | ||
|
||
assertEnvironment(); | ||
} | ||
|
||
private void assertEnvironment() { | ||
Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "up", "1", "--trace")); | ||
|
||
String output = out.getLog(); | ||
assertTrue(output.contains("SUCCESS")); | ||
assertTrue(output.contains("username: Pocahontas")); | ||
assertTrue(output.contains("var1: Variable 1")); | ||
assertTrue(output.contains("var2: ${var2}")); | ||
assertTrue(output.contains("var3: Variable 3")); | ||
assertTrue(output.contains("var4: Variable 4")); | ||
assertTrue(output.contains("var5: Variable 5")); | ||
assertTrue(output.contains("Var5: Var5 in properties file")); | ||
|
||
Migrator.main(TestUtil.args("--path=" + dir.getAbsolutePath(), "down", "1")); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...a/org/apache/ibatis/migration/system_property/testdir/environments/development.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
driver=should_be_overwritten_by_system_property | ||
url=jdbc:hsqldb:mem:sysprop | ||
username=${username} | ||
password= | ||
|
||
changelog=CHANGELOG | ||
|
||
var1=${var1} | ||
var2=${var2} | ||
var4=should be overwritten by system property | ||
Var5=Var5 in properties file |
49 changes: 49 additions & 0 deletions
49
...java/org/apache/ibatis/migration/system_property/testdir/scripts/001_create_changelog.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
-- | ||
-- 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. | ||
-- | ||
|
||
-- // Create Changelog | ||
|
||
-- Default DDL for changelog table that will keep | ||
-- a record of the migrations that have been run. | ||
|
||
-- You can modify this to suit your database before | ||
-- running your first migration. | ||
|
||
-- Be sure that ID and DESCRIPTION fields exist in | ||
-- BigInteger and String compatible fields respectively. | ||
|
||
CREATE TABLE ${changelog} ( | ||
ID NUMERIC(20,0) NOT NULL, | ||
APPLIED_AT VARCHAR(25) NOT NULL, | ||
DESCRIPTION VARCHAR(255) NOT NULL | ||
); | ||
|
||
ALTER TABLE ${changelog} | ||
ADD CONSTRAINT PK_${changelog} | ||
PRIMARY KEY (id); | ||
|
||
SELECT 'username: ' || USER_NAME FROM INFORMATION_SCHEMA.SYSTEM_USERS; | ||
SELECT 'var1: ' || '${var1}' FROM (VALUES(0)); | ||
SELECT 'var2: ' || '${var2}' FROM (VALUES(0)); | ||
SELECT 'var3: ' || '${var3}' FROM (VALUES(0)); | ||
SELECT 'var4: ' || '${var4}' FROM (VALUES(0)); | ||
SELECT 'var5: ' || '${var5}' FROM (VALUES(0)); | ||
SELECT 'Var5: ' || '${Var5}' FROM (VALUES(0)); | ||
|
||
|
||
-- //@UNDO | ||
|
||
DROP TABLE ${changelog}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.