Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add warehouse.db.rows helper which returns a list of rows

  • Loading branch information...
commit 1fa86aedee1f1562411e59980e13c89cd595caa7 1 parent 125bf6a
@dstufft authored
Showing with 163 additions and 204 deletions.
  1. +12 −0 warehouse/db.py
  2. +151 −204 warehouse/packaging/models.py
View
12 warehouse/db.py
@@ -34,3 +34,15 @@ def scalar_inner(model, *args, **kwargs):
return val
return scalar_inner
+
+
+def rows(query, row_func=dict):
+ """
+ A helper function that takes a query and returns a function that will query
+ the database and return a list of rows with the row_func applied to each.
+ """
+ def inner(model, *args, **kwargs):
+ with model.engine.connect() as conn:
+ return [row_func(r) for r in conn.execute(query, *args, **kwargs)]
+
+ return inner
View
355 warehouse/packaging/models.py
@@ -37,53 +37,44 @@ class Model(models.Model):
default=0,
)
- def get_recently_updated(self, num=10):
+ get_recently_updated = db.rows(
# We only consider releases made in the last 7 days, otherwise we have
- # to do a Sequence Scan against the entire table and it takes 5+
- # seconds to complete. This shouldn't be a big deal as it is highly
- # unlikely we'll have a week without at least 10 releases.
- query = \
- """ SELECT *
- FROM (
- SELECT DISTINCT ON (name) name, version, summary, created
- FROM releases
- WHERE created >= now() - interval '7 days'
- ORDER BY name, created DESC
- ) r
- ORDER BY r.created DESC
- LIMIT %(num)s
- """
-
- with self.engine.connect() as conn:
- return [dict(r) for r in conn.execute(query, num=num)]
-
- def get_releases_since(self, since):
- query = \
- """ SELECT name, version, created, summary
+ # to do a Sequence Scan against the entire table and it takes 5+
+ # seconds to complete. This shouldn't be a big deal as it is highly
+ # unlikely we'll have a week without at least 10 releases.
+ """ SELECT *
+ FROM (
+ SELECT DISTINCT ON (name) name, version, summary, created
FROM releases
- WHERE created > %(since)s
- ORDER BY created DESC
- """
-
- with self.engine.connect() as conn:
- return [dict(r) for r in conn.execute(query, since=since)]
-
- def get_changed_since(self, since):
- query = \
- """SELECT name, max(submitted_date) FROM journals
- WHERE submitted_date > %(since)s
- GROUP BY name
- ORDER BY max(submitted_date) DESC
- """
+ WHERE created >= now() - interval '7 days'
+ ORDER BY name, created DESC
+ ) r
+ ORDER BY r.created DESC
+ LIMIT 10
+ """
+ )
- with self.engine.connect() as conn:
- return [r[0] for r in conn.execute(query, since=since)]
+ get_releases_since = db.rows(
+ """ SELECT name, version, created, summary
+ FROM releases
+ WHERE created > %s
+ ORDER BY created DESC
+ """
+ )
- def all_projects(self):
- query = "SELECT name FROM packages ORDER BY lower(name)"
+ get_changed_since = db.rows(
+ """ SELECT name, max(submitted_date) FROM journals
+ WHERE submitted_date > %s
+ GROUP BY name
+ ORDER BY max(submitted_date) DESC
+ """,
+ row_func=lambda r: r[0]
+ )
- with self.engine.connect() as conn:
- return [r["name"] for r in conn.execute(query)]
+ all_projects = db.rows(
+ "SELECT name FROM packages ORDER BY lower(name)",
+ row_func=lambda r: r["name"]
+ )
def get_top_projects(self, num=None):
query = \
@@ -107,64 +98,52 @@ def get_top_projects(self, num=None):
"""
)
- def get_projects_for_user(self, username):
- query = \
- """ SELECT DISTINCT ON (lower(name)) name, summary
- FROM (
- SELECT package_name
- FROM roles
- WHERE user_name = %(username)s
- ) roles
- INNER JOIN (
- SELECT name, summary
- FROM releases
- ORDER BY _pypi_ordering DESC
- ) releases
- ON (releases.name = roles.package_name)
- ORDER BY lower(name)
- """
-
- with self.engine.connect() as conn:
- return [dict(r) for r in conn.execute(query, username=username)]
-
- def get_users_for_project(self, project):
- query = \
- """ SELECT DISTINCT ON (u.username) u.username, u.email
- FROM (
- SELECT username, email
- FROM accounts_user
- LEFT OUTER JOIN accounts_email ON (
- accounts_email.user_id = accounts_user.id
- )
- ) u
- INNER JOIN roles ON (u.username = roles.user_name)
- WHERE roles.package_name = %(project)s
- """
-
- with self.engine.connect() as conn:
- return [dict(r) for r in conn.execute(query, project=project)]
-
- def get_roles_for_project(self, project):
- query = \
- """ SELECT user_name, role_name
+ get_projects_for_user = db.rows(
+ """ SELECT DISTINCT ON (lower(name)) name, summary
+ FROM (
+ SELECT package_name
FROM roles
- WHERE package_name = %(project)s
- ORDER BY role_name, user_name
- """
+ WHERE user_name = %s
+ ) roles
+ INNER JOIN (
+ SELECT name, summary
+ FROM releases
+ ORDER BY _pypi_ordering DESC
+ ) releases
+ ON (releases.name = roles.package_name)
+ ORDER BY lower(name)
+ """
+ )
- with self.engine.connect() as conn:
- return [dict(r) for r in conn.execute(query, project=project)]
+ get_users_for_project = db.rows(
+ """ SELECT DISTINCT ON (u.username) u.username, u.email
+ FROM (
+ SELECT username, email
+ FROM accounts_user
+ LEFT OUTER JOIN accounts_email ON (
+ accounts_email.user_id = accounts_user.id
+ )
+ ) u
+ INNER JOIN roles ON (u.username = roles.user_name)
+ WHERE roles.package_name = %s
+ """
+ )
- def get_roles_for_user(self, user):
- query = \
- """ SELECT package_name, role_name
- FROM roles
- WHERE user_name = %(user)s
- ORDER BY package_name, role_name
- """
+ get_roles_for_project = db.rows(
+ """ SELECT user_name, role_name
+ FROM roles
+ WHERE package_name = %s
+ ORDER BY role_name, user_name
+ """
+ )
- with self.engine.connect() as conn:
- return [dict(r) for r in conn.execute(query, user=user)]
+ get_roles_for_user = db.rows(
+ """ SELECT package_name, role_name
+ FROM roles
+ WHERE user_name = %s
+ ORDER BY package_name, role_name
+ """
+ )
get_hosting_mode = db.scalar(
"SELECT hosting_mode FROM packages WHERE name = %s"
@@ -184,44 +163,35 @@ def get_release_urls(self, name):
for r in conn.execute(query, project=name)
}
- def get_external_urls(self, name):
- query = \
- """ SELECT DISTINCT ON (url) url
- FROM description_urls
- WHERE name = %(project)s
- ORDER BY url
- """
-
- with self.engine.connect() as conn:
- return [r["url"] for r in conn.execute(query, project=name)]
-
- def get_file_urls(self, name):
- query = \
- """ SELECT name, filename, python_version, md5_digest
- FROM release_files
- WHERE name = %(project)s
- ORDER BY filename DESC
- """
+ get_external_urls = db.rows(
+ """ SELECT DISTINCT ON (url) url
+ FROM description_urls
+ WHERE name = %s
+ ORDER BY url
+ """,
+ row_func=lambda r: r["url"]
+ )
- with self.engine.connect() as conn:
- results = conn.execute(query, project=name)
-
- return [
- {
- "filename": r["filename"],
- "url": urlparse.urljoin(
- "/".join([
- "../../packages",
- r["python_version"],
- r["name"][0],
- r["name"],
- r["filename"],
- ]),
- "#md5={}".format(r["md5_digest"]),
- ),
- }
- for r in results
- ]
+ get_file_urls = db.rows(
+ """ SELECT name, filename, python_version, md5_digest
+ FROM release_files
+ WHERE name = %s
+ ORDER BY filename DESC
+ """,
+ lambda r: {
+ "filename": r["filename"],
+ "url": urlparse.urljoin(
+ "/".join([
+ "../../packages",
+ r["python_version"],
+ r["name"][0],
+ r["name"],
+ r["filename"],
+ ]),
+ "#md5={}".format(r["md5_digest"]),
+ ),
+ }
+ )
get_project_for_filename = db.scalar(
"SELECT name FROM release_files WHERE filename = %s"
@@ -246,16 +216,14 @@ def get_projects_with_serial(self):
with self.engine.connect() as conn:
return dict(r for r in conn.execute(query))
- def get_project_versions(self, project):
- query = \
- """ SELECT version
- FROM releases
- WHERE name = %(project)s
- ORDER BY _pypi_ordering DESC
- """
-
- with self.engine.connect() as conn:
- return [r["version"] for r in conn.execute(query, project=project)]
+ get_project_versions = db.rows(
+ """ SELECT version
+ FROM releases
+ WHERE name = %s
+ ORDER BY _pypi_ordering DESC
+ """,
+ row_func=lambda r: r["version"]
+ )
def get_downloads(self, project, version):
query = \
@@ -351,37 +319,26 @@ def get_release(self, project, version):
return result
- def get_releases(self, project):
- # Get the release data
- query = \
- """ SELECT
- name, version, author, author_email, maintainer,
- maintainer_email, home_page, license, summary, keywords,
- platform, download_url, created
- FROM releases
- WHERE name = %(project)s
- ORDER BY _pypi_ordering DESC
- """
-
- with self.engine.connect() as conn:
- results = [dict(r) for r in conn.execute(query, project=project)]
-
- return results
-
- def get_full_latest_releases(self):
- query = \
- """ SELECT DISTINCT ON (name)
- name, version, author, author_email, maintainer,
- maintainer_email, home_page, license, summary, description,
- keywords, platform, download_url, created
- FROM releases
- ORDER BY name, _pypi_ordering DESC
- """
-
- with self.engine.connect() as conn:
- results = [dict(r) for r in conn.execute(query)]
+ get_releases = db.rows(
+ """ SELECT
+ name, version, author, author_email, maintainer,
+ maintainer_email, home_page, license, summary, keywords,
+ platform, download_url, created
+ FROM releases
+ WHERE name = %s
+ ORDER BY _pypi_ordering DESC
+ """
+ )
- return results
+ get_full_latest_releases = db.rows(
+ """ SELECT DISTINCT ON (name)
+ name, version, author, author_email, maintainer,
+ maintainer_email, home_page, license, summary, description,
+ keywords, platform, download_url, created
+ FROM releases
+ ORDER BY name, _pypi_ordering DESC
+ """
+ )
def get_download_counts(self, project):
def _make_key(precision, datetime, key):
@@ -444,22 +401,17 @@ def _make_key(precision, datetime, key):
"last_month": last_30,
}
- def get_classifiers(self, project, version):
- query = \
- """ SELECT classifier
- FROM release_classifiers
- INNER JOIN trove_classifiers ON (
- release_classifiers.trove_id = trove_classifiers.id
- )
- WHERE name = %(project)s AND version = %(version)s
- ORDER BY classifier
- """
-
- with self.engine.connect() as conn:
- return [
- r["classifier"]
- for r in conn.execute(query, project=project, version=version)
- ]
+ get_classifiers = db.rows(
+ """ SELECT classifier
+ FROM release_classifiers
+ INNER JOIN trove_classifiers ON (
+ release_classifiers.trove_id = trove_classifiers.id
+ )
+ WHERE name = %s AND version = %s
+ ORDER BY classifier
+ """,
+ row_func=lambda r: r["classifier"]
+ )
def get_classifier_ids(self, classifiers):
placeholders = ', '.join(['%s'] * len(classifiers))
@@ -528,27 +480,22 @@ def get_documentation_url(self, project):
"SELECT bugtrack_url FROM packages WHERE name = %s"
)
- #
- # Mirroring support
- #
- def get_changelog(self, since):
- query = '''SELECT name, version, submitted_date, action, id
+ get_changelog = db.rows(
+ """ SELECT name, version, submitted_date, action, id
FROM journals
- WHERE journals.submitted_date > %(since)s
+ WHERE journals.submitted_date > %s
ORDER BY submitted_date DESC
- '''
- with self.engine.connect() as conn:
- return [dict(r) for r in conn.execute(query, since=since)]
+ """
+ )
get_last_changelog_serial = db.scalar(
"SELECT max(id) FROM journals"
)
- def get_changelog_serial(self, since):
- query = '''SELECT name, version, submitted_date, action, id
+ get_changelog_serial = db.rows(
+ """ SELECT name, version, submitted_date, action, id
FROM journals
- WHERE journals.id > %(since)s
+ WHERE journals.id > %s
ORDER BY submitted_date DESC
- '''
- with self.engine.connect() as conn:
- return [dict(r) for r in conn.execute(query, since=since)]
+ """
+ )
Please sign in to comment.
Something went wrong with that request. Please try again.