Skip to content

Commit 0086d04

Browse files
committed
Rename MigrationSchemaHandler registration method. Drop a few self-join FKs before and recreate after data migration.
1 parent c805009 commit 0086d04

File tree

8 files changed

+31
-31
lines changed

8 files changed

+31
-31
lines changed

api/src/org/labkey/api/data/DatabaseMigrationService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ default void migrate(DatabaseMigrationConfiguration configuration)
4949
}
5050

5151
// By default, no-op implementations
52-
default void registerHandler(MigrationSchemaHandler schemaHandler) {}
53-
default void registerHandler(MigrationTableHandler tableHandler) {}
52+
default void registerSchemaHandler(MigrationSchemaHandler schemaHandler) {}
53+
default void registerTableHandler(MigrationTableHandler tableHandler) {}
5454

5555
Map<String, MigrationFilter> _migrationFilters = new CopyOnWriteCaseInsensitiveHashMap<>();
5656

api/src/org/labkey/api/data/SimpleFilter.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.commons.beanutils.ConversionException;
2020
import org.apache.commons.beanutils.ConvertUtils;
2121
import org.apache.commons.lang3.StringUtils;
22+
import org.apache.commons.lang3.Strings;
2223
import org.jetbrains.annotations.NotNull;
2324
import org.jetbrains.annotations.Nullable;
2425
import org.junit.Assert;
@@ -371,7 +372,7 @@ public SQLFragment toSQLFragment(String tableAlias, Map<FieldKey, ? extends Colu
371372
{
372373
var ret = toSQLFragment(columnMap, dialect);
373374
tableAlias = StringUtils.trimToEmpty(tableAlias);
374-
ret.setSqlUnsafe(StringUtils.replace(ret.getRawSQL(), STR_TABLE_ALIAS+".", tableAlias+"."));
375+
ret.setSqlUnsafe(Strings.CS.replace(ret.getRawSQL(), STR_TABLE_ALIAS + ".", tableAlias + "."));
375376
return ret;
376377
}
377378

@@ -733,8 +734,12 @@ else if (!isNegated())
733734
throw new IllegalArgumentException("Filter column '" + _fieldKey.toDisplayString() + "' not found.");
734735
}
735736

736-
in.append("((").append(alias);
737-
in.append(" ").append(isNegated() ? "NOT " : "").append("IN (");
737+
in.append(isIncludeNull() || isNegated() ? "((" : "(")
738+
.append(alias)
739+
.append(" ")
740+
.append(isNegated() ? "NOT " : "")
741+
.append("IN (");
742+
738743
String sep = "";
739744

740745
for (Object param : params)
@@ -757,7 +762,7 @@ else if (!isNegated())
757762
if (isNegated())
758763
in.append(") OR ").append(alias).append(" IS NULL)");
759764
else
760-
in.append("))");
765+
in.append(")");
761766
}
762767
}
763768

@@ -824,7 +829,7 @@ public SQLFragment toSQLFragment(Map<FieldKey, ? extends ColumnInfo> columnMap,
824829
}
825830
}
826831

827-
in.append("((");
832+
in.append(isIncludeNull() || isNegated() ? "((" : "(");
828833

829834
if (isNegated())
830835
in.append("NOT ");
@@ -846,7 +851,7 @@ public SQLFragment toSQLFragment(Map<FieldKey, ? extends ColumnInfo> columnMap,
846851
if (isNegated())
847852
in.append(") OR ").appendIdentifier(alias).append(" IS NULL)");
848853
else
849-
in.append("))");
854+
in.append(")");
850855
}
851856

852857
return in;

assay/src/org/labkey/assay/AssayModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public void moduleStartupComplete(ServletContext servletContext)
289289
svc.registerUsageMetrics(getName(), new PlateMetricsProvider());
290290
}
291291

292-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(AssayDbSchema.getInstance().getSchema())
292+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(AssayDbSchema.getInstance().getSchema())
293293
{
294294
@Override
295295
public @Nullable FieldKey getContainerFieldKey(TableInfo sourceTable)
@@ -299,7 +299,7 @@ public void moduleStartupComplete(ServletContext servletContext)
299299
});
300300

