Skip to content

Commit

Permalink
Map more of the database into the ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
dstufft committed Aug 3, 2015
1 parent a60a77d commit f40e734
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 35 deletions.
35 changes: 0 additions & 35 deletions warehouse/legacy/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,41 +457,6 @@
Index("rego_otk_otk_idx", rego_otk.c.otk)


release_dependencies = Table(
"release_dependencies",
db.metadata,

Column("name", Text()),
Column("version", Text()),
Column("kind", Integer()),
Column("specifier", Text()),

ForeignKeyConstraint(
["name", "version"],
["releases.name", "releases.version"],
onupdate="CASCADE",
),
)


Index("rel_dep_name_idx", release_dependencies.c.name)


Index(
"rel_dep_name_version_idx",
release_dependencies.c.name,
release_dependencies.c.version,
)


Index(
"rel_dep_name_version_kind_idx",
release_dependencies.c.name,
release_dependencies.c.version,
release_dependencies.c.kind,
)


release_requires_python = Table(
"release_requires_python",
db.metadata,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 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 primary key to Dependency
Revision ID: 5ff0c99c94
Revises: 312040efcfe
Create Date: 2015-07-20 19:50:55.153532
"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision = "5ff0c99c94"
down_revision = "312040efcfe"


def upgrade():
op.add_column(
"release_dependencies",
sa.Column(
"id",
postgresql.UUID(as_uuid=True),
server_default=sa.text("gen_random_uuid()"),
nullable=False,
),
)


def downgrade():
op.drop_column("release_dependencies", "id")
77 changes: 77 additions & 0 deletions warehouse/packaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import enum

from citext import CIText
from pyramid.threadlocal import get_current_request
from sqlalchemy import (
Expand Down Expand Up @@ -118,6 +120,55 @@ def documentation_url(self):
return request.route_url("legacy.docs", project=self.name)


class DependencyKind(enum.IntEnum):

requires = 1
provides = 2
obsoletes = 3
requires_dist = 4
provides_dist = 5
obsoletes_dist = 6
requires_external = 7

# TODO: Move project URLs into their own table, since they are not actually
# a "dependency".
project_url = 8


class Dependency(db.Model):

__tablename__ = "release_dependencies"
__table_args__ = (
Index("rel_dep_name_idx", "name"),
Index("rel_dep_name_version_idx", "name", "version"),
Index("rel_dep_name_version_kind_idx", "name", "version", "kind"),
ForeignKeyConstraint(
["name", "version"],
["releases.name", "releases.version"],
onupdate="CASCADE",
),
)
__repr__ = make_repr("name", "version", "kind", "specifier")

name = Column(Text)
version = Column(Text)
kind = Column(Integer)
specifier = Column(Text)


def _dependency_relation(kind):
return orm.relationship(
"Dependency",
primaryjoin=lambda: sql.and_(
Release.name == Dependency.name,
Release.version == Dependency.version,
Dependency.kind == kind.value,
),
lazy=False,
viewonly=True,
)


class Release(db.ModelBase):

__tablename__ = "releases"
Expand Down Expand Up @@ -188,6 +239,32 @@ def __table_args__(cls): # noqa
order_by=lambda: File.filename,
)

dependencies = orm.relationship("Dependency")

_requires = _dependency_relation(DependencyKind.requires)
requires = association_proxy("_requires", "specifier")

_provides = _dependency_relation(DependencyKind.provides)
provides = association_proxy("_provides", "specifier")

_obsoletes = _dependency_relation(DependencyKind.obsoletes)
obsoletes = association_proxy("_obsoletes", "specifier")

_requires_dist = _dependency_relation(DependencyKind.requires_dist)
requires_dist = association_proxy("_requires_dist", "specifier")

_provides_dist = _dependency_relation(DependencyKind.provides_dist)
provides_dist = association_proxy("_provides_dist", "specifier")

_obsoletes_dist = _dependency_relation(DependencyKind.obsoletes_dist)
obsoletes_dist = association_proxy("_obsoletes_dist", "specifier")

_requires_external = _dependency_relation(DependencyKind.requires_external)
requires_external = association_proxy("_requires_external", "specifier")

_project_urls = _dependency_relation(DependencyKind.project_url)
project_urls = association_proxy("_project_urls", "specifier")


class File(db.Model):

Expand Down

0 comments on commit f40e734

Please sign in to comment.