-
Notifications
You must be signed in to change notification settings - Fork 32
CV3-47 Use celery to delete archived data that is over 30 days old #510
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """include cascading deletion on room tags | ||
|
|
||
| Revision ID: c058d462a21d | ||
| Revises: 98e4dbfc868a | ||
| Create Date: 2019-11-04 17:45:56.163962 | ||
|
|
||
| """ | ||
| from alembic import op | ||
| import sqlalchemy as sa | ||
|
|
||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = 'c058d462a21d' | ||
| down_revision = '98e4dbfc868a' | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| op.drop_constraint('room_tags_tag_id_fkey', | ||
| 'room_tags', type_='foreignkey') | ||
| op.create_foreign_key(None, 'room_tags', 'tags', [ | ||
| 'tag_id'], ['id'], ondelete='CASCADE') | ||
| op.drop_constraint('room_tags_room_id_fkey', | ||
| 'room_tags', type_='foreignkey') | ||
| op.create_foreign_key(None, 'room_tags', 'rooms', [ | ||
| 'room_id'], ['id'], ondelete='CASCADE') | ||
|
|
||
|
|
||
| def downgrade(): | ||
| op.drop_constraint('room_tags_room_id_fkey', | ||
| 'room_tags', type_='foreignkey') | ||
| op.create_foreign_key('room_tags_room_id_fkey', 'room_tags', | ||
| 'rooms', ['room_id'], ['id']) | ||
| op.drop_constraint('room_tags_tag_id_fkey', 'room_tags', type_='foreignkey') | ||
| op.create_foreign_key('room_tags_tag_id_fkey', 'room_tags', | ||
| 'tags', ['tag_id'], ['id']) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| from helpers.database import database_uri | ||
| from sqlalchemy import create_engine, MetaData, and_ | ||
| from datetime import timedelta, datetime | ||
| import celery | ||
|
|
||
|
|
||
| @celery.task(name='clean_archived_data.delete_archived_data') | ||
| def delete_archived_data(): | ||
| """ | ||
| This method deletes data that has been deleted for | ||
| more than 30 days | ||
| """ | ||
| database_engine = create_engine(database_uri) | ||
| metadata = MetaData() | ||
| metadata.reflect(bind=database_engine) | ||
|
|
||
| for table in reversed(metadata.sorted_tables): | ||
| try: | ||
| now = datetime.now() | ||
| delta = now - timedelta(days=30) | ||
| statement = table.delete().where( | ||
| and_(table.c.date_updated < delta, table.c.state == 'archived')) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mifeille after finishing the job, this is what celery.err.log is saying
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a migration to include cascading deletion I guess that one on room tags was forgotten, I am going to add it. |
||
| database_engine.execute(statement) | ||
| except AttributeError: | ||
| continue | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you please put in a docstring which explains what this function, specifically to delete data which are 30 days old with also an archived state.