From ee78d288b355d9c34bceef241c2b274560c1db99 Mon Sep 17 00:00:00 2001 From: Jumana B Date: Tue, 23 Jan 2024 09:05:51 -0500 Subject: [PATCH] Added a last_used column for the api_key (#2091) * Added a last_used column for the api_key * added migration file --------- Co-authored-by: William B <7444334+whabanks@users.noreply.github.com> --- app/models.py | 1 + .../0443_add_apikey_last_used_column.py | 22 +++++++++++++++++++ tests/app/dao/test_api_key_dao.py | 1 + 3 files changed, 24 insertions(+) create mode 100644 migrations/versions/0443_add_apikey_last_used_column.py diff --git a/app/models.py b/app/models.py index fd640e1473..704ccf798a 100644 --- a/app/models.py +++ b/app/models.py @@ -918,6 +918,7 @@ class ApiKey(BaseModel, Versioned): created_by = db.relationship("User") created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey("users.id"), index=True, nullable=False) compromised_key_info = db.Column(JSONB(none_as_null=True), nullable=True, default={}) + last_used_timestamp = db.Column(db.DateTime, index=False, unique=False, nullable=True, default=None) __table_args__ = ( Index( diff --git a/migrations/versions/0443_add_apikey_last_used_column.py b/migrations/versions/0443_add_apikey_last_used_column.py new file mode 100644 index 0000000000..23d532d983 --- /dev/null +++ b/migrations/versions/0443_add_apikey_last_used_column.py @@ -0,0 +1,22 @@ +""" +Revision ID: 0443_add_apikey_last_used_column +Revises: 0442_add_heartbeat_templates +Create Date: 2022-09-21 00:00:00 +""" +from datetime import datetime + +import sqlalchemy as sa +from alembic import op + +revision = "0443_add_apikey_last_used_column" +down_revision = "0442_add_heartbeat_templates" + + +def upgrade(): + op.add_column("api_keys", sa.Column("last_used_timestamp", sa.DateTime(), nullable=True)) + op.add_column("api_keys_history", sa.Column("last_used_timestamp", sa.DateTime(), nullable=True)) + + +def downgrade(): + op.drop_column("api_keys", "last_used_timestamp") + op.drop_column("api_keys_history", "last_used_timestamp") diff --git a/tests/app/dao/test_api_key_dao.py b/tests/app/dao/test_api_key_dao.py index 9535ce0343..7642325ded 100644 --- a/tests/app/dao/test_api_key_dao.py +++ b/tests/app/dao/test_api_key_dao.py @@ -35,6 +35,7 @@ def test_save_api_key_should_create_new_api_key_and_history(sample_service): assert len(all_api_keys) == 1 assert all_api_keys[0] == api_key assert api_key.version == 1 + assert api_key.last_used_timestamp is None all_history = api_key.get_history_model().query.all() assert len(all_history) == 1