Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TRUNK-4129 Modify datatype of drug_order.quantity column and add
quantity_units column to drug_order
- Loading branch information
1 parent
a2e4eb1
commit 2e3c7cf
Showing
7 changed files
with
251 additions
and
5 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
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
80 changes: 80 additions & 0 deletions
80
...rg/openmrs/util/databasechange/MigrateDrugOrderQuantityToCodedQuantityUnitsChangeset.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,80 @@ | ||
/** | ||
* The contents of this file are subject to the OpenMRS Public License | ||
* Version 1.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://license.openmrs.org | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" | ||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing rights and limitations | ||
* under the License. | ||
* | ||
* Copyright (C) OpenMRS, LLC. All Rights Reserved. | ||
*/ | ||
package org.openmrs.util.databasechange; | ||
|
||
import liquibase.change.custom.CustomTaskChange; | ||
import liquibase.database.Database; | ||
import liquibase.database.jvm.JdbcConnection; | ||
import liquibase.exception.CustomChangeException; | ||
import liquibase.exception.DatabaseException; | ||
import liquibase.exception.SetupException; | ||
import liquibase.exception.ValidationErrors; | ||
import liquibase.resource.ResourceAccessor; | ||
import org.openmrs.util.DatabaseUtil; | ||
import org.openmrs.util.OpenmrsConstants; | ||
|
||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
|
||
/** | ||
* Applies quantity units to drug orders created pre 1.10.x based on a concept specified in a global property. | ||
* | ||
* @see OpenmrsConstants#GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS | ||
*/ | ||
public class MigrateDrugOrderQuantityToCodedQuantityUnitsChangeset implements CustomTaskChange { | ||
|
||
@Override | ||
public void execute(Database database) throws CustomChangeException { | ||
JdbcConnection connection = (JdbcConnection) database.getConnection(); | ||
|
||
Integer conceptId = DatabaseUtil.getConceptIdForUnits(connection.getUnderlyingConnection(), | ||
"drug_order_quantity_units"); | ||
|
||
try { | ||
PreparedStatement update = connection | ||
.prepareStatement("update drug_order set quantity_units = ? where quantity_units is NULL"); | ||
if (conceptId == null) { | ||
update.setNull(1, Types.INTEGER); | ||
} else { | ||
update.setInt(1, conceptId); | ||
} | ||
update.executeUpdate(); | ||
} | ||
catch (DatabaseException e) { | ||
throw new CustomChangeException(e); | ||
} | ||
catch (SQLException e) { | ||
throw new CustomChangeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public String getConfirmationMessage() { | ||
return "Finished migrating drug order quantity to coded quantity units"; | ||
} | ||
|
||
@Override | ||
public void setUp() throws SetupException { | ||
} | ||
|
||
@Override | ||
public void setFileOpener(ResourceAccessor resourceAccessor) { | ||
} | ||
|
||
@Override | ||
public ValidationErrors validate(Database database) { | ||
return null; | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
api/src/test/java/org/openmrs/util/DatabaseUtilIntegrationTest.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,58 @@ | ||
/** | ||
* The contents of this file are subject to the OpenMRS Public License | ||
* Version 1.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://license.openmrs.org | ||
* | ||
* Software distributed under the License is distributed on an "AS IS" | ||
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing rights and limitations | ||
* under the License. | ||
* | ||
* Copyright (C) OpenMRS, LLC. All Rights Reserved. | ||
*/ | ||
package org.openmrs.util; | ||
|
||
import org.hamcrest.core.Is; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.openmrs.GlobalProperty; | ||
import org.openmrs.api.AdministrationService; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.api.db.DAOException; | ||
import org.openmrs.test.BaseContextSensitiveTest; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
public class DatabaseUtilIntegrationTest extends BaseContextSensitiveTest { | ||
|
||
@Autowired | ||
AdministrationService adminService; | ||
|
||
/** | ||
* @verifies return concept_id for drug_order_quantity_units | ||
* @see DatabaseUtil#getConceptIdForUnits(java.sql.Connection, String) | ||
*/ | ||
@Test | ||
public void getConceptIdForUnits_shouldReturnConcept_idForDrug_order_quantity_units() throws Exception { | ||
adminService.saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS, | ||
"mg:5401,drug_order_quantity_units:5403,ounces:5402")); | ||
Context.flushSession(); | ||
|
||
Integer conceptId = DatabaseUtil.getConceptIdForUnits(getConnection(), "drug_order_quantity_units"); | ||
|
||
Assert.assertThat(conceptId, Is.is(5403)); | ||
} | ||
|
||
/** | ||
* @verifies fail if units is not specified | ||
* @see DatabaseUtil#getConceptIdForUnits(java.sql.Connection, String) | ||
*/ | ||
@Test(expected = DAOException.class) | ||
public void getConceptIdForUnits_shouldFailIfUnitsIsNotSpecified() throws Exception { | ||
adminService.saveGlobalProperty(new GlobalProperty(OpenmrsConstants.GP_ORDER_ENTRY_UNITS_TO_CONCEPTS_MAPPINGS, | ||
"mg:5401,ounces:5402")); | ||
Context.flushSession(); | ||
|
||
DatabaseUtil.getConceptIdForUnits(getConnection(), "drug_order_quantity_units"); | ||
} | ||
} |