← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20478: Resource tables. Handling of missing tables.

 

------------------------------------------------------------
revno: 20478
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-10-02 16:27:58 +0200
message:
  Resource tables. Handling of missing tables.
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/dbms/DbmsManager.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/resourcetable/ResourceTable.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/jdbc/JdbcResourceTableStore.java
  dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/dbms/HibernateDbmsManager.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-api/src/main/java/org/hisp/dhis/dbms/DbmsManager.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dbms/DbmsManager.java	2015-09-16 14:49:50 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dbms/DbmsManager.java	2015-10-02 14:27:58 +0000
@@ -30,7 +30,6 @@
 
 /**
  * @author Lars Helge Overland
- * @version $Id$
  */
 public interface DbmsManager
 {
@@ -41,4 +40,6 @@
     void clearSession();
     
     void emptyTable( String table );
+    
+    boolean tableExists( String tableName );
 }

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/resourcetable/ResourceTable.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/resourcetable/ResourceTable.java	2015-10-02 13:58:22 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/resourcetable/ResourceTable.java	2015-10-02 14:27:58 +0000
@@ -58,7 +58,7 @@
     {
     }
     
-    public ResourceTable( String tableName, List<T> objects, String columnQuote )
+    protected ResourceTable( String tableName, List<T> objects, String columnQuote )
     {
         this.tableName = tableName;
         this.objects = objects;
@@ -79,13 +79,14 @@
         return tableName + TEMP_TABLE_SUFFIX;
     }
     
-    public final String getSwapTablesStatement()
-    {
-        final String sql = 
-            "drop table " + getTableName() + ";" +
-            "alter table " + getTempTableName() + " rename to " + getTableName() + ";";
-        
-        return sql;
+    public final String getDropTableStatement()
+    {
+        return "drop table " + getTableName() + ";";
+    }
+    
+    public final String getRenameTempTableStatement()
+    {
+        return "alter table " + getTempTableName() + " rename to " + getTableName() + ";";
     }
 
     // -------------------------------------------------------------------------
@@ -108,10 +109,10 @@
     public abstract Optional<List<Object[]>> getPopulateTempTableContent();
     
     /**
-     * Get a SQL index create statement. Note that the index name must have a 
-     * random component to avoid uniqueness conflicts.
+     * Creates a SQL index create statement. Note that the index name must have 
+     * a random component to avoid uniqueness conflicts.
      * 
-     * @return an optional SQL statement.
+     * @return a SQL statement.
      */
     public abstract Optional<String> getCreateIndexStatement();
 }

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/jdbc/JdbcResourceTableStore.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/jdbc/JdbcResourceTableStore.java	2015-10-02 13:58:22 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/resourcetable/jdbc/JdbcResourceTableStore.java	2015-10-02 14:27:58 +0000
@@ -38,6 +38,7 @@
 import org.hisp.dhis.dataelement.CategoryOptionGroupSet;
 import org.hisp.dhis.dataelement.DataElementCategory;
 import org.hisp.dhis.dataelement.DataElementGroupSet;
+import org.hisp.dhis.dbms.DbmsManager;
 import org.hisp.dhis.indicator.IndicatorGroupSet;
 import org.hisp.dhis.jdbc.StatementBuilder;
 import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
@@ -79,6 +80,13 @@
         this.statementBuilder = statementBuilder;
     }
 
+    private DbmsManager dbmsManager;
+
+    public void setDbmsManager( DbmsManager dbmsManager )
+    {
+        this.dbmsManager = dbmsManager;
+    }
+
     // -------------------------------------------------------------------------
     // ResourceTableStore implementation
     // -------------------------------------------------------------------------
@@ -137,7 +145,12 @@
         // Swap tables
         // ---------------------------------------------------------------------
 
-        jdbcTemplate.execute( resourceTable.getSwapTablesStatement() );
+        if ( dbmsManager.tableExists( resourceTable.getTableName() ) )
+        {
+            jdbcTemplate.execute( resourceTable.getDropTableStatement() );
+        }
+        
+        jdbcTemplate.execute( resourceTable.getRenameTempTableStatement() );
         
         log.info( "Swapped resource table, done: " + resourceTable.getTableName() );
     }

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2015-10-02 13:58:22 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2015-10-02 14:27:58 +0000
@@ -7,6 +7,7 @@
   <bean id="org.hisp.dhis.resourcetable.ResourceTableStore" class="org.hisp.dhis.resourcetable.jdbc.JdbcResourceTableStore">
     <property name="jdbcTemplate" ref="jdbcTemplate" />
     <property name="statementBuilder" ref="statementBuilder" />
+    <property name="dbmsManager" ref="dbmsManager" />
   </bean>
 
   <bean id="org.hisp.dhis.resourcetable.ResourceTableService" class="org.hisp.dhis.resourcetable.DefaultResourceTableService">

=== modified file 'dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/dbms/HibernateDbmsManager.java'
--- dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/dbms/HibernateDbmsManager.java	2015-06-08 07:58:14 +0000
+++ dhis-2/dhis-support/dhis-support-hibernate/src/main/java/org/hisp/dhis/dbms/HibernateDbmsManager.java	2015-10-02 14:27:58 +0000
@@ -28,6 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.List;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.hibernate.SessionFactory;
@@ -261,6 +263,19 @@
         }
     }
 
+    @Override
+    public boolean tableExists( String tableName )
+    {
+        final String sql = 
+            "select table_name from information_schema.tables " +
+            "where table_name = '" + tableName + "' " +
+            "and table_type = 'BASE TABLE'";
+        
+        List<Object> tables = jdbcTemplate.queryForList( sql, Object.class );
+        
+        return tables != null && tables.size() > 0;
+    }
+
     // -------------------------------------------------------------------------
     // Supportive methods
     // -------------------------------------------------------------------------