From dc83f6a932aeb8d41b4ea1e7589ab2aa048bc261 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Wed, 28 Feb 2024 16:14:16 +0100 Subject: [PATCH] refactor: make package_versions.meta NOT NULL (#10) --- ...3b0b332edd91109da403e973d321c13aec550.json | 2 +- ...3352a37352bff3bfd7c7a3dc338bf83248408.json | 2 +- ...16c78bb364d62c460aec1f36aaf66cba69afb.json | 2 +- ...3051e7d068026c35eb136034cd465af54ddd1.json | 2 +- ...f140c6323d2d53554fa9c80a5bfe24cbb1f81.json | 2 +- ...9b3b5df437a3d9fc8b4be2303ecbbbb4c283f.json | 2 +- .../20240228112319_scoring_not_null.sql | 1 + api/src/db/database.rs | 108 +++--------------- 8 files changed, 22 insertions(+), 99 deletions(-) create mode 100644 api/migrations/20240228112319_scoring_not_null.sql diff --git a/api/.sqlx/query-5bb6821b0440eafed6005ecb6073b0b332edd91109da403e973d321c13aec550.json b/api/.sqlx/query-5bb6821b0440eafed6005ecb6073b0b332edd91109da403e973d321c13aec550.json index bdb115e6..744c8085 100644 --- a/api/.sqlx/query-5bb6821b0440eafed6005ecb6073b0b332edd91109da403e973d321c13aec550.json +++ b/api/.sqlx/query-5bb6821b0440eafed6005ecb6073b0b332edd91109da403e973d321c13aec550.json @@ -80,7 +80,7 @@ false, false, false, - true, + false, false, false, true diff --git a/api/.sqlx/query-6bcee848fcbb540b0c59c0755643352a37352bff3bfd7c7a3dc338bf83248408.json b/api/.sqlx/query-6bcee848fcbb540b0c59c0755643352a37352bff3bfd7c7a3dc338bf83248408.json index 076129be..7d2b1579 100644 --- a/api/.sqlx/query-6bcee848fcbb540b0c59c0755643352a37352bff3bfd7c7a3dc338bf83248408.json +++ b/api/.sqlx/query-6bcee848fcbb540b0c59c0755643352a37352bff3bfd7c7a3dc338bf83248408.json @@ -76,7 +76,7 @@ false, false, false, - true, + false, false, false, true diff --git a/api/.sqlx/query-92e4033d0a82242fbf61c4c7fe716c78bb364d62c460aec1f36aaf66cba69afb.json b/api/.sqlx/query-92e4033d0a82242fbf61c4c7fe716c78bb364d62c460aec1f36aaf66cba69afb.json index 565f887e..1237b9bf 100644 --- a/api/.sqlx/query-92e4033d0a82242fbf61c4c7fe716c78bb364d62c460aec1f36aaf66cba69afb.json +++ b/api/.sqlx/query-92e4033d0a82242fbf61c4c7fe716c78bb364d62c460aec1f36aaf66cba69afb.json @@ -81,7 +81,7 @@ false, false, false, - true, + false, false, false, true diff --git a/api/.sqlx/query-ad72a71d4ed7fe4d4e9857af1713051e7d068026c35eb136034cd465af54ddd1.json b/api/.sqlx/query-ad72a71d4ed7fe4d4e9857af1713051e7d068026c35eb136034cd465af54ddd1.json index 6a31486c..123bc2d4 100644 --- a/api/.sqlx/query-ad72a71d4ed7fe4d4e9857af1713051e7d068026c35eb136034cd465af54ddd1.json +++ b/api/.sqlx/query-ad72a71d4ed7fe4d4e9857af1713051e7d068026c35eb136034cd465af54ddd1.json @@ -85,7 +85,7 @@ false, false, false, - true, + false, false, false, true diff --git a/api/.sqlx/query-c0bee504ba48a093f7777d900bff140c6323d2d53554fa9c80a5bfe24cbb1f81.json b/api/.sqlx/query-c0bee504ba48a093f7777d900bff140c6323d2d53554fa9c80a5bfe24cbb1f81.json index 5a594562..2576f13f 100644 --- a/api/.sqlx/query-c0bee504ba48a093f7777d900bff140c6323d2d53554fa9c80a5bfe24cbb1f81.json +++ b/api/.sqlx/query-c0bee504ba48a093f7777d900bff140c6323d2d53554fa9c80a5bfe24cbb1f81.json @@ -79,7 +79,7 @@ false, false, false, - true, + false, false, false, true diff --git a/api/.sqlx/query-c318a55a59b024b7ccddc955d719b3b5df437a3d9fc8b4be2303ecbbbb4c283f.json b/api/.sqlx/query-c318a55a59b024b7ccddc955d719b3b5df437a3d9fc8b4be2303ecbbbb4c283f.json index c3888205..5f00675e 100644 --- a/api/.sqlx/query-c318a55a59b024b7ccddc955d719b3b5df437a3d9fc8b4be2303ecbbbb4c283f.json +++ b/api/.sqlx/query-c318a55a59b024b7ccddc955d719b3b5df437a3d9fc8b4be2303ecbbbb4c283f.json @@ -104,7 +104,7 @@ false, false, false, - true, + false, false, false, true, diff --git a/api/migrations/20240228112319_scoring_not_null.sql b/api/migrations/20240228112319_scoring_not_null.sql new file mode 100644 index 00000000..b4acd41d --- /dev/null +++ b/api/migrations/20240228112319_scoring_not_null.sql @@ -0,0 +1 @@ +ALTER TABLE package_versions ALTER COLUMN meta SET NOT NULL; diff --git a/api/src/db/database.rs b/api/src/db/database.rs index 0b296ee4..9c23c36f 100644 --- a/api/src/db/database.rs +++ b/api/src/db/database.rs @@ -1129,28 +1129,13 @@ impl Database { .fetch_all(&self.pool) .await?; - let updated = sqlx::query!( + let updated = sqlx::query_as!( + PackageVersion, r#"SELECT scope as "scope: ScopeName", name as "name: PackageName", version as "version: Version", user_id, readme_path as "readme_path: PackagePath", exports as "exports: ExportsMap", is_yanked, uses_npm, meta as "meta: PackageVersionMeta", updated_at, created_at, rekor_log_id FROM package_versions ORDER BY package_versions.created_at DESC LIMIT 10"#, ) - .map(|r| { - PackageVersion { - scope: r.scope, - name: r.name, - version: r.version, - user_id: r.user_id, - exports: r.exports, - is_yanked: r.is_yanked, - readme_path: r.readme_path, - uses_npm: r.uses_npm, - meta: r.meta.unwrap_or_default(), - rekor_log_id: r.rekor_log_id, - updated_at: r.updated_at, - created_at: r.created_at, - } - }) .fetch_all(&self.pool) .await?; @@ -1243,7 +1228,7 @@ impl Database { is_yanked: r.package_version_is_yanked, readme_path: r.package_version_readme_path, uses_npm: r.package_version_uses_npm, - meta: r.package_version_meta.unwrap_or_default(), + meta: r.package_version_meta, updated_at: r.package_version_updated_at, created_at: r.package_version_created_at, rekor_log_id: r.package_version_rekor_log_id, @@ -1279,7 +1264,8 @@ impl Database { scope: &ScopeName, name: &PackageName, ) -> Result> { - sqlx::query!( + sqlx::query_as!( + PackageVersion, r#"SELECT scope as "scope: ScopeName", name as "name: PackageName", version as "version: Version", user_id, readme_path as "readme_path: PackagePath", exports as "exports: ExportsMap", is_yanked, uses_npm, meta as "meta: PackageVersionMeta", updated_at, created_at, rekor_log_id FROM package_versions WHERE scope = $1 AND name = $2 AND version NOT LIKE '%-%' AND is_yanked = false @@ -1288,23 +1274,7 @@ impl Database { scope as _, name as _, ) - .map(|r| { - PackageVersion { - scope: r.scope, - name: r.name, - version: r.version, - user_id: r.user_id, - exports: r.exports, - is_yanked: r.is_yanked, - readme_path: r.readme_path, - uses_npm: r.uses_npm, - meta: r.meta.unwrap_or_default(), - rekor_log_id: r.rekor_log_id, - updated_at: r.updated_at, - created_at: r.created_at, - } - }) - .fetch_optional(&self.pool) + .fetch_optional(&self.pool) .await } @@ -1315,7 +1285,8 @@ impl Database { name: &PackageName, version: &Version, ) -> Result> { - sqlx::query!( + sqlx::query_as!( + PackageVersion, r#"SELECT scope as "scope: ScopeName", name as "name: PackageName", version as "version: Version", user_id, readme_path as "readme_path: PackagePath", exports as "exports: ExportsMap", is_yanked, uses_npm, meta as "meta: PackageVersionMeta", updated_at, created_at, rekor_log_id FROM package_versions WHERE scope = $1 AND name = $2 AND version = $3"#, @@ -1323,24 +1294,7 @@ impl Database { name as _, version as _ ) - .map(|r| { - PackageVersion { - scope: r.scope, - name: r.name, - version: r.version, - user_id: r.user_id, - exports: r.exports, - is_yanked: r.is_yanked, - readme_path: r.readme_path, - uses_npm: r.uses_npm, - meta: r.meta.unwrap_or_default(), - rekor_log_id: r.rekor_log_id, - updated_at: r.updated_at, - created_at: r.created_at, - } - }) - - .fetch_optional(&self.pool) + .fetch_optional(&self.pool) .await } @@ -1436,7 +1390,8 @@ impl Database { &self, new_package_version: NewPackageVersion<'_>, ) -> Result { - sqlx::query!( + sqlx::query_as!( + PackageVersion, r#"INSERT INTO package_versions (scope, name, version, user_id, readme_path, exports, uses_npm, meta) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING scope as "scope: ScopeName", name as "name: PackageName", version as "version: Version", user_id, readme_path as "readme_path: PackagePath", exports as "exports: ExportsMap", is_yanked, uses_npm, meta as "meta: PackageVersionMeta", updated_at, created_at, rekor_log_id"#, @@ -1449,24 +1404,7 @@ impl Database { new_package_version.uses_npm as _, new_package_version.meta as _, ) - .map(|r| { - PackageVersion { - scope: r.scope, - name: r.name, - version: r.version, - user_id: r.user_id, - exports: r.exports, - is_yanked: r.is_yanked, - readme_path: r.readme_path, - uses_npm: r.uses_npm, - meta: r.meta.unwrap_or_default(), - rekor_log_id: r.rekor_log_id, - updated_at: r.updated_at, - created_at: r.created_at, - } - }) - - .fetch_one(&self.pool) + .fetch_one(&self.pool) .await } @@ -1478,7 +1416,8 @@ impl Database { version: &Version, yank: bool, ) -> Result { - sqlx::query!( + sqlx::query_as!( + PackageVersion, r#"UPDATE package_versions SET is_yanked = $4 WHERE scope = $1 AND name = $2 AND version = $3 @@ -1488,24 +1427,7 @@ impl Database { version as _, yank ) - .map(|r| { - PackageVersion { - scope: r.scope, - name: r.name, - version: r.version, - user_id: r.user_id, - exports: r.exports, - is_yanked: r.is_yanked, - readme_path: r.readme_path, - uses_npm: r.uses_npm, - meta: r.meta.unwrap_or_default(), - rekor_log_id: r.rekor_log_id, - updated_at: r.updated_at, - created_at: r.created_at, - } - }) - - .fetch_one(&self.pool) + .fetch_one(&self.pool) .await }