Skip to content

Commit ab6f251

Browse files
Ensure FKs and indices in targetedms schema (#1187)
* Ensure FKs and indices in targetedms schema * code review comments * put back missing foreign keys * Typo fix --------- Co-authored-by: labkey-jeckels <jeckels@labkey.com>
1 parent 25d3393 commit ab6f251

4 files changed

Lines changed: 65 additions & 1 deletion

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SELECT core.executeJavaUpgradeCode('reparentOrphanedTargetedMSData');
2+
3+
-- Adding missing foreign keys to core.Containers(EntityId)
4+
ALTER TABLE targetedms.Runs ADD CONSTRAINT FK_Runs_Container FOREIGN KEY (Container) REFERENCES core.Containers(EntityId);
5+
ALTER TABLE targetedms.QCAnnotation ADD CONSTRAINT FK_QCAnnotation_Container FOREIGN KEY (Container) REFERENCES core.Containers(EntityId);
6+
ALTER TABLE targetedms.GuideSet ADD CONSTRAINT FK_GuideSet_Container FOREIGN KEY (Container) REFERENCES core.Containers(EntityId);
7+
ALTER TABLE targetedms.AutoQCPing ADD CONSTRAINT FK_AutoQCPing_Container FOREIGN KEY (Container) REFERENCES core.Containers(EntityId);
8+
ALTER TABLE targetedms.PrecursorChromInfo ADD CONSTRAINT FK_PrecursorChromInfo_Container FOREIGN KEY (Container) REFERENCES core.Containers(EntityId);
9+
ALTER TABLE targetedms.SampleFileChromInfo ADD CONSTRAINT FK_SampleFileChromInfo_Container FOREIGN KEY (Container) REFERENCES core.Containers(EntityId);
10+
11+
-- Adding missing indices on Container
12+
CREATE INDEX IX_QCAnnotationType_Container ON targetedms.QCAnnotationType(Container);
13+
CREATE INDEX IX_QCAnnotation_Container ON targetedms.QCAnnotation(Container);
14+
CREATE INDEX IX_GuideSet_Container ON targetedms.GuideSet(Container);
15+
CREATE INDEX IX_QCMetricConfiguration_Container ON targetedms.QCMetricConfiguration(Container);

src/org/labkey/targetedms/TargetedMSManager.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,6 +2533,17 @@ public void moveRun(TargetedMSRun run, Container newContainer, String newRunLSID
25332533

25342534
new SqlExecutor(getSchema()).execute(updatePrecChromInfoSql);
25352535

2536+
SQLFragment updateSampleFileChromInfoSql = new SQLFragment("UPDATE ");
2537+
updateSampleFileChromInfoSql.append(getTableInfoSampleFileChromInfo(), "");
2538+
updateSampleFileChromInfoSql.append(" SET container = ?").add(newContainer);
2539+
updateSampleFileChromInfoSql.append(" WHERE sampleFileId IN (");
2540+
updateSampleFileChromInfoSql.append(" SELECT sf.Id FROM ").append(getTableInfoSampleFile(), "sf");
2541+
updateSampleFileChromInfoSql.append(" INNER JOIN ").append(getTableInfoReplicate(), "rep").append(" ON rep.Id = sf.ReplicateId");
2542+
updateSampleFileChromInfoSql.append(" WHERE rep.runId = ?").add(run.getId());
2543+
updateSampleFileChromInfoSql.append(" )");
2544+
2545+
new SqlExecutor(getSchema()).execute(updateSampleFileChromInfoSql);
2546+
25362547
run.setExperimentRunLSID(newRunLSID);
25372548
run.setDataId(newDataRowId);
25382549
run.setContainer(newContainer);

src/org/labkey/targetedms/TargetedMSModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ public String getName()
231231
@Override
232232
public Double getSchemaVersion()
233233
{
234-
return 26.003;
234+
return 26.004;
235235
}
236236

237237
@Override

src/org/labkey/targetedms/TargetedMSUpgradeCode.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,42 @@ public void populatePTMPercentsGroupedPrepivotCache(final ModuleContext moduleCo
108108

109109
LOG.info("Finished populating PTMPercentsGroupedPrepivotCache for existing ExperimentMAM folders");
110110
}
111+
112+
@SuppressWarnings("UnusedDeclaration")
113+
public void reparentOrphanedTargetedMSData(final ModuleContext moduleContext)
114+
{
115+
if (moduleContext.isNewInstall())
116+
{
117+
return;
118+
}
119+
120+
Container sharedContainer = ContainerManager.getSharedContainer();
121+
122+
SqlExecutor executor = new SqlExecutor(TargetedMSManager.getSchema());
123+
124+
String[] tablesToDeleteOrphans = {"QCAnnotation", "GuideSet", "AutoQCPing"};
125+
for (String tableName : tablesToDeleteOrphans)
126+
{
127+
SQLFragment deleteSql = new SQLFragment("DELETE FROM targetedms.").append(tableName)
128+
.append(" WHERE Container NOT IN (SELECT EntityId FROM core.Containers)");
129+
int deletedCount = executor.execute(deleteSql);
130+
if (deletedCount > 0)
131+
{
132+
LOG.info("Deleted " + deletedCount + " orphaned rows from targetedms." + tableName);
133+
}
134+
}
135+
136+
String[] tablesToReparentOrphans = {"Runs", "PrecursorChromInfo", "SampleFileChromInfo"};
137+
for (String tableName : tablesToReparentOrphans)
138+
{
139+
SQLFragment updateSql = new SQLFragment("UPDATE targetedms.").append(tableName)
140+
.append(" SET Container = ? WHERE Container NOT IN (SELECT EntityId FROM core.Containers)")
141+
.add(sharedContainer.getEntityId());
142+
int updatedCount = executor.execute(updateSql);
143+
if (updatedCount > 0)
144+
{
145+
LOG.info("Reparented " + updatedCount + " orphaned rows from targetedms." + tableName + " to /Shared");
146+
}
147+
}
148+
}
111149
}

0 commit comments

Comments
 (0)