Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server: Fixes #9931: Add task to delete events older than a week #9988

Closed
wants to merge 4 commits into from

Conversation

tecc
Copy link

@tecc tecc commented Feb 23, 2024

This fixes #9931 through adding a task that deletes any tasks older than a week.

The task in question looks like this:

public async deleteOldEvents(beforeMillis: number = Date.now() - Week) {
	return this.withTransaction(async () => {
		await this.db(this.tableName)
			.where('created_time', '<', beforeMillis)
			.delete();
	}, 'EventModel::deleteOldEvents');
}

Copy link
Contributor

github-actions bot commented Feb 23, 2024

CLA Assistant Lite bot All contributors have signed the CLA ✍️ ✅

@tecc
Copy link
Author

tecc commented Feb 23, 2024

I have read the CLA Document and I hereby sign the CLA

github-actions bot added a commit that referenced this pull request Feb 23, 2024
Copy link
Owner

@laurent22 laurent22 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If something is batch deleting data, surely we need some tests too.

@@ -33,4 +34,11 @@ export default class EventModel extends BaseModel<Event> {
.first();
}

public async deleteOldEvents(beforeMillis: number = Date.now() - Week) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All time should be milliseconds so there's no need to append a suffix. It's only in the rare case when it's not milliseconds that we append one.

No need for the weird default argument value, please make it required.

…as an optional parameter to `EventModel::create`
@tecc
Copy link
Author

tecc commented Feb 24, 2024

Added a test for it, although as a side effect I felt it necessary to make created_time a parameter.
Making created_time a parameter makes it easier to control the timestamps of the events rather than haphazardly guessing them. The parameter could be used in the should get the latest event test as well to speed it up ever so slightly.

…ter-deletion check, forgotten negative sign
@@ -16,11 +15,11 @@ export default class EventModel extends BaseModel<Event> {
return UuidType.Native;
}

public async create(type: EventType, name = '') {
public async create(type: EventType, name = '', created_time = Date.now()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary and should be avoided. Instead you can use this in the test to simulate time:

jest
.useFakeTimers()
.setSystemTime(new Date('2020-01-01'));

See here for more information: https://stackoverflow.com/a/63377110/561309

@laurent22
Copy link
Owner

@tecc, sorry I need to close this pull request since we're trying to keep the list of PR current. However if you would like to complete the pull request please let me know and I'll reopen.

@laurent22 laurent22 closed this Mar 9, 2024
@rainforest1155
Copy link

@tecc Thank you for your work on this so far! I was wondering if you think you would be able to work on this again in the near future? I have about 210MB of data on my Joplin server right now, but the database dump amounts to almost 1000MB, which with my daily backup strategy does add up quite a bit. So I would love to have a clean up task in place as you proposed.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Server - Table events grow constantly
3 participants