diff --git a/services/db-migrations/migrations/1732483677017-dbs.js b/services/db-migrations/migrations/1732483677017-dbs.js index 019ab0a..fb58e03 100644 --- a/services/db-migrations/migrations/1732483677017-dbs.js +++ b/services/db-migrations/migrations/1732483677017-dbs.js @@ -1,18 +1,9 @@ import { fetch } from "pouchdb-fetch"; const dbsSecurity = { - _users: { - admins: { roles: ["_admin"] }, - members: { roles: ["_admin"] }, - }, - _replicator: { - admins: { roles: ["_admin"] }, - members: { roles: ["_admin"] }, - }, - _global_changes: { - admins: { roles: ["_admin"] }, - members: { roles: ["_admin"] }, - }, + _users: null, + _replicator: null, + _global_changes: null, server_settings: { admins: { roles: ["_admin"] }, @@ -59,19 +50,29 @@ export const up = async () => { headers, }).then((response) => response.json()); - if (!responseCreateDb.ok) { + if (responseCreateDb.ok) { + console.log(`created ${dbName}`); + } else if (responseCreateDb.error === "file_exists") { + console.log(`exists ${dbName}`); + } else { throw new Error( - `${dbName} security error ${JSON.stringify(responseCreateDb)}`, + `${dbName} create error ${JSON.stringify(responseCreateDb)}`, ); } + if (!dbSecurity) { + continue; + } + const responseSetSecurity = await fetch(getUrl(`${dbName}/_security`), { method: "PUT", headers, body: JSON.stringify(dbSecurity), }).then((response) => response.json()); - if (!responseSetSecurity.ok) { + if (responseSetSecurity.ok) { + console.log(`security ${dbName}`); + } else { throw new Error( `${dbName} security error ${JSON.stringify(responseSetSecurity)}`, ); @@ -80,14 +81,19 @@ export const up = async () => { }; export const down = async () => { - for (const dbName of Object.keys(dbsSecurity)) { + const promises = Object.keys(dbsSecurity).map(async (dbName) => { const responseDeleteDb = await fetch(getUrl(dbName), { headers, method: "DELETE", - }); - console.assert( - (await responseDeleteDb.json()).ok, - `${dbName} create db error`, - ); - } + }).then((response) => response.json()); + if (responseDeleteDb.ok) { + console.log(`deleted ${dbName}`); + } else { + throw new Error( + `${dbName} delete error ${JSON.stringify(responseDeleteDb)}`, + ); + } + }); + await Promise.allSettled(promises); + await Promise.all(promises); };