-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add mybatis runtime feature * remove run_migration task from ci * update configs module * remove unused variables * remove data migration scripts * update server core version * update configs module
- Loading branch information
Showing
6 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
72 changes: 72 additions & 0 deletions
72
src/main/java/org/opensrp/migrations/MybatisMigration.java
This file contains 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,72 @@ | ||
package org.opensrp.migrations; | ||
|
||
import com.google.gson.FieldNamingPolicy; | ||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import org.apache.ibatis.migration.DataSourceConnectionProvider; | ||
import org.apache.ibatis.migration.FileMigrationLoader; | ||
import org.apache.ibatis.migration.operations.UpOperation; | ||
import org.apache.ibatis.migration.options.DatabaseOperationOption; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
import org.json.JSONObject; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.core.io.Resource; | ||
import org.springframework.core.io.support.PropertiesLoaderUtils; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.annotation.PostConstruct; | ||
import javax.sql.DataSource; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Properties; | ||
|
||
@Component | ||
public class MybatisMigration { | ||
|
||
private static Logger logger = LogManager.getLogger(MybatisMigration.class.getName()); | ||
|
||
@Autowired | ||
private DataSource dataSource; | ||
|
||
private static final String CONFIG_FILE = "mybatis/environments/deployment.properties"; | ||
|
||
private static final String SCRIPTS_FOLDER = "mybatis/scripts"; | ||
|
||
@PostConstruct | ||
public void initializeMybatisMigration() { | ||
logger.info("Running migrations."); | ||
Resource resource = new ClassPathResource(CONFIG_FILE); | ||
try { | ||
Properties props = PropertiesLoaderUtils.loadProperties(resource); | ||
Gson gson = new GsonBuilder() | ||
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) | ||
.create(); | ||
|
||
JSONObject dbOperationOptionObject = new JSONObject(gson.toJson(new DatabaseOperationOption())); | ||
for(String key : dbOperationOptionObject.keySet()){ | ||
if(props.containsKey(key)){ | ||
dbOperationOptionObject.put(key, props.getProperty(key)); | ||
} | ||
} | ||
|
||
DatabaseOperationOption databaseOperationOption = gson.fromJson(dbOperationOptionObject.toString(), DatabaseOperationOption.class); | ||
|
||
runMigrationOperation(props, databaseOperationOption); | ||
|
||
logger.info("Migration done."); | ||
} | ||
catch (IOException e) { | ||
logger.error(e); | ||
} | ||
} | ||
|
||
protected void runMigrationOperation(Properties props, DatabaseOperationOption databaseOperationOption) throws IOException { | ||
new UpOperation().operate( | ||
new DataSourceConnectionProvider(dataSource), | ||
new FileMigrationLoader(new ClassPathResource(SCRIPTS_FOLDER).getFile(), StandardCharsets.UTF_8.toString(), | ||
props), databaseOperationOption, System.out); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/test/java/org/opensrp/migrations/MybatisMigrationTest.java
This file contains 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,47 @@ | ||
package org.opensrp.migrations; | ||
|
||
import org.apache.ibatis.migration.options.DatabaseOperationOption; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.ArgumentCaptor; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.io.IOException; | ||
import java.util.Properties; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class MybatisMigrationTest { | ||
|
||
private MybatisMigration mybatisMigration; | ||
|
||
@Before | ||
public void setUp() { | ||
mybatisMigration = Mockito.spy(new MybatisMigration()); | ||
} | ||
|
||
@Test | ||
public void testInitializeMybatisMigrationShouldInvokeRunMigrationOperation() throws IOException { | ||
Mockito.doNothing().when(mybatisMigration).runMigrationOperation(Mockito.any(Properties.class), Mockito.any( | ||
DatabaseOperationOption.class)); | ||
|
||
mybatisMigration.initializeMybatisMigration(); | ||
|
||
ArgumentCaptor<Properties> propertiesArgumentCaptor = ArgumentCaptor.forClass(Properties.class); | ||
ArgumentCaptor<DatabaseOperationOption> databaseOperationOptionArgumentCaptor = ArgumentCaptor.forClass( | ||
DatabaseOperationOption.class); | ||
|
||
Mockito.verify(mybatisMigration).runMigrationOperation(propertiesArgumentCaptor.capture(), | ||
databaseOperationOptionArgumentCaptor.capture()); | ||
|
||
assertNotNull(databaseOperationOptionArgumentCaptor.getValue()); | ||
assertNotNull(propertiesArgumentCaptor.getValue()); | ||
|
||
// Has to be set to false | ||
assertFalse(databaseOperationOptionArgumentCaptor.getValue().isAutoCommit()); | ||
} | ||
} |
This file contains 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