mirror of
https://github.com/outline/outline.git
synced 2026-12-01 01:14:11 +03:00
fix: Complete customizable document retention
Follow-up fixes to the retention feature. The superseded CleanupDeletedDocumentsTask was still registered and ran hourly, hard deleting documents 30 days after `deletedAt` regardless of the team's configured period. Any retention longer than 30 days — including infinite — had no effect. The `destroyedAt` column was declared with a bare `@Column`. Decorator metadata is disabled in the server build, so sequelize-typescript could not infer its type and threw while loading the Document model. Documents pending permanent deletion stayed readable and restorable through `documents.info`, `documents.restore` and the MCP restore tool. They are now treated as if they no longer exist. Also: - Add trash and data retention controls to workspace security settings, the only way to configure either was to call the API directly. - Derive the retention tranches to process from the periods teams actually use, so a team holding a period that is no longer offered is not skipped. - Bound the trash expiry task by `limit`, it previously ran an unbounded UPDATE across the whole documents table every hour. - Match team preferences on JSON text rather than casting to int, so one bad value cannot fail the query for every other team in the batch. - Derive the API validation schema from the same presets the UI offers. - Send `ASC` rather than `asc` from the trash list, the sort direction was silently coerced back to descending. - Move the retention badge in with the other document badges, pluralize it, and never show a negative day count. - Drop the unused `Template.destroyedAt` computed, which returned a future date under a name meaning the opposite on the sibling model. - Rename the migration to match its position in the sequence and the column it adds. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01U3kXbDxLMJesYLM5E4P6HQ
This commit is contained in:
1 parent
750c30b470
commit
597acedf0b
25 files changed
+717
-177
No files matched your search
@@ -0,0 +1,78 @@
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { buildTeam } from "@server/test/factories";
|
||||
import { TeamPreferenceDefaults } from "@shared/constants";
|
||||
import { TeamPreference } from "@shared/types";
|
||||
import {
|
||||
getDefaultRetentionPeriod,
|
||||
getRetentionPeriodsInUse,
|
||||
} from "./retention";
|
||||
|
||||
const defaultRetentionDays = TeamPreferenceDefaults[
|
||||
TeamPreference.TrashRetentionDays
|
||||
] as number;
|
||||
|
||||
describe("getDefaultRetentionPeriod", () => {
|
||||
it("should return the configured default", () => {
|
||||
expect(
|
||||
getDefaultRetentionPeriod(TeamPreference.TrashRetentionDays)
|
||||
).toEqual(defaultRetentionDays);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getRetentionPeriodsInUse", () => {
|
||||
it("should always include the default period", async () => {
|
||||
const periods = await getRetentionPeriodsInUse(
|
||||
TeamPreference.TrashRetentionDays
|
||||
);
|
||||
expect(periods).toContain(defaultRetentionDays);
|
||||
});
|
||||
|
||||
it("should include a period configured by a team", async () => {
|
||||
const team = await buildTeam();
|
||||
team.setPreference(TeamPreference.TrashRetentionDays, 365);
|
||||
await team.save();
|
||||
|
||||
const periods = await getRetentionPeriodsInUse(
|
||||
TeamPreference.TrashRetentionDays
|
||||
);
|
||||
expect(periods).toContain(365);
|
||||
});
|
||||
|
||||
it("should exclude infinite retention", async () => {
|
||||
const team = await buildTeam();
|
||||
team.setPreference(TeamPreference.TrashRetentionDays, 0);
|
||||
await team.save();
|
||||
|
||||
const periods = await getRetentionPeriodsInUse(
|
||||
TeamPreference.TrashRetentionDays
|
||||
);
|
||||
expect(periods).not.toContain(0);
|
||||
});
|
||||
|
||||
it("should ignore values that are not a whole number of days", async () => {
|
||||
const team = await buildTeam();
|
||||
await sequelize.query(
|
||||
`UPDATE teams SET preferences = jsonb_set(coalesce(preferences, '{}'::jsonb), :path, '"nonsense"') WHERE id = :id`,
|
||||
{
|
||||
replacements: {
|
||||
path: `{${TeamPreference.TrashRetentionDays}}`,
|
||||
id: team.id,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const periods = await getRetentionPeriodsInUse(
|
||||
TeamPreference.TrashRetentionDays
|
||||
);
|
||||
expect(periods.every((days) => Number.isInteger(days) && days > 0)).toBe(
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
it("should return periods in ascending order", async () => {
|
||||
const periods = await getRetentionPeriodsInUse(
|
||||
TeamPreference.TrashRetentionDays
|
||||
);
|
||||
expect([...periods].sort((a, b) => a - b)).toEqual(periods);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,110 @@
|
||||
import type { Utils } from "sequelize";
|
||||
import { QueryTypes, Sequelize } from "sequelize";
|
||||
import { TeamPreferenceDefaults } from "@shared/constants";
|
||||
import type { RetentionPeriodPreset, TeamPreference } from "@shared/types";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
|
||||
/** Team preferences that express a retention period, in days. */
|
||||
export type RetentionPreference =
|
||||
| TeamPreference.TrashRetentionDays
|
||||
| TeamPreference.DataRetentionDays;
|
||||
|
||||
/**
|
||||
* The SQL alias of the table holding the `teamId` column being matched against.
|
||||
* Sequelize aliases a table by its model name when selecting, and by its table
|
||||
* name when updating.
|
||||
*/
|
||||
type TeamOwnedAlias = "document" | "documents";
|
||||
|
||||
/**
|
||||
* The retention period applied to teams that have not set an explicit
|
||||
* preference. Falls back to infinite retention, so a missing default can never
|
||||
* cause data to be deleted sooner than intended.
|
||||
*
|
||||
* @param preference the retention preference to read.
|
||||
* @returns the default retention period in days.
|
||||
*/
|
||||
export function getDefaultRetentionPeriod(
|
||||
preference: RetentionPreference
|
||||
): RetentionPeriodPreset {
|
||||
return TeamPreferenceDefaults[preference] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns every retention period, in days, that is currently in use across all
|
||||
* teams for the given preference. The default period is always included so that
|
||||
* teams without an explicit preference are covered, and periods of zero
|
||||
* (infinite retention) are omitted as they never require processing.
|
||||
*
|
||||
* Deriving the periods from the data rather than from a fixed list of presets
|
||||
* ensures teams are never silently skipped if the presets offered in the UI
|
||||
* change, or if a team holds a value that is no longer offered.
|
||||
*
|
||||
* @param preference the retention preference to read.
|
||||
* @returns a sorted list of retention periods in days.
|
||||
*/
|
||||
export async function getRetentionPeriodsInUse(
|
||||
preference: RetentionPreference
|
||||
): Promise<number[]> {
|
||||
const rows = await sequelize.query<{ days: string | null }>(
|
||||
`SELECT DISTINCT preferences->>:preference AS days FROM teams WHERE preferences->>:preference IS NOT NULL`,
|
||||
{
|
||||
type: QueryTypes.SELECT,
|
||||
replacements: { preference },
|
||||
}
|
||||
);
|
||||
|
||||
const periods = new Set<number>([getDefaultRetentionPeriod(preference)]);
|
||||
|
||||
for (const row of rows) {
|
||||
const days = Number(row.days);
|
||||
// Preferences are free-form JSON, values that are not a positive whole
|
||||
// number of days cannot be acted on and are ignored.
|
||||
if (Number.isInteger(days) && days > 0) {
|
||||
periods.add(days);
|
||||
}
|
||||
}
|
||||
|
||||
periods.delete(0);
|
||||
|
||||
return [...periods].sort((a, b) => a - b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a filter matching rows that belong to a team configured with the given
|
||||
* retention period. Teams that have not set the preference are matched by the
|
||||
* default period.
|
||||
*
|
||||
* Comparison is performed on the raw JSON text rather than casting to an integer
|
||||
* so that an unexpected value stored against the preference cannot fail the
|
||||
* query for every other team in the same batch.
|
||||
*
|
||||
* @param preference the retention preference to match on.
|
||||
* @param retentionDays the retention period in days.
|
||||
* @param alias the SQL alias of the table holding the `teamId` column.
|
||||
* @returns the where clause literal and the replacements it requires.
|
||||
*/
|
||||
export function teamRetentionPeriodFilter(
|
||||
preference: RetentionPreference,
|
||||
retentionDays: number,
|
||||
alias: TeamOwnedAlias
|
||||
): { where: Utils.Literal; replacements: Record<string, string> } {
|
||||
const isDefault = retentionDays === getDefaultRetentionPeriod(preference);
|
||||
const matchesPreference = isDefault
|
||||
? `(preferences->>:preference IS NULL OR preferences->>:preference = :retentionDaysText)`
|
||||
: `preferences->>:preference = :retentionDaysText`;
|
||||
|
||||
return {
|
||||
where: Sequelize.literal(
|
||||
`EXISTS (
|
||||
SELECT 1 FROM teams
|
||||
WHERE teams.id = "${alias}"."teamId"
|
||||
AND ${matchesPreference}
|
||||
)`
|
||||
),
|
||||
replacements: {
|
||||
preference,
|
||||
retentionDaysText: String(retentionDays),
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user