Fix Package not using lazyloading for some relationships

This commit is contained in:
rubenwardy 2020-12-10 17:46:10 +00:00
parent 8d8577a941
commit fb1d33d27a
5 changed files with 37 additions and 15 deletions

@ -297,17 +297,13 @@ class Package(db.Model):
issueTracker = db.Column(db.String(200), nullable=True)
forums = db.Column(db.Integer, nullable=True)
provides = db.relationship("MetaPackage",
secondary=provides, lazy="select", order_by=db.asc("name"),
back_populates="packages")
provides = db.relationship("MetaPackage", secondary=provides, order_by=db.asc("name"), back_populates="packages")
dependencies = db.relationship("Dependency", back_populates="depender", lazy="dynamic", foreign_keys=[Dependency.depender_id])
tags = db.relationship("Tag", secondary=Tags, lazy="select",
back_populates="packages")
tags = db.relationship("Tag", secondary=Tags, back_populates="packages")
content_warnings = db.relationship("ContentWarning", secondary=ContentWarnings, lazy="select",
back_populates="packages")
content_warnings = db.relationship("ContentWarning", secondary=ContentWarnings, back_populates="packages")
releases = db.relationship("PackageRelease", back_populates="package",
lazy="dynamic", order_by=db.desc("package_release_releaseDate"), cascade="all, delete, delete-orphan")
@ -315,7 +311,7 @@ class Package(db.Model):
screenshots = db.relationship("PackageScreenshot", back_populates="package",
lazy="dynamic", order_by=db.asc("package_screenshot_order"), cascade="all, delete, delete-orphan")
maintainers = db.relationship("User", secondary=maintainers, lazy="subquery")
maintainers = db.relationship("User", secondary=maintainers)
threads = db.relationship("Thread", back_populates="package", order_by=db.desc("thread_created_at"),
foreign_keys="Thread.package_id", cascade="all, delete, delete-orphan")
@ -324,13 +320,13 @@ class Package(db.Model):
cascade="all, delete, delete-orphan")
audit_log_entries = db.relationship("AuditLogEntry", foreign_keys="AuditLogEntry.package_id", back_populates="package",
order_by=db.desc("audit_log_entry_created_at"), lazy="dynamic")
order_by=db.desc("audit_log_entry_created_at"))
notifications = db.relationship("Notification", foreign_keys="Notification.package_id",
back_populates="package", cascade="all, delete, delete-orphan")
tokens = db.relationship("APIToken", foreign_keys="APIToken.package_id", back_populates="package",
lazy="dynamic", cascade="all, delete, delete-orphan")
cascade="all, delete, delete-orphan")
def __init__(self, package=None):
if package is None:
@ -398,9 +394,9 @@ class Package(db.Model):
"type": self.type.toName(),
}
def getAsDictionaryShort(self, base_url, version=None):
def getAsDictionaryShort(self, base_url, version=None, release=None):
tnurl = self.getThumbnailURL(1)
release = self.getDownloadRelease(version=version)
release = release if release else self.getDownloadRelease(version=version)
return {
"name": self.name,
"title": self.title,

@ -53,7 +53,7 @@ class Thread(db.Model):
replies = db.relationship("ThreadReply", back_populates="thread", lazy="dynamic",
order_by=db.asc("thread_reply_id"), cascade="all, delete, delete-orphan")
watchers = db.relationship("User", secondary=watchers, lazy="subquery", backref="watching")
watchers = db.relationship("User", secondary=watchers, backref="watching")
def getViewURL(self):
return url_for("threads.view", id=self.id)

@ -71,7 +71,6 @@ class QueryBuilder:
return MinetestRelease.get(self.minetest_version, self.protocol_version)
def buildPackageQuery(self):
query = None
if self.order_by == "last_release":
query = db.session.query(Package).select_from(PackageRelease).join(Package) \
.filter_by(state=PackageState.APPROVED)

@ -2,4 +2,4 @@
# Open SQL console for the database
docker exec -it contentdb_db_1 sh -c "psql contentdb contentdb"
docker exec -it contentdb_db_1 psql contentdb contentdb

27
utils/restore_bk.sh Executable file

@ -0,0 +1,27 @@
#!/bin/bash
# Restores backup
set -e
if [ -z "$1" ]; then
echo "Usage: ./utils/restore_bk.sh path/to/backup.sql"
exit 1
fi
BKFILE=$1
if [[ ! -f "$BKFILE" ]]; then
echo "No such file: $BKFILE"
exit 1
fi
if [[ "$BKFILE" == *.gpg ]]; then
IN=$BKFILE
BKFILE=/tmp/$(basename "$BKFILE" .gpg)
echo "Decrypting backup at $IN to $BKFILE"
gpg --decrypt "$IN" > "$BKFILE"
fi
echo "Importing backup from $BKFILE"
cat $BKFILE | docker exec -i contentdb_db_1 psql contentdb contentdb