301301
// Tables in the "assaywell" provisioned schema are all single-container, so no filtering is needed
302-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(PlateMetadataDomainKind.getSchema())
302+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(PlateMetadataDomainKind.getSchema())
303303
{
304304
@Override
305305
public @Nullable FieldKey getContainerFieldKey(TableInfo sourceTable)

core/src/org/labkey/core/CoreModule.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ public void moduleStartupComplete(ServletContext servletContext)
12791279
ContainerManager.addContainerListener(new EmailPreferenceContainerListener());
12801280
UserManager.addUserListener(new EmailPreferenceUserListener());
12811281

1282-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(CoreSchema.getInstance().getSchema())
1282+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(CoreSchema.getInstance().getSchema())
12831283
{
12841284
@Override
12851285
public void beforeVerification()
@@ -1319,7 +1319,8 @@ public List<TableInfo> getTablesToCopy()
13191319
case "ContainerAliases" -> FieldKey.fromParts("ContainerRowId", "EntityId");
13201320
case "Containers" -> FieldKey.fromParts("EntityId");
13211321
case "Report" -> FieldKey.fromParts("ContainerId");
1322-
case "APIKeys", "AuthenticationConfigurations", "EmailOptions", "Logins", "ReportEngines", "ShortURL", "UsersData" -> SITE_WIDE_TABLE;
1322+
// Note: DataStates is not really site-wide, but there seem to be exp.Materials referencing DataStates with conflicting containers
1323+
case "APIKeys", "AuthenticationConfigurations", "DataStates", "EmailOptions", "Logins", "ReportEngines", "ShortURL", "UsersData" -> SITE_WIDE_TABLE;
13231324
default -> super.getContainerFieldKey(sourceTable);
13241325
};
13251326
}
@@ -1338,16 +1339,6 @@ public FilterClause getContainerClause(TableInfo sourceTable, FieldKey container
13381339
containerClause = orClause;
13391340
}
13401341

1341-
if (sourceTable.getName().equals("Documents"))
1342-
{
1343-
// If labbook module is present then filter out the rows in the root that are associated with the
1344-
// canonical attachment parent and *don't* have attachment pointers in the desired containers.
1345-
// So that would be something like:
1346-
// containerClause AND NOT (Container = <RootId> AND Parent = <CanonicalParentId> AND DocumentName NOT IN (
1347-
// SELECT hash WHERE SourceId IN (THE SIX DIFFERENT CASES OF SourceId)
1348-
// )
1349-
}
1350-
13511342
return containerClause;
13521343
}
13531344

@@ -1359,15 +1350,15 @@ public void afterSchema()
13591350
}
13601351
});
13611352

1362-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(PropertySchema.getInstance().getSchema()){
1353+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(PropertySchema.getInstance().getSchema()){
13631354
@Override
13641355
public @Nullable FieldKey getContainerFieldKey(TableInfo sourceTable)
13651356
{
13661357
return sourceTable.getName().equals("PropertySets") ? FieldKey.fromParts("ObjectId") : super.getContainerFieldKey(sourceTable);
13671358
}
13681359
});
13691360

