Skip to content
This repository has been archived by the owner on Jan 31, 2023. It is now read-only.

Transaction for removePackageByName #68

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions docs/complexity-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Change cost: 11.805555555555555%
* Core size: 100%

## /home/runner/work/atom-backend/atom-backend/jest.config.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/jest.config.js

* Physical LOC: 19
* Logical LOC: 11
Expand All @@ -19,7 +19,7 @@
* Maintainability index: 63.4637930063897
* Dependency count: 0

## /home/runner/work/atom-backend/atom-backend/src/cache.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/src/cache.js

* Physical LOC: 28
* Logical LOC: 3
Expand All @@ -29,7 +29,7 @@
* Maintainability index: 78.8444767459975
* Dependency count: 1

## /home/runner/work/atom-backend/atom-backend/src/config.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/src/config.js

* Physical LOC: 102
* Logical LOC: 40
Expand All @@ -49,7 +49,7 @@
* Halstead volume: 2432.752928864466
* Halstead effort: 77848.09372366291

## /home/runner/work/atom-backend/atom-backend/src/debug_utils.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/src/debug_utils.js

* Physical LOC: 30
* Logical LOC: 22
Expand All @@ -69,7 +69,7 @@
* Halstead volume: 475.6861996976024
* Halstead effort: 8970.082622869073

## /home/runner/work/atom-backend/atom-backend/src/error.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/src/error.js

* Physical LOC: 114
* Logical LOC: 33
Expand Down Expand Up @@ -159,7 +159,7 @@
* Halstead volume: 48.43204266092217
* Halstead effort: 110.70181179639353

## /home/runner/work/atom-backend/atom-backend/src/logger.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/src/logger.js

* Physical LOC: 105
* Logical LOC: 35
Expand Down Expand Up @@ -219,7 +219,7 @@
* Halstead volume: 19.651484454403228
* Halstead effort: 29.47722668160484

## /home/runner/work/atom-backend/atom-backend/scripts/deprecated/search.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/scripts/deprecated/search.js

* Physical LOC: 176
* Logical LOC: 73
Expand Down Expand Up @@ -289,7 +289,7 @@
* Halstead volume: 380.3296723500879
* Halstead effort: 12318.455498894515

## /home/runner/work/atom-backend/atom-backend/scripts/tools/genBadges.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/scripts/tools/genBadges.js

* Physical LOC: 90
* Logical LOC: 50
Expand Down Expand Up @@ -339,7 +339,7 @@
* Halstead volume: 91.37651812938249
* Halstead effort: 186.9065143555551

## /home/runner/work/atom-backend/atom-backend/src/dev-runner/github_mock.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/src/dev-runner/github_mock.js

* Physical LOC: 50
* Logical LOC: 4
Expand All @@ -349,7 +349,7 @@
* Maintainability index: 76.20094227321904
* Dependency count: 1

## /home/runner/work/atom-backend/atom-backend/src/tests/debug_utils.test.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/src/tests/debug_utils.test.js

* Physical LOC: 31
* Logical LOC: 2
Expand All @@ -359,7 +359,7 @@
* Maintainability index: 86.03073855173344
* Dependency count: 1

## /home/runner/work/atom-backend/atom-backend/src/tests/logger.test.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/src/tests/logger.test.js

* Physical LOC: 142
* Logical LOC: 7
Expand All @@ -369,7 +369,7 @@
* Maintainability index: 69.95236801837311
* Dependency count: 1

## /home/runner/work/atom-backend/atom-backend/src/tests/query.test.js
## /home/runner/work/atom-community-server-backend-JS/atom-community-server-backend-JS/src/tests/query.test.js

* Physical LOC: 120
* Logical LOC: 79
Expand Down
144 changes: 71 additions & 73 deletions src/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ async function insertNewPackage(pack) {
return { ok: true, content: pointer };
})
.catch((err) => {
return { ok: false, content: err, short: "Server Error" };
const msg =
typeof err === "string"
? err
: `A generic error occurred while inserting ${pack.name} package`;

return { ok: false, content: msg, short: "Server Error" };
});
}

Expand Down Expand Up @@ -533,87 +538,80 @@ async function updatePackageByName(name, data) {
}

async function removePackageByName(name) {
try {
sql_storage ??= setupSQL();
sql_storage ??= setupSQL();

const command_vers = await sql_storage`
DELETE FROM versions
WHERE package IN (
SELECT pointer FROM names
WHERE name = ${name}
)
RETURNING *;
`;
return await sql_storage
.begin(async () => {
// Remove versions of the package
const command_vers = await sql_storage`
DELETE FROM versions
WHERE package IN (
SELECT pointer FROM names
WHERE name = ${name}
)
RETURNING *;
`;

if (command_vers.count === 0) {
return {
ok: false,
content: `Failed to delete any Versions for: ${name}`,
short: "Server Error",
};
}
if (command_vers.count === 0) {
throw `Failed to delete any versions for: ${name}`;
}

const command_star = await sql_storage`
DELETE FROM stars
WHERE package IN (
SELECT pointer FROM names
WHERE name = ${name}
)
RETURNING *;
`;
// Remove stars assigned to the package
const command_star = await sql_storage`
DELETE FROM stars
WHERE package IN (
SELECT pointer FROM names
WHERE name = ${name}
)
RETURNING *;
`;

if (command_star.count === 0) {
return {
ok: false,
content: `Failed to delete stars for: ${name}`,
short: "Server Error",
};
}
if (command_star.count === 0) {
throw `Failed to delete stars for: ${name}`;
}

const command_name = await sql_storage`
DELETE FROM names
WHERE pointer IN (
SELECT pointer FROM names
WHERE name = ${name}
)
RETURNING *;
`;
// Remove names related to the package
const command_name = await sql_storage`
DELETE FROM names
WHERE pointer IN (
SELECT pointer FROM names
WHERE name = ${name}
)
RETURNING *;
`;

if (command_name.count === 0) {
return {
ok: false,
content: `Failed to delete the name for: ${name}`,
short: "Server Error",
};
}
if (command_name.count === 0) {
throw `Failed to delete names for: ${name}`;
}

// We will have to use the pointer returning from this last command, since we
// can no longer preform the same lookup as before.
const command_pack = await sql_storage`
DELETE FROM packages
WHERE pointer = ${command_name[0].pointer}
RETURNING *;
`;
// Remove the package itself.
// We will have to use the pointer returning from this last command, since we
// can no longer preform the same lookup as before.
const command_pack = await sql_storage`
DELETE FROM packages
WHERE pointer = ${command_name[0].pointer}
RETURNING *;
`;

if (command_pack.count === 0) {
// nothing was returning, the delete probably failed
return {
ok: false,
content: `Failed to Delete Package for: ${name}`,
short: "Server Errror",
};
}
if (command_pack.count === 0) {
// nothing was returning, the delete probably failed
throw `Failed to Delete Package for: ${name}`;
}

return command_pack[0].name === name
? { ok: true, content: `Successfully Deleted Package: ${name}` }
: {
ok: false,
content: `Deleted unkown Package ${command_pack[0].name} during Deletion of ${name}`,
short: "Server Error",
};
} catch (err) {
return { ok: false, content: err, short: "Server Error" };
}
if (command_pack[0].name !== name) {
throw `Attempted to delete ${command_pack[0].name} rather than ${name}`;
}

return { ok: true, content: `Successfully Deleted Package: ${name}` };
})
.catch((err) => {
const msg =
typeof err === "string"
? err
: `A generic error occurred while inserting ${pack.name} package`;

return { ok: false, content: msg, short: "Server Error" };
});
}

async function removePackageByID(id) {
Expand Down