Skip to content

Commit

Permalink
fix: Separate user db automated & script provisioning (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley authored Jul 27, 2023
1 parent c1e2fb2 commit 466e787
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ exports[`HasuraClient untracks the specified tables 1`] = `
[
"mock-hasura-endpoint/v1/metadata",
{
"body": "{"type":"pg_untrack_tables","args":{"tables":[{"table":{"schema":"schema","name":"height"},"source":"default","cascade":true},{"table":{"schema":"schema","name":"width"},"source":"default","cascade":true}]}}",
"body": "{"type":"bulk","args":[{"type":"pg_untrack_table","args":{"table":{"schema":"schema","name":"height"},"source":"default","cascade":true}},{"type":"pg_untrack_table","args":{"table":{"schema":"schema","name":"width"},"source":"default","cascade":true}}]}",
"headers": {
"X-Hasura-Admin-Secret": "mock-hasura-admin-secret",
},
Expand Down
21 changes: 12 additions & 9 deletions indexer-js-queue-handler/hasura-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,19 @@ export default class HasuraClient {
}

async untrackTables(source, schema, tableNames, cascade = true) {
return this.executeMetadataRequest('pg_untrack_tables', {
tables: tableNames.map((name) => ({
table: {
schema,
name,
},
source,
cascade,
return this.executeBulkMetadataRequest(
tableNames.map((name) => ({
type: 'pg_untrack_table',
args: {
table: {
schema,
name,
},
source,
cascade,
}
}))
});
);
}

async getForeignKeys(schemaName, source) {
Expand Down
34 changes: 19 additions & 15 deletions indexer-js-queue-handler/scripts/migrate-schema-to-db.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
process.env.HASURA_ENDPOINT = 'http://localhost:8080'
process.env.HASURA_ADMIN_SECRET = 'myadminsecretkey'

process.env.PG_ADMIN_USER = 'postgres'
process.env.PG_ADMIN_PASSWORD = 'postgrespassword'
process.env.PG_ADMIN_DATABASE = 'postgres'
process.env.PG_HOST = 'localhost'
process.env.PG_PORT = 5432

process.env.CHAIN_ID = 'mainnet'
process.env.ENV = 'dev'
// export HASURA_ENDPOINT='https://queryapi-hasura-graphql-vcqilefdcq-ew.a.run.app'
// export HASURA_ADMIN_SECRET=''
// export PG_ADMIN_USER='hasura'
// export PG_ADMIN_PASSWORD=''
// export PG_ADMIN_DATABASE='postgres'
// export PG_HOST=''
// export PG_PORT=5432
// export CHAIN_ID='mainnet'
// export ENV='dev'

import { execSync } from 'child_process'
import { providers } from 'near-api-js'
Expand Down Expand Up @@ -59,8 +57,8 @@ const schemaName = sanitizedFunctionName;

const existingSchemaName = `${sanitizedAccountId}_${sanitizedFunctionName}`;

const password = provisioner.generatePassword()
if (!await provisioner.hasuraClient.doesSourceExist(databaseName)) {
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 to Hasura')
Expand All @@ -77,13 +75,19 @@ await provisioner.createSchema(databaseName, existingSchemaName);
await provisioner.runMigrations(databaseName, existingSchemaName, databaseSchema);

console.log('Dumping existing data');
execSync(`pg_dump --data-only --schema=${existingSchemaName} --file="${existingSchemaName}.sql"`);
execSync(
`pg_dump ${`postgres://${process.env.PG_ADMIN_USER}:${process.env.PG_ADMIN_PASSWORD}@${process.env.PG_HOST}:${process.env.PG_PORT}/${process.env.PG_ADMIN_DATABASE}`} --data-only --schema=${existingSchemaName} --file="${existingSchemaName}.sql"`
);

console.log(`Restoring data to schema ${existingSchemaName} in DB ${databaseName}`);
execSync(`psql --dbname=${databaseName} < "${existingSchemaName}.sql"`);
execSync(
`psql ${`postgres://${userName}:${password}@${process.env.PG_HOST}:${process.env.PG_PORT}/${databaseName}`} < "${existingSchemaName}.sql"`
);

console.log(`Renaming schema ${existingSchemaName} to ${schemaName}`);
execSync(`psql --dbname=${databaseName} --command="ALTER SCHEMA \"${existingSchemaName}\" RENAME TO \"${schemaName}\";"`)
execSync(
`psql ${`postgres://${userName}:${password}@${process.env.PG_HOST}:${process.env.PG_PORT}/${databaseName}`} --command="ALTER SCHEMA \"${existingSchemaName}\" RENAME TO \"${schemaName}\";"`
)

console.log('Tracking tables');
await provisioner.trackTables(schemaName, tableNames, databaseName);
Expand Down

0 comments on commit 466e787

Please sign in to comment.