1370-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(TestSchema.getInstance().getSchema()){
1361+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(TestSchema.getInstance().getSchema()){
13711362
@Override
13721363
public List<TableInfo> getTablesToCopy()
13731364
{
@@ -1378,7 +1369,7 @@ public List<TableInfo> getTablesToCopy()
13781369
// TODO: Temporary, until "clone" migration type copies schemas with a registered handler only
13791370
if (ModuleLoader.getInstance().getModule(DbScope.getLabKeyScope(), "vehicle") != null)
13801371
{
1381-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(DbSchema.get("vehicle", DbSchemaType.Module))
1372+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(DbSchema.get("vehicle", DbSchemaType.Module))
13821373
{
13831374
@Override
13841375
public List<TableInfo> getTablesToCopy()

experiment/src/org/labkey/experiment/ExperimentModule.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ SELECT COUNT(DISTINCT DD.DomainURI) FROM
878878
});
879879
}
880880

881-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(OntologyManager.getExpSchema())
881+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(OntologyManager.getExpSchema())
882882
{
883883
@Override
884884
public void beforeSchema()
@@ -887,6 +887,9 @@ public void beforeSchema()
887887
// Yes, the FK name is misspelled
888888
new SqlExecutor(getSchema()).execute("ALTER TABLE exp.ExperimentRun DROP CONSTRAINT FK_Run_WorfklowTask");
889889
new SqlExecutor(getSchema()).execute("ALTER TABLE exp.Object DROP CONSTRAINT FK_Object_Object");
890+
891+
// Need to drop self FK until all rows are populated because replaced runs will be inserted before their replacements
892+
new SqlExecutor(getSchema()).execute("ALTER TABLE exp.ExperimentRun DROP CONSTRAINT FK_ExperimentRun_ReplacedByRunId");
890893
}
891894

892895
@Override
@@ -940,11 +943,12 @@ public void afterSchema()
940943
{
941944
new SqlExecutor(getSchema()).execute("ALTER TABLE exp.ExperimentRun ADD CONSTRAINT FK_Run_WorfklowTask FOREIGN KEY (WorkflowTask) REFERENCES exp.ProtocolApplication (RowId) MATCH SIMPLE ON DELETE SET NULL");
942945
new SqlExecutor(getSchema()).execute("ALTER TABLE exp.Object ADD CONSTRAINT FK_Object_Object FOREIGN KEY (OwnerObjectId) REFERENCES exp.Object (ObjectId)");
946+
new SqlExecutor(getSchema()).execute("ALTER TABLE exp.ExperimentRun ADD CONSTRAINT FK_ExperimentRun_ReplacedByRunId FOREIGN KEY (ReplacedByRunId) REFERENCES exp.ExperimentRun (RowId)");
943947
}
944948
});
945949

946950
// Sample set materialized tables join on RowId to exp.Material
947-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(SampleTypeDomainKind.getSchema()) {
951+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(SampleTypeDomainKind.getSchema()) {
948952
@Override
949953
public @Nullable FieldKey getContainerFieldKey(TableInfo table)
950954
{

search/src/org/labkey/search/SearchModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void handle(Map<SearchStartupProperties, StartupPropertyEntry> properties
188188
return Collections.singletonMap("fullTextSearches", count);
189189
});
190190

191-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(SearchSchema.getInstance().getSchema())
191+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(SearchSchema.getInstance().getSchema())
192192
{
193193
@Override
194194
public List<TableInfo> getTablesToCopy()

study/src/org/labkey/study/StudyModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ SELECT COUNT(DISTINCT DD.DomainURI) FROM
530530
AdminConsole.addLink(AdminConsole.SettingsLinkType.Premium, "Master Patient Index", new ActionURL(StudyController.MasterPatientProviderAction.class, ContainerManager.getRoot()), AdminPermission.class);
531531
DataStateImportExportHelper.registerProvider(new StudyQCImportExportHelper());
532532

533-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(StudySchema.getInstance().getSchema())
533+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(StudySchema.getInstance().getSchema())
534534
{
535535
@Override
536536
public @Nullable FieldKey getContainerFieldKey(TableInfo sourceTable)
@@ -539,7 +539,7 @@ SELECT COUNT(DISTINCT DD.DomainURI) FROM
539539
}
540540
});
541541

542-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(StudySchema.getInstance().getDatasetSchema())
542+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(StudySchema.getInstance().getDatasetSchema())
543543
{
544544
@Override
545545
public @Nullable FieldKey getContainerFieldKey(TableInfo sourceTable)

wiki/src/org/labkey/wiki/WikiModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public void doStartup(ModuleContext moduleContext)
120120

121121
WikiSchema.register(this);
122122
WikiController.registerAdminConsoleLinks();
123-
DatabaseMigrationService.get().registerHandler(new DefaultMigrationSchemaHandler(CommSchema.getInstance().getSchema())
123+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(CommSchema.getInstance().getSchema())
124124
{
125125
@Override
126126
public void beforeSchema()

0 commit comments

Comments
 (0)