From c058bfd067035f0a050e5647065049efd43694c5 Mon Sep 17 00:00:00 2001 From: msramalho <19508417+msramalho@users.noreply.github.com> Date: Wed, 20 Dec 2023 18:38:28 +0000 Subject: [PATCH] fixes unique constraint issues for archives containing the same url in archive_urls --- src/db/models.py | 2 +- ...45b_modify_archive_url_to_have_uuid_id_.py | 27 ++++++++++++++++++ ...vacuum_database_if_there_s_enough_space.py | 28 +++++++++++++++++++ 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/migrations/versions/9369a264945b_modify_archive_url_to_have_uuid_id_.py create mode 100644 src/migrations/versions/93a611e4c066_vacuum_database_if_there_s_enough_space.py diff --git a/src/db/models.py b/src/db/models.py index 9cddc7f..604bbcc 100644 --- a/src/db/models.py +++ b/src/db/models.py @@ -45,8 +45,8 @@ class ArchiveUrl(Base): __tablename__ = "archive_urls" url = Column(String, primary_key=True, index=True) + archive_id = Column(String, ForeignKey("archives.id"), primary_key=True) key = Column(String, default=None) - archive_id = Column(String, ForeignKey("archives.id")) archive = relationship("Archive", back_populates="urls") diff --git a/src/migrations/versions/9369a264945b_modify_archive_url_to_have_uuid_id_.py b/src/migrations/versions/9369a264945b_modify_archive_url_to_have_uuid_id_.py new file mode 100644 index 0000000..a2b708a --- /dev/null +++ b/src/migrations/versions/9369a264945b_modify_archive_url_to_have_uuid_id_.py @@ -0,0 +1,27 @@ +"""modify archive url to have uuid id instead of url unique constraint + +Revision ID: 9369a264945b +Revises: +Create Date: 2023-12-20 17:24:59.320691 + +""" +from alembic import op + +# revision identifiers, used by Alembic. +revision = '9369a264945b' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade() -> None: + # since the primary key constraint is not named, we have to recreate it first + with op.batch_alter_table("archive_urls") as batch_op: + batch_op.create_primary_key("pk_url", ["url"]) + batch_op.drop_constraint("pk_url", type_='primary') + batch_op.create_primary_key("pk_url_archive_id", ["url", "archive_id"]) + +def downgrade() -> None: + with op.batch_alter_table("archive_urls") as batch_op: + batch_op.drop_constraint("pk_url_archive_id", type_='primary') + batch_op.create_primary_key("url", ["url"]) diff --git a/src/migrations/versions/93a611e4c066_vacuum_database_if_there_s_enough_space.py b/src/migrations/versions/93a611e4c066_vacuum_database_if_there_s_enough_space.py new file mode 100644 index 0000000..6b8d8d2 --- /dev/null +++ b/src/migrations/versions/93a611e4c066_vacuum_database_if_there_s_enough_space.py @@ -0,0 +1,28 @@ +"""vacuum database (if there's enough space) + +Revision ID: 93a611e4c066 +Revises: 9369a264945b +Create Date: 2023-12-20 18:33:27.132566 + +""" +from alembic import op + + +# revision identifiers, used by Alembic. +revision = '93a611e4c066' +down_revision = '9369a264945b' +branch_labels = None +depends_on = None + + +def upgrade() -> None: + try: + with op.get_context().autocommit_block(): + op.execute("VACUUM") + except Exception as e: + print("Unable to run vacuum, maybe there's not enough disk space. it should be 2x the size of the database") + print(e) + + +def downgrade() -> None: + pass