Commit d7481b96 authored by Kevin Morris's avatar Kevin Morris
Browse files

modify schema primary keys to be nullable+defaulted



This fixes SQLAlchemy warnings related to primary keys not
having an auto_increment or nullable.

We've done this by making all foreign primary keys nullable.

In ApiRateLimit's case, we can set a default str to act as
a null, which seems a bit more sensible.
Signed-off-by: Kevin Morris's avatarKevin Morris <kevr@0cost.org>
parent e5df083d
from sqlalchemy.orm import mapper
from sqlalchemy.exc import IntegrityError
from aurweb.db import make_relationship
from aurweb.models.group import Group
......@@ -9,7 +10,18 @@ from aurweb.schema import PackageGroups
class PackageGroup:
def __init__(self, Package: Package = None, Group: Group = None):
self.Package = Package
if not self.Package:
raise IntegrityError(
statement="Primary key PackageID cannot be null.",
orig="PackageGroups.PackageID",
params=("NULL"))
self.Group = Group
if not self.Group:
raise IntegrityError(
statement="Primary key GroupID cannot be null.",
orig="PackageGroups.GroupID",
params=("NULL"))
properties = {
......
from sqlalchemy.orm import mapper
from sqlalchemy.exc import IntegrityError
from aurweb.db import make_relationship
from aurweb.models.package_base import PackageBase
......@@ -10,6 +11,12 @@ class PackageKeyword:
PackageBase: PackageBase = None,
Keyword: str = None):
self.PackageBase = PackageBase
if not self.PackageBase:
raise IntegrityError(
statement="Primary key PackageBaseID cannot be null.",
orig="PackageKeywords.PackageBaseID",
params=("NULL"))
self.Keyword = Keyword
......
from sqlalchemy.orm import mapper
from sqlalchemy.exc import IntegrityError
from aurweb.db import make_relationship
from aurweb.models.license import License
......@@ -9,7 +10,18 @@ from aurweb.schema import PackageLicenses
class PackageLicense:
def __init__(self, Package: Package = None, License: License = None):
self.Package = Package
if not self.Package:
raise IntegrityError(
statement="Primary key PackageID cannot be null.",
orig="PackageLicenses.PackageID",
params=("NULL"))
self.License = License
if not self.License:
raise IntegrityError(
statement="Primary key LicenseID cannot be null.",
orig="PackageLicenses.LicenseID",
params=("NULL"))
properties = {
......@@ -21,6 +33,8 @@ properties = {
PackageLicenses.c.LicenseID,
"package_license",
uselist=False)
}
mapper(PackageLicense, PackageLicenses, properties=properties,
......
......@@ -133,7 +133,7 @@ PackageBases = Table(
# Keywords of package bases
PackageKeywords = Table(
'PackageKeywords', metadata,
Column('PackageBaseID', ForeignKey('PackageBases.ID', ondelete='CASCADE'), primary_key=True, nullable=False),
Column('PackageBaseID', ForeignKey('PackageBases.ID', ondelete='CASCADE'), primary_key=True, nullable=True),
Column('Keyword', String(255), primary_key=True, nullable=False, server_default=text("''")),
mysql_engine='InnoDB',
mysql_charset='utf8mb4',
......@@ -170,8 +170,8 @@ Licenses = Table(
# Information about package-license-relations
PackageLicenses = Table(
'PackageLicenses', metadata,
Column('PackageID', ForeignKey('Packages.ID', ondelete='CASCADE'), primary_key=True, nullable=False),
Column('LicenseID', ForeignKey('Licenses.ID', ondelete='CASCADE'), primary_key=True, nullable=False),
Column('PackageID', ForeignKey('Packages.ID', ondelete='CASCADE'), primary_key=True, nullable=True),
Column('LicenseID', ForeignKey('Licenses.ID', ondelete='CASCADE'), primary_key=True, nullable=True),
mysql_engine='InnoDB',
)
......@@ -190,8 +190,8 @@ Groups = Table(
# Information about package-group-relations
PackageGroups = Table(
'PackageGroups', metadata,
Column('PackageID', ForeignKey('Packages.ID', ondelete='CASCADE'), primary_key=True, nullable=False),
Column('GroupID', ForeignKey('Groups.ID', ondelete='CASCADE'), primary_key=True, nullable=False),
Column('PackageID', ForeignKey('Packages.ID', ondelete='CASCADE'), primary_key=True, nullable=True),
Column('GroupID', ForeignKey('Groups.ID', ondelete='CASCADE'), primary_key=True, nullable=True),
mysql_engine='InnoDB',
)
......@@ -445,7 +445,7 @@ AcceptedTerms = Table(
# Rate limits for API
ApiRateLimit = Table(
'ApiRateLimit', metadata,
Column('IP', String(45), primary_key=True),
Column('IP', String(45), primary_key=True, unique=True, default=str()),
Column('Requests', INTEGER(11), nullable=False),
Column('WindowStart', BIGINT(20), nullable=False),
Index('ApiRateLimitWindowStart', 'WindowStart'),
......
......@@ -19,11 +19,9 @@ def test_api_rate_key_creation():
assert rate.WindowStart == 1
def test_api_rate_key_null_ip_raises_exception():
from aurweb.db import session
with pytest.raises(IntegrityError):
create(ApiRateLimit, Requests=10, WindowStart=1)
session.rollback()
def test_api_rate_key_ip_default():
api_rate_limit = create(ApiRateLimit, Requests=10, WindowStart=1)
assert api_rate_limit.IP == str()
def test_api_rate_key_null_requests_raises_exception():
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment