Skip to content

Add a new table to hold project URLs #11564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/unit/forklift/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3091,6 +3091,7 @@ def test_upload_succeeds_creates_release(
]
assert set(release.requires_dist) == {"foo", "bar (>1.0)"}
assert set(release.project_urls) == {"Test, https://example.com/"}
assert release.project_urls_new == {"Test": "https://example.com/"}
assert set(release.requires_external) == {"Cheese (>1.0)"}
assert set(release.provides) == {"testing"}
assert release.version == expected_version
Expand Down
7 changes: 7 additions & 0 deletions warehouse/forklift/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,12 @@ def file_upload(request):
request.db.add(missing_classifier)
release_classifiers.append(missing_classifier)

# Parse the Project URLs structure into a key/value dict
project_urls = {
name.strip(): url.strip()
for name, url in (us.split(",", 1) for us in form.project_urls.data)
}

release = Release(
project=project,
_classifiers=release_classifiers,
Expand Down Expand Up @@ -1151,6 +1157,7 @@ def file_upload(request):
html=rendered or "",
rendered_by=readme.renderer_version(),
),
project_urls_new=project_urls,
**{
k: getattr(form, k).data
for k in {
Expand Down
54 changes: 54 additions & 0 deletions warehouse/migrations/versions/7a8c380cefa4_add_releaseurl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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 ReleaseURL

Revision ID: 7a8c380cefa4
Revises: d1c00b634ac8
Create Date: 2022-06-10 22:02:49.522320
"""

import sqlalchemy as sa

from alembic import op
from sqlalchemy.dialects import postgresql

revision = "7a8c380cefa4"
down_revision = "d1c00b634ac8"


def upgrade():
op.create_table(
"release_urls",
sa.Column(
"id",
postgresql.UUID(as_uuid=True),
server_default=sa.text("gen_random_uuid()"),
nullable=False,
),
sa.Column("release_id", postgresql.UUID(as_uuid=True), nullable=False),
sa.Column("name", sa.String(length=32), nullable=False),
sa.Column("url", sa.Text(), nullable=False),
sa.ForeignKeyConstraint(
["release_id"], ["releases.id"], onupdate="CASCADE", ondelete="CASCADE"
),
sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint("release_id", "name"),
)
op.create_index(
op.f("ix_release_urls_release_id"), "release_urls", ["release_id"], unique=False
)


def downgrade():
op.drop_index(op.f("ix_release_urls_release_id"), table_name="release_urls")
op.drop_table("release_urls")
32 changes: 32 additions & 0 deletions warehouse/packaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
ForeignKey,
Index,
Integer,
String,
Table,
Text,
UniqueConstraint,
Expand All @@ -43,6 +44,7 @@
from sqlalchemy.ext.declarative import declared_attr # type: ignore
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import validates
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.orm.exc import MultipleResultsFound, NoResultFound
from sqlalchemy.sql import expression
from trove_classifiers import sorted_classifiers
Expand Down Expand Up @@ -323,6 +325,22 @@ class Description(db.Model):
rendered_by = Column(Text, nullable=False)


class ReleaseURL(db.Model):

__tablename__ = "release_urls"
__table_args__ = (UniqueConstraint("release_id", "name", name="uix_1"),)
__repr__ = make_repr("name", "url")

release_id = Column(
ForeignKey("releases.id", onupdate="CASCADE", ondelete="CASCADE"),
nullable=False,
index=True,
)

name = Column(String(32), nullable=False)
url = Column(Text, nullable=False)


class Release(db.Model):

__tablename__ = "releases"
Expand Down Expand Up @@ -396,6 +414,20 @@ def __table_args__(cls): # noqa
)
classifiers = association_proxy("_classifiers", "classifier")

_project_urls_new = orm.relationship(
ReleaseURL,
backref="release",
collection_class=attribute_mapped_collection("name"),
cascade="all, delete-orphan",
order_by=lambda: ReleaseURL.name.asc(),
passive_deletes=True,
)
project_urls_new = association_proxy(
"_project_urls_new",
"url",
creator=lambda k, v: ReleaseURL(name=k, url=v), # type: ignore
)

files = orm.relationship(
"File",
backref="release",
Expand Down