Skip to content

Commit

Permalink
DPLT-1049 Revert separate user DB provisioning (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
morgsmccauley committed Jul 21, 2023
1 parent 980e496 commit 858eea3
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 712 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/deploy-lambdas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,3 @@ jobs:
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
HASURA_ENDPOINT: ${{ vars.HASURA_ENDPOINT }}
HASURA_ADMIN_SECRET: ${{ secrets.HASURA_ADMIN_SECRET }}
PG_ADMIN_USER: ${{ secrets.PG_ADMIN_USER }}
PG_ADMIN_PASSWORD: ${{ secrets.PG_ADMIN_PASSWORD }}
PG_ADMIN_DATABASE: ${{ secrets.PG_ADMIN_DATABASE }}
PG_HOST: ${{ secrets.PG_HOST }}
PG_PORT: ${{ secrets.PG_PORT }}
105 changes: 30 additions & 75 deletions indexer-js-queue-handler/__snapshots__/hasura-client.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`HasuraClient adds a datasource 1`] = `
{
"args": {
"configuration": {
"connection_info": {
"database_url": {
"connection_parameters": {
"database": "morgs_near",
"host": "localhost",
"password": "password",
"port": 5432,
"username": "morgs_near",
},
},
},
},
"customization": {
"root_fields": {
"namespace": "morgs_near",
},
"type_names": {
"prefix": "morgs_near_",
},
},
"name": "morgs_near",
},
"type": "pg_add_source",
}
`;

exports[`HasuraClient adds the specified permissions for the specified roles/table/schema 1`] = `
{
"args": [
Expand Down Expand Up @@ -182,66 +152,51 @@ exports[`HasuraClient adds the specified permissions for the specified roles/tab
}
`;

exports[`HasuraClient checks if a schema exists within source 1`] = `
[
[
"mock-hasura-endpoint/v2/query",
{
"body": "{"type":"run_sql","args":{"sql":"SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'schema'","read_only":true,"source":"source"}}",
"headers": {
"X-Hasura-Admin-Secret": "mock-hasura-admin-secret",
},
"method": "POST",
},
],
]
`;

exports[`HasuraClient checks if datasource exists 1`] = `
exports[`HasuraClient checks if a schema exists 1`] = `
{
"args": {},
"type": "export_metadata",
"version": 2,
"args": {
"read_only": true,
"source": "default",
"sql": "SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'name'",
},
"type": "run_sql",
}
`;

exports[`HasuraClient creates a schema 1`] = `
[
[
"mock-hasura-endpoint/v2/query",
{
"body": "{"type":"run_sql","args":{"sql":"CREATE schema schemaName","read_only":false,"source":"dbName"}}",
"headers": {
"X-Hasura-Admin-Secret": "mock-hasura-admin-secret",
},
"method": "POST",
},
],
]
{
"args": {
"read_only": false,
"source": "default",
"sql": "CREATE schema name",
},
"type": "run_sql",
}
`;

exports[`HasuraClient gets table names within a schema 1`] = `
{
"args": {
"source": "source",
"read_only": true,
"source": "default",
"sql": "SELECT table_name FROM information_schema.tables WHERE table_schema = 'schema'",
},
"type": "pg_get_source_tables",
"type": "run_sql",
}
`;

exports[`HasuraClient runs migrations for the specified schema 1`] = `
[
[
"mock-hasura-endpoint/v2/query",
{
"body": "{"type":"run_sql","args":{"sql":"\\n set schema 'schemaName';\\n CREATE TABLE blocks (height numeric)\\n ","read_only":false,"source":"dbName"}}",
"headers": {
"X-Hasura-Admin-Secret": "mock-hasura-admin-secret",
},
"method": "POST",
},
],
]
{
"args": {
"read_only": false,
"source": "default",
"sql": "
set schema 'schema';
CREATE TABLE blocks (height numeric)
",
},
"type": "run_sql",
}
`;

exports[`HasuraClient tracks foreign key relationships 1`] = `
Expand Down
22 changes: 0 additions & 22 deletions indexer-js-queue-handler/__snapshots__/provisioner.test.js.snap

This file was deleted.

89 changes: 22 additions & 67 deletions indexer-js-queue-handler/hasura-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class HasuraClient {
args: {
sql,
read_only: opts.readOnly,
source: opts.source || 'default',
source: 'default',
}
}),
});
Expand All @@ -36,7 +36,7 @@ export default class HasuraClient {
return JSON.parse(body)
};

async executeMetadataRequest (type, args, version) {
async executeMetadataRequest (type, args) {
const response = await this.deps.fetch(`${process.env.HASURA_ENDPOINT}/v1/metadata`, {
method: 'POST',
headers: {
Expand All @@ -45,7 +45,6 @@ export default class HasuraClient {
body: JSON.stringify({
type,
args,
...(version && { version })
}),
});

Expand All @@ -62,61 +61,46 @@ export default class HasuraClient {
return this.executeMetadataRequest('bulk', metadataRequests);
}

async exportMetadata() {
const { metadata } = await this.executeMetadataRequest('export_metadata', {}, 2);
return metadata;
}

async doesSourceExist(source) {
const metadata = await this.exportMetadata();
return metadata.sources.filter(({ name }) => name === source).length > 0;
}

async doesSchemaExist(source, schemaName) {
async isSchemaCreated (schemaName) {
const { result } = await this.executeSql(
`SELECT schema_name FROM information_schema.schemata WHERE schema_name = '${schemaName}'`,
{ source, readOnly: true }
{ readOnly: true }
);

return result.length > 1;
}
};

createSchema (source, schemaName) {
createSchema (schemaName) {
return this.executeSql(
`CREATE schema ${schemaName}`,
{ source, readOnly: false }
{ readOnly: false }
);
}

runMigrations(source, schemaName, migration) {
runMigrations(schemaName, migration) {
return this.executeSql(
`
set schema '${schemaName}';
${migration}
`,
{ source, readOnly: false }
{ readOnly: false }
);
}

async getTableNames(schemaName, source) {
const tablesInSource = await this.executeMetadataRequest(
'pg_get_source_tables',
{
source
}
async getTableNames(schemaName) {
const { result } = await this.executeSql(
`SELECT table_name FROM information_schema.tables WHERE table_schema = '${schemaName}'`,
{ readOnly: true }
);

return tablesInSource
.filter(({ name, schema }) => schema === schemaName)
.map(({ name }) => name);
const [_columnNames, ...tableNames] = result;
return tableNames.flat();
};

async trackTables(schemaName, tableNames, source) {
async trackTables(schemaName, tableNames) {
return this.executeBulkMetadataRequest(
tableNames.map((name) => ({
type: 'pg_track_table',
args: {
source,
table: {
name,
schema: schemaName,
Expand All @@ -126,7 +110,7 @@ export default class HasuraClient {
);
}

async getForeignKeys(schemaName, source) {
async getForeignKeys(schemaName) {
const { result } = await this.executeSql(
`
SELECT
Expand Down Expand Up @@ -174,16 +158,16 @@ export default class HasuraClient {
q.table_name,
q.constraint_name) AS info;
`,
{ readOnly: true, source }
{ readOnly: true }
);

const [_, [foreignKeysJsonString]] = result;

return JSON.parse(foreignKeysJsonString);
}

async trackForeignKeyRelationships(schemaName, source) {
const foreignKeys = await this.getForeignKeys(schemaName, source);
async trackForeignKeyRelationships(schemaName) {
const foreignKeys = await this.getForeignKeys(schemaName);

if (foreignKeys.length === 0) {
return;
Expand All @@ -195,7 +179,6 @@ export default class HasuraClient {
{
type: "pg_create_array_relationship",
args: {
source,
name: foreignKey.table_name,
table: {
name: foreignKey.ref_table,
Expand All @@ -215,7 +198,6 @@ export default class HasuraClient {
{
type: "pg_create_object_relationship",
args: {
source,
name: pluralize.singular(foreignKey.ref_table),
table: {
name: foreignKey.table_name,
Expand All @@ -231,14 +213,13 @@ export default class HasuraClient {
);
}

async addPermissionsToTables(schemaName, source, tableNames, roleName, permissions) {
async addPermissionsToTables(schemaName, tableNames, roleName, permissions) {
return this.executeBulkMetadataRequest(
tableNames
.map((tableName) => (
permissions.map((permission) => ({
type: `pg_create_${permission}_permission`,
args: {
source,
table: {
name: tableName,
schema: schemaName,
Expand All @@ -253,37 +234,11 @@ export default class HasuraClient {
? { allow_aggregations: true }
: { backend_only: true }),
},
source: 'default'
},
}))
))
.flat()
);
}

async addDatasource(userName, password, databaseName) {
return this.executeMetadataRequest("pg_add_source", {
name: databaseName,
configuration: {
connection_info: {
database_url: {
connection_parameters: {
password,
database: databaseName,
username: userName,
host: process.env.PG_HOST,
port: Number(process.env.PG_PORT),
}
},
},
},
customization: {
root_fields: {
namespace: userName,
},
type_names: {
prefix: `${userName}_`,
},
},
});
}
}
Loading

0 comments on commit 858eea3

Please sign in to comment.