Skip to content

Conversation

@mifeille
Copy link
Contributor

@mifeille mifeille commented Nov 2, 2019

Description

Currently, the Backend implements soft deletes which instead of wiping a record from the database, changes the state of a row from active to archived. We would like to periodically remove rows whose state is archived from the database.

Type of change

Please select the relevant option

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Others (cosmetics, styling, improvements)
  • This change requires a documentation update

Checklist

  • My changes generate no new warnings
  • My code follows the style guidelines of this project
  • I have linted my code prior to submission
  • Existing unit tests pass locally with my changes
  • Implementation works according to expectations

JIRA

CV3-47

@mifeille mifeille force-pushed the story/CV3-47-delete-archived-data branch 9 times, most recently from 113a74d to 89c5f49 Compare November 2, 2019 21:38
@mifeille mifeille added the wip label Nov 2, 2019
@mifeille mifeille force-pushed the story/CV3-47-delete-archived-data branch from 89c5f49 to cd4b237 Compare November 3, 2019 10:23
@mifeille mifeille force-pushed the story/CV3-47-delete-archived-data branch 2 times, most recently from 5441f22 to fe09586 Compare November 4, 2019 06:59

@celery.task(name='clean_archived_data.delete_archived_data')
def delete_archived_data():
database_engine = create_engine(database_uri)

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.

now = datetime.now()
delta = now - timedelta(days=30)
statement = table.delete().where(
and_(table.c.date_updated < delta, table.c.state == 'archived'))

Choose a reason for hiding this comment

The 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 sqlalchemy.exc.IntegrityError: (psycopg2.IntegrityError) update or delete on table "rooms" violates foreign key constraint "room_tags_room_id_fkey" on table "room_tags" DETAIL: Key (id)=(1) is still referenced from table "room_tags". [SQL: 'DELETE FROM rooms WHERE rooms.date_updated < %(date_updated_1)s AND rooms.state = %(state_1)s'] [parameters: {'date_updated_1': datetime.datetime(2019, 10, 5, 14, 31, 1, 378610), 'state_1': 'archived'}] (Background on this error at: http://sqlalche.me/e/gkpj)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

@mifeille mifeille force-pushed the story/CV3-47-delete-archived-data branch 11 times, most recently from 25d2812 to 0e2b3fd Compare November 6, 2019 09:29
@mifeille mifeille force-pushed the story/CV3-47-delete-archived-data branch 3 times, most recently from 0b05269 to 7216ae3 Compare November 14, 2019 08:45
@joshuaocero joshuaocero self-requested a review November 14, 2019 11:00
Copy link
Contributor

@joshuaocero joshuaocero left a comment

Choose a reason for hiding this comment

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

@mifeille this looks really good. Please remove the print statement so I can merge.

database_engine = create_engine(database_uri)
metadata = MetaData()
metadata.reflect(bind=database_engine)
print('deleting archived data...')
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this debug statement.

- configure celery beat
- implement use celery to delete archived data that is over 30 days old
[Delivers CV3-47]
@mifeille mifeille force-pushed the story/CV3-47-delete-archived-data branch from 7216ae3 to 8df72b2 Compare November 14, 2019 11:08
@joshuaocero joshuaocero merged commit a38685b into develop Nov 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants