From 2e46f517359872e8a52ac5d59c37b1c3de193ed3 Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Mon, 27 Jun 2022 13:53:01 -0400 Subject: [PATCH 1/2] Add Release.is_prerelease to denormalize prerelease data --- ...add_a_column_for_denormalizing_release_.py | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 warehouse/migrations/versions/b0dbcd2f5c77_add_a_column_for_denormalizing_release_.py diff --git a/warehouse/migrations/versions/b0dbcd2f5c77_add_a_column_for_denormalizing_release_.py b/warehouse/migrations/versions/b0dbcd2f5c77_add_a_column_for_denormalizing_release_.py new file mode 100644 index 000000000000..16aa5836258b --- /dev/null +++ b/warehouse/migrations/versions/b0dbcd2f5c77_add_a_column_for_denormalizing_release_.py @@ -0,0 +1,53 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Add a column for denormalizing Release.is_prerelease + +Revision ID: b0dbcd2f5c77 +Revises: 8bee9c119e41 +Create Date: 2022-06-27 17:19:00.117464 +""" + +import sqlalchemy as sa + +from alembic import op + +revision = "b0dbcd2f5c77" +down_revision = "8bee9c119e41" + + +def upgrade(): + op.add_column("releases", sa.Column("is_prerelease", sa.Boolean(), nullable=True)) + + op.execute( + """ CREATE OR REPLACE FUNCTION maintain_releases_is_prerelease() + RETURNS TRIGGER AS $$ + BEGIN + NEW.is_prerelease := pep440_is_prerelease(NEW.version); + RETURN NEW; + END; + $$ + LANGUAGE plpgsql + """ + ) + + op.execute( + """ CREATE TRIGGER releases_update_is_prerelease + BEFORE INSERT OR UPDATE OF version ON releases + FOR EACH ROW + EXECUTE PROCEDURE maintain_releases_is_prerelease() + """ + ) + + +def downgrade(): + op.drop_column("releases", "is_prerelease") From 075fe0819a9901b608b2750d304538b2be2bc716 Mon Sep 17 00:00:00 2001 From: Donald Stufft Date: Mon, 27 Jun 2022 13:53:51 -0400 Subject: [PATCH 2/2] Migrate existing data and switch to using Release.is_prerelease --- ...f_migrate_existing_data_for_release_is_.py | 52 +++++++++++++++++++ warehouse/packaging/models.py | 2 +- 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 warehouse/migrations/versions/4490777c984f_migrate_existing_data_for_release_is_.py diff --git a/warehouse/migrations/versions/4490777c984f_migrate_existing_data_for_release_is_.py b/warehouse/migrations/versions/4490777c984f_migrate_existing_data_for_release_is_.py new file mode 100644 index 000000000000..2e7c873ef950 --- /dev/null +++ b/warehouse/migrations/versions/4490777c984f_migrate_existing_data_for_release_is_.py @@ -0,0 +1,52 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Migrate Existing Data for Release.is_prerelease + +Revision ID: 4490777c984f +Revises: b0dbcd2f5c77 +Create Date: 2022-06-27 17:49:09.835384 +""" + +import sqlalchemy as sa + +from alembic import op + +revision = "4490777c984f" +down_revision = "b0dbcd2f5c77" + + +def upgrade(): + op.execute( + """ + UPDATE releases + SET is_prerelease = pep440_is_prerelease(version) + WHERE is_prerelease IS NULL + """ + ) + op.alter_column( + "releases", + "is_prerelease", + existing_type=sa.BOOLEAN(), + server_default=sa.text("false"), + nullable=False, + ) + + +def downgrade(): + op.alter_column( + "releases", + "is_prerelease", + existing_type=sa.BOOLEAN(), + server_default=None, + nullable=True, + ) diff --git a/warehouse/packaging/models.py b/warehouse/packaging/models.py index ed377e2001fa..8fcecd8c2e5e 100644 --- a/warehouse/packaging/models.py +++ b/warehouse/packaging/models.py @@ -418,7 +418,7 @@ def __table_args__(cls): # noqa ) version = Column(Text, nullable=False) canonical_version = Column(Text, nullable=False) - is_prerelease = orm.column_property(func.pep440_is_prerelease(version)) + is_prerelease = Column(Boolean, nullable=False, server_default=sql.false()) author = Column(Text) author_email = Column(Text) maintainer = Column(Text)