← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13595: Table alteror, found a more reliable way of detecting if attributeoptioncomboid upgrade script ha...

 

------------------------------------------------------------
revno: 13595
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-01-06 18:25:34 +0100
message:
  Table alteror, found a more reliable way of detecting if attributeoptioncomboid upgrade script has run. Using information_schema to detect number of columns involved in primary key in datavalue table.
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/AbstractStatementBuilder.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java	2014-01-06 14:13:52 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java	2014-01-06 17:25:34 +0000
@@ -28,6 +28,13 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import org.amplecode.quick.BatchHandler;
 import org.amplecode.quick.BatchHandlerFactory;
 import org.amplecode.quick.StatementHolder;
@@ -42,15 +49,6 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.transaction.annotation.Transactional;
 
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.sql.Statement;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 /**
  * @author Lars Helge Overland
  */
@@ -676,9 +674,13 @@
 
     private void upgradeDataValuesWithAttributeOptionCombo()
     {
-        if ( columnExists( "datavalue", "attributeoptioncomboid" ) )
+        final String sql = statementBuilder.getNumberOfColumnsInPrimaryKey( "datavalue" );
+        
+        Integer no = statementManager.getHolder().queryForInteger( sql );
+        
+        if ( no >= 5 )
         {
-            return;
+            return; // attributeoptioncomboid already part of datavalue primary key
         }
 
         int optionComboId = getDefaultOptionCombo();
@@ -1052,29 +1054,6 @@
         }
     }
 
-    private boolean columnExists( String table, String column )
-    {
-        try
-        {
-            ResultSetMetaData metaData = statementManager.getHolder().getStatement().executeQuery( "select * from datavalue limit 1" ).getMetaData();
-
-            for ( int i = 1; i <= metaData.getColumnCount(); i++ )
-            {
-                if ( column.equalsIgnoreCase( metaData.getColumnName( i ) ) )
-                {
-                    return true;
-                }
-            }
-        }
-        catch ( SQLException ex )
-        {
-            log.error( "Column detection failed: " + ex.getMessage() );
-            log.error( ex );
-        }
-
-        return false;
-    }
-
     private Integer getDefaultOptionCombo()
     {
         String sql =

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java	2013-11-18 13:30:37 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/StatementBuilder.java	2014-01-06 17:25:34 +0000
@@ -157,6 +157,11 @@
     String getCreateOrgUnitDataSetCompletenessTable();
     
     /**
+     * Returns the number of columns part of the primary key for the given table.
+     */
+    String getNumberOfColumnsInPrimaryKey( String table );
+    
+    /**
      * Creates a delete datavalue statement.
      * @return a delete datavalue statement.
      */

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/AbstractStatementBuilder.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/AbstractStatementBuilder.java	2013-10-11 08:27:42 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/statementbuilder/AbstractStatementBuilder.java	2014-01-06 17:25:34 +0000
@@ -173,4 +173,19 @@
             "value " + getDoubleColumnType() + ", " +
             "valueOnTime " + getDoubleColumnType() + " );";
     }
+
+    @Override
+    public String getNumberOfColumnsInPrimaryKey( String table )
+    {
+        return
+            "select count(cu.column_name) from information_schema.key_column_usage cu " +
+            "inner join information_schema.table_constraints tc  " +
+            "on cu.constraint_catalog=tc.constraint_catalog " +
+                "and cu.constraint_schema=tc.constraint_schema " +
+                "and cu.constraint_name=tc.constraint_name " +
+                "and cu.table_schema=tc.table_schema " +
+                "and cu.table_name=tc.table_name " +
+            "where tc.constraint_type='PRIMARY KEY' " +
+            "and cu.table_name='" + table + "';";
+    }
 }