From 22c46bea746ba2ec800a760a3f83e0a8b6278932 Mon Sep 17 00:00:00 2001 From: Wim De Clercq Date: Tue, 30 Jul 2024 09:15:06 +0200 Subject: [PATCH] Alter ConceptVisitLog concept_id to string. Issue #906 --- ...43_alter_conceptvisitlog_concept_id_to_.py | 30 ++++++++++++++++ .../88214d86a9d1_concept_id_is_string.py | 5 ++- atramhasis/data/models.py | 2 +- tests/test_datamanagers.py | 36 +++++++++---------- 4 files changed, 52 insertions(+), 21 deletions(-) create mode 100644 atramhasis/alembic/versions/29306f749043_alter_conceptvisitlog_concept_id_to_.py diff --git a/atramhasis/alembic/versions/29306f749043_alter_conceptvisitlog_concept_id_to_.py b/atramhasis/alembic/versions/29306f749043_alter_conceptvisitlog_concept_id_to_.py new file mode 100644 index 00000000..6b2059b3 --- /dev/null +++ b/atramhasis/alembic/versions/29306f749043_alter_conceptvisitlog_concept_id_to_.py @@ -0,0 +1,30 @@ +"""alter ConceptVisitLog concept_id to string + +Revision ID: 29306f749043 +Revises: b2a7d7614973 +Create Date: 2024-07-30 09:12:37.521748 + +""" + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '29306f749043' +down_revision = 'b2a7d7614973' + + +def upgrade(): + with op.batch_alter_table('concept_visit_log') as batch_op: + batch_op.alter_column('concept_id', type_=sa.String, existing_type=sa.Integer) + + +def downgrade(): + # Drop concept_visit_log which have non-integer concept_id + op.execute( + "DELETE FROM concept_visit_log " + "WHERE cast(cast(concept_id AS INTEGER) AS TEXT) != concept_id" + ) + with op.batch_alter_table('concept_visit_log') as batch_op: + batch_op.alter_column('concept_id', type_=sa.Integer, existing_type=sa.String) diff --git a/atramhasis/alembic/versions/88214d86a9d1_concept_id_is_string.py b/atramhasis/alembic/versions/88214d86a9d1_concept_id_is_string.py index ecaa5a72..19374586 100644 --- a/atramhasis/alembic/versions/88214d86a9d1_concept_id_is_string.py +++ b/atramhasis/alembic/versions/88214d86a9d1_concept_id_is_string.py @@ -22,6 +22,9 @@ def upgrade(): def downgrade(): # Drop concepts which have non-integer concept_id - op.execute("DELETE FROM concept WHERE cast(cast(concept_id AS INTEGER) AS TEXT) != concept_id") + op.execute( + "DELETE FROM concept " + "WHERE cast(cast(concept_id AS INTEGER) AS TEXT) != concept_id" + ) with op.batch_alter_table('concept') as batch_op: batch_op.alter_column('concept_id', existing_type=sa.String, type_=sa.Integer) diff --git a/atramhasis/data/models.py b/atramhasis/data/models.py index a00bcf84..ca3b3876 100644 --- a/atramhasis/data/models.py +++ b/atramhasis/data/models.py @@ -39,7 +39,7 @@ class ConceptschemeVisitLog(Base): class ConceptVisitLog(Base): __tablename__ = 'concept_visit_log' id = Column(Integer, primary_key=True, autoincrement=True) - concept_id = Column(Integer, nullable=False) + concept_id = Column(String, nullable=False) conceptscheme_id = Column(String(25), nullable=False) visited_at = Column(DateTime, default=func.now(), nullable=False) origin = Column(String(25), nullable=False) diff --git a/tests/test_datamanagers.py b/tests/test_datamanagers.py index b06d57b2..ec70fc17 100644 --- a/tests/test_datamanagers.py +++ b/tests/test_datamanagers.py @@ -178,36 +178,34 @@ def test_get_first_day(self): @patch('atramhasis.data.datamanagers.date', Mock(today=Mock(return_value=date(2015, 9, 15)))) def test_get_most_popular_concepts_for_conceptscheme(self): self.session.add( - ConceptVisitLog(concept_id=1, conceptscheme_id='1', origin='REST', + ConceptVisitLog(concept_id='1', conceptscheme_id='1', origin='REST', visited_at=datetime(2015, 8, 27, 10, 58, 3)) ) self.session.add( - ConceptVisitLog(concept_id=1, conceptscheme_id='1', origin='REST', + ConceptVisitLog(concept_id='1', conceptscheme_id='1', origin='REST', visited_at=datetime(2015, 8, 27, 11, 58, 3)) ) self.session.add( - ConceptVisitLog(concept_id=2, conceptscheme_id='1', origin='REST', + ConceptVisitLog(concept_id='2', conceptscheme_id='1', origin='REST', visited_at=datetime(2015, 8, 27, 10, 58, 3)) ) self.session.add( - ConceptVisitLog(concept_id=2, conceptscheme_id='2', origin='REST', + ConceptVisitLog(concept_id='2', conceptscheme_id='2', origin='REST', visited_at=datetime(2015, 8, 27, 10, 58, 3)) ) - self.assertListEqual( - [ - {'concept_id': 1, 'scheme_id': 1}, - {'concept_id': 2, 'scheme_id': 1} - ], - self.audit_manager.get_most_popular_concepts_for_conceptscheme( - 1, 5, 'last_month' - ) - ) - self.assertListEqual([{'concept_id': 2, 'scheme_id': 2}], - self.audit_manager.get_most_popular_concepts_for_conceptscheme(2, 5, 'last_month')) - self.assertListEqual([{'concept_id': 1, 'scheme_id': 1}], - self.audit_manager.get_most_popular_concepts_for_conceptscheme(1, 1, 'last_month')) - self.assertListEqual([], - self.audit_manager.get_most_popular_concepts_for_conceptscheme(1, 5, 'last_day')) + + manager = self.audit_manager + result = manager.get_most_popular_concepts_for_conceptscheme(1, 5, 'last_month') + expected = [ + {'concept_id': '1', 'scheme_id': 1}, {'concept_id': '2', 'scheme_id': 1} + ] + self.assertListEqual(expected, result) + result = manager.get_most_popular_concepts_for_conceptscheme(2, 5, 'last_month') + self.assertListEqual([{'concept_id': '2', 'scheme_id': 2}], result) + result = manager.get_most_popular_concepts_for_conceptscheme(1, 1, 'last_month') + self.assertListEqual([{'concept_id': '1', 'scheme_id': 1}], result) + result = manager.get_most_popular_concepts_for_conceptscheme(1, 5, 'last_day') + self.assertListEqual([], result) class CountsManagerTest(DbTest):