Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DPLT-1124 Create script to preemptively provision user DBs #155

Merged
merged 2 commits into from
Jul 28, 2023
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
2 changes: 1 addition & 1 deletion indexer-js-queue-handler/pg-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
54 changes: 54 additions & 0 deletions indexer-js-queue-handler/scripts/provision-user-dbs.js
Original file line number Diff line number Diff line change
@@ -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');