From 6b582696ae9bd363ecddf10a6c6052f5d86c0744 Mon Sep 17 00:00:00 2001
From: Morgan Mccauley <morgan@mccauley.co.nz>
Date: Fri, 28 Jul 2023 12:16:54 +1200
Subject: [PATCH 1/2] fix: Return pg query response

---
 indexer-js-queue-handler/pg-client.js | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/indexer-js-queue-handler/pg-client.js b/indexer-js-queue-handler/pg-client.js
index 2d822e4d2..5ed2cdd82 100644
--- a/indexer-js-queue-handler/pg-client.js
+++ b/indexer-js-queue-handler/pg-client.js
@@ -22,7 +22,7 @@ export default class PgClient {
     async query(query, params = []) {
         const client = await this.pgPool.connect();
         try {
-            await client.query(query, params);
+            return await client.query(query, params);
         } finally {
             client.release();
         }

From 32877867c8d43da3ae9adac0a6aff674d996d79b Mon Sep 17 00:00:00 2001
From: Morgan Mccauley <morgan@mccauley.co.nz>
Date: Fri, 28 Jul 2023 12:17:17 +1200
Subject: [PATCH 2/2] feat: Create script to preemptively provision user DBs

---
 .../scripts/provision-user-dbs.js             | 54 +++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 indexer-js-queue-handler/scripts/provision-user-dbs.js

diff --git a/indexer-js-queue-handler/scripts/provision-user-dbs.js b/indexer-js-queue-handler/scripts/provision-user-dbs.js
new file mode 100644
index 000000000..a9921ad1a
--- /dev/null
+++ b/indexer-js-queue-handler/scripts/provision-user-dbs.js
@@ -0,0 +1,54 @@
+// export HASURA_ENDPOINT=''
+// export HASURA_ADMIN_SECRET=''
+// export PG_ADMIN_USER=''
+// export PG_ADMIN_PASSWORD=''
+// export PG_ADMIN_DATABASE=''
+// export PG_HOST=''
+// export PG_PORT=
+
+import { execSync } from 'child_process'
+import { providers } from 'near-api-js'
+
+import Provisioner from '../provisioner.js'
+import HasuraClient from '../hasura-client.js'
+
+const provisioner = new Provisioner();
+
+const { rows } = await provisioner.pgClient.query('SELECT nspname AS name FROM pg_namespace;')
+
+const schemaNames = rows.map((row) => row.name);
+
+const accountIdsSet = schemaNames.reduce((accountIdsSet, schemaName) => {
+    const parts = schemaName.split('_near_');
+    if (parts.length > 1) {
+        accountIdsSet.add(`${parts[0]}_near`);
+    }
+    return accountIdsSet;
+}, new Set());
+
+const accountIds = Array.from(accountIdsSet);
+
+console.log(`Creating datasources for accounts: ${accountIds.join(', ')}`)
+
+for (const accountId of accountIds) {
+    console.log('---');
+    const sanitizedAccountId = provisioner.replaceSpecialChars(accountId);
+
+    const databaseName = sanitizedAccountId;
+    const userName = sanitizedAccountId;
+
+    if (await provisioner.hasuraClient.doesSourceExist(databaseName)) {
+        console.log(`Datasource ${databaseName} already exists, skipping.`)
+        continue;
+    }
+
+    const password = provisioner.generatePassword()
+    console.log(`Creating user: ${userName} and database: ${databaseName} with password: ${password}`);
+    await provisioner.createUserDb(userName, password, databaseName);
+
+    console.log(`Adding datasource ${databaseName} to Hasura`)
+    await provisioner.addDatasource(userName, password, databaseName);
+}
+console.log('---');
+
+console.log('Done');