Error when upgrading from 17.2 to 17.3 with study.QCstate and moveQCStateToCore

LabKey Support Forum
Error when upgrading from 17.2 to 17.3 with study.QCstate and moveQCStateToCore Matt V  2017-12-06 12:06
Status: Closed
 
Working on moving from 17.2 to 17.3. When I start the development server within IntelliJ, I run into the following errors:
org.labkey.api.data.SqlScriptRunner$SqlScriptException: study-17.20-17.30.sql : Can't invoke method moveQCStateToCore(ModuleContext moduleContext) on class org.labkey.study.StudyUpgradeCode
       at org.labkey.api.data.SqlScriptManager.runScript(SqlScriptManager.java:220)
       at org.labkey.api.data.SqlScriptRunner.runScripts(SqlScriptRunner.java:82)
       at org.labkey.api.module.DefaultModule.versionUpdate(DefaultModule.java:335)
       at org.labkey.api.module.ModuleUpgrader.upgrade(ModuleUpgrader.java:83)
       at org.labkey.api.module.ModuleUpgrader.lambda$upgrade$0(ModuleUpgrader.java:95)
       at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: Can't invoke method moveQCStateToCore(ModuleContext moduleContext) on class org.labkey.study.StudyUpgradeCode
       at org.labkey.api.data.SqlScriptExecutor$JavaCodeBlock.lambda$execute$0(SqlScriptExecutor.java:211)
       at org.labkey.api.data.SqlScriptExecutor$JavaCodeBlock.execute(SqlScriptExecutor.java:228)
       at org.labkey.api.data.SqlScriptExecutor.lambda$execute$0(SqlScriptExecutor.java:101)
       at java.util.ArrayList.forEach(ArrayList.java:1249)
       at org.labkey.api.data.SqlScriptExecutor.execute(SqlScriptExecutor.java:98)
       at org.labkey.api.data.dialect.SqlDialect.runSql(SqlDialect.java:588)
       at org.labkey.api.data.SqlScriptManager.runScript(SqlScriptManager.java:215)
       ... 5 more
Caused by: java.lang.reflect.InvocationTargetException
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
       at java.lang.reflect.Method.invoke(Method.java:498)
       at org.labkey.api.data.SqlScriptExecutor$JavaCodeBlock.lambda$execute$0(SqlScriptExecutor.java:207)
       ... 11 more

Caused by: org.springframework.jdbc.UncategorizedSQLException: SqlExecutor.execute(); uncategorized SQLException for SQL []; SQL state [2BP01]; error code [0]; ERROR: cannot drop table study.qcstate because other objects depend on it
  Detail: constraint fk_mytable__qcstate on table studydataset.mytable depends on table study.qcstate

I'm not 100% sure, but I'm betting the errors are caused by the upgrade script needing to drop study.qcstate to recreate it as part of the upgrade process? If I keep going through the log I see a couple hundred similar errors. Every so often I also run into:
    Hint: Use DROP ... CASCADE to drop the dependent objects too.
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:84)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
    at org.labkey.api.data.ExceptionFramework$1.translate(ExceptionFramework.java:37)
    at org.labkey.api.data.ExceptionFramework$1.translate(ExceptionFramework.java:31)
    at org.labkey.api.data.SqlExecutor.execute(SqlExecutor.java:129)
    at org.labkey.api.data.SqlExecutor.execute(SqlExecutor.java:75)
    at org.labkey.study.StudyUpgradeCode.moveQCStateToCore(StudyUpgradeCode.java:393)
    ... 16 more

Curious if you have any suggestions or have run into similar issues to date. I can always create an upgrade script for my modules that drops the FKs ahead of time and then recreates them later, but I was hoping for an easier solution.
 
 
Matt V responded:  2017-12-09 16:16
I resolved this with following process.

1) Wrote a postgres function to grab all the relevant FKs (constraint name, schema, table, col, etc) from the info schema table and dump into a "temp" table.
2) In that same function, looped over the results from 1 to dump the FK values into another "temp" table. I say temp because its not flagged as such, but I'll drop it later.
3) Wrote a second function to remove the FKs from 1.

1,2, and 3 occur in release scripts that our process executes before the LabKey compilation.

4) Increment my module's version to 17.3. Write a 17.2-17.3.sql script that includes a trigger function that fires after insert or update on core.qcstate. This trigger grabs the data from 1 and 2 to reconstruct the FKs.

The trigger was necessary since my module's name is alphabetically before study and the upgrade script would fire before the qcstate migration one. I didn't want to hack around with naming schemes (e.g. Module: zMyUpdates), so a trigger made the most sense.
5) Remove the trigger function.

This was necessary because we register datasets with LabKey and adhere to the FK constraint definitions. The study.qcstate --> core.qcstate migration didn't account for this and exploded on the DROP table study.qcstate; query.


If I missed something, I'd still be interested in hearing about it, but this seems to have done the trick.