Skip to content

Commit f75ddef

Browse files
authored
Rework attachment handling during database migrations (#7191)
1 parent 31adbe7 commit f75ddef

File tree

59 files changed

+1112
-724
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1112
-724
lines changed

announcements/src/org/labkey/announcements/AnnouncementModule.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@
3131
import org.labkey.api.announcements.CommSchema;
3232
import org.labkey.api.announcements.api.AnnouncementService;
3333
import org.labkey.api.attachments.AttachmentService;
34+
import org.labkey.api.attachments.AttachmentType;
3435
import org.labkey.api.audit.AuditLogService;
3536
import org.labkey.api.audit.provider.MessageAuditProvider;
3637
import org.labkey.api.data.Container;
3738
import org.labkey.api.data.ContainerManager;
39+
import org.labkey.api.data.DbSchema;
3840
import org.labkey.api.data.SqlExecutor;
41+
import org.labkey.api.data.TableInfo;
3942
import org.labkey.api.message.digest.DailyMessageDigest;
4043
import org.labkey.api.message.settings.MessageConfigService;
44+
import org.labkey.api.migration.DatabaseMigrationConfiguration;
45+
import org.labkey.api.migration.DatabaseMigrationService;
46+
import org.labkey.api.migration.DefaultMigrationSchemaHandler;
4147
import org.labkey.api.module.DefaultModule;
4248
import org.labkey.api.module.ModuleContext;
4349
import org.labkey.api.rss.RSSService;
@@ -53,6 +59,7 @@
5359
import org.labkey.api.view.ViewContext;
5460
import org.labkey.api.view.WebPartFactory;
5561
import org.labkey.api.view.WebPartView;
62+
import org.labkey.api.wiki.WikiService;
5663

5764
import java.util.ArrayList;
5865
import java.util.Collection;
@@ -165,6 +172,42 @@ public void doStartup(ModuleContext moduleContext)
165172
{
166173
fsr.addFactories(new NotificationSettingsWriterFactory(), new NotificationSettingsImporterFactory());
167174
}
175+
176+
// AnnouncementModule owns the schema, so it registers the schema handler... even though it's mostly about wiki
177+
DatabaseMigrationService.get().registerSchemaHandler(new DefaultMigrationSchemaHandler(CommSchema.getInstance().getSchema())
178+
{
179+
@Override
180+
public void beforeSchema()
181+
{
182+
new SqlExecutor(getSchema()).execute("ALTER TABLE comm.Pages DROP CONSTRAINT FK_Pages_PageVersions");
183+
new SqlExecutor(getSchema()).execute("ALTER TABLE comm.Pages DROP CONSTRAINT FK_Pages_Parent");
184+
}
185+
186+
@Override
187+
public List<TableInfo> getTablesToCopy()
188+
{
189+
List<TableInfo> tablesToCopy = super.getTablesToCopy();
190+
tablesToCopy.add(CommSchema.getInstance().getTableInfoPages());
191+
tablesToCopy.add(CommSchema.getInstance().getTableInfoPageVersions());
192+
193+
return tablesToCopy;
194+
}
195+
196+
@Override
197+
public void afterSchema(DatabaseMigrationConfiguration configuration, DbSchema sourceSchema, DbSchema targetSchema)
198+
{
199+
new SqlExecutor(getSchema()).execute("ALTER TABLE comm.Pages ADD CONSTRAINT FK_Pages_PageVersions FOREIGN KEY (PageVersionId) REFERENCES comm.PageVersions (RowId)");
200+
new SqlExecutor(getSchema()).execute("ALTER TABLE comm.Pages ADD CONSTRAINT FK_Pages_Parent FOREIGN KEY (Parent) REFERENCES comm.Pages (RowId)");
201+
}
202+
203+
@Override
204+
public @NotNull Collection<AttachmentType> getAttachmentTypes()
205+
{
206+
// It's theoretically possible to deploy Announcement without Wiki, so conditionalize
207+
WikiService ws = WikiService.get();
208+
return ws != null ? List.of(AnnouncementType.get(), ws.getAttachmentType()) : List.of(AnnouncementType.get());
209+
}
210+
});
168211
}
169212

170213

announcements/src/org/labkey/announcements/model/AnnouncementType.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.labkey.announcements.model;
1717

1818
import org.jetbrains.annotations.NotNull;
19+
import org.jetbrains.annotations.Nullable;
1920
import org.labkey.api.announcements.CommSchema;
2021
import org.labkey.api.attachments.AttachmentType;
2122
import org.labkey.api.data.SQLFragment;
@@ -40,8 +41,8 @@ public static AttachmentType get()
4041
}
4142

4243
@Override
43-
public void addWhereSql(SQLFragment sql, String parentColumn, String documentNameColumn)
44+
public @Nullable SQLFragment getSelectParentEntityIdsSql()
4445
{
45-
sql.append(parentColumn).append(" IN (SELECT EntityId FROM ").append(CommSchema.getInstance().getTableInfoAnnouncements(), "ann").append(")");
46+
return new SQLFragment("SELECT EntityId FROM ").append(CommSchema.getInstance().getTableInfoAnnouncements(), "ann");
4647
}
4748
}

api/src/org/labkey/api/ApiModule.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.labkey.api.attachments.AttachmentService;
3232
import org.labkey.api.attachments.ImageServlet;
3333
import org.labkey.api.attachments.LookAndFeelResourceType;
34-
import org.labkey.api.attachments.SecureDocumentType;
3534
import org.labkey.api.audit.query.AbstractAuditDomainKind;
3635
import org.labkey.api.cache.BlockingCache;
3736
import org.labkey.api.collections.ArrayListMap;
@@ -222,7 +221,6 @@ protected void init()
222221
AttachmentService.get().registerAttachmentType(LookAndFeelResourceType.get());
223222
AttachmentService.get().registerAttachmentType(AuthenticationLogoType.get());
224223
AttachmentService.get().registerAttachmentType(AvatarType.get());
225-
AttachmentService.get().registerAttachmentType(SecureDocumentType.get());
226224

227225
PropertyManager.registerEncryptionMigrationHandler();
228226
AuthenticationManager.registerEncryptionMigrationHandler();

api/src/org/labkey/api/attachments/AttachmentService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ static AttachmentService get()
133133

134134
void registerAttachmentType(AttachmentType type);
135135

136+
/**
137+
* Returns a collection of all registered AttachmentTypes
138+
**/
139+
Collection<AttachmentType> getAttachmentTypes();
140+
136141
HttpView<?> getAdminView(ActionURL currentUrl);
137142

138143
HttpView<?> getFindAttachmentParentsView();

api/src/org/labkey/api/attachments/AttachmentType.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@
1616
package org.labkey.api.attachments;
1717

1818
import org.jetbrains.annotations.NotNull;
19+
import org.jetbrains.annotations.Nullable;
1920
import org.labkey.api.data.SQLFragment;
2021

2122
/**
2223
* Tags {@link Attachment} objects based on their intended use and what they're attached to. Does not
23-
* necessarily indicate that they are a file of a particular type/format.
24+
* indicate that they are a file of a particular type/format.
2425
*/
2526
public interface AttachmentType
2627
{
28+
SQLFragment NO_ENTITY_IDS = new SQLFragment("SELECT NULL AS EntityId WHERE 1 = 0");
29+
2730
AttachmentType UNKNOWN = new AttachmentType()
2831
{
2932
@NotNull
@@ -43,10 +46,29 @@ public void addWhereSql(SQLFragment sql, String parentColumn, String documentNam
4346
@NotNull String getUniqueName();
4447

4548
/**
46-
* Append to the where clause of a query that wants to select attachments of the implementing type
49+
* Append to the where clause of a query that wants to select attachments of the implementing type from the
50+
* core.Documents table
4751
* @param sql Implementers MUST append a valid where clause to this SQLFragment
4852
* @param parentColumn Column identifier for use in where clause. Usually represents 'core.Documents.Parent'
4953
* @param documentNameColumn Column identifier for use in where clause. Usually represents 'core.Documents.DocumentName'
5054
*/
51-
void addWhereSql(SQLFragment sql, String parentColumn, String documentNameColumn);
55+
default void addWhereSql(SQLFragment sql, String parentColumn, String documentNameColumn)
56+
{
57+
SQLFragment selectSql = getSelectParentEntityIdsSql();
58+
if (selectSql == null)
59+
throw new IllegalStateException("Must override either addWhereSql() or getSelectParentEntityIdsSql()");
60+
sql.append(parentColumn).append(" IN (").append(selectSql).append(")");
61+
}
62+
63+
/**
64+
* Return a SQLFragment that selects all the EntityIds that might be attachment parents from the table(s) that
65+
* provide attachments of this type, without involving the core.Documents table. For example,
66+
* {@code SELECT EntityId FROM comm.Announcements}. Return null if this is not-yet-implemented or inappropriate.
67+
* For example, some attachments' parents are container IDs. If the method determines that no parents exist, then
68+
* return a valid query that selects no rows, for example, {@code NO_ENTITY_IDS}.
69+
*/
70+
default @Nullable SQLFragment getSelectParentEntityIdsSql()
71+
{
72+
return null;
73+
}
5274
}

api/src/org/labkey/api/attachments/SecureDocumentType.java

Lines changed: 0 additions & 45 deletions
This file was deleted.

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

Lines changed: 0 additions & 59 deletions
This file was deleted.

0 commit comments

Comments
 (0)