-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(observer): connect to spark_stats database (#123)
Also, modify spark-stats to accept two connection strings: - DATABASE_URL pointing to spark_stats database - EVALUATE_DB_URL pointing to spark_evaluate database For now, spark-stats is reading data from the spark_evaluate database only. Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
- Loading branch information
Showing
19 changed files
with
202 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
SELECT now(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { dirname, join } from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
import pg from 'pg' | ||
import Postgrator from 'postgrator' | ||
|
||
const migrationsDirectory = join( | ||
dirname(fileURLToPath(import.meta.url)), | ||
'..', | ||
'migrations' | ||
) | ||
|
||
/** | ||
*@param {pg.ClientConfig} pgConfig | ||
*/ | ||
export const migrateWithPgConfig = async (pgConfig) => { | ||
const client = new pg.Client(pgConfig) | ||
await client.connect() | ||
try { | ||
await migrateWithPgClient(client) | ||
} finally { | ||
await client.end() | ||
} | ||
} | ||
|
||
/** | ||
* @param {pg.Client} client | ||
*/ | ||
export const migrateWithPgClient = async (client) => { | ||
const postgrator = new Postgrator({ | ||
migrationPattern: join(migrationsDirectory, '*'), | ||
driver: 'pg', | ||
execQuery: (query) => client.query(query) | ||
}) | ||
console.log( | ||
'Migrating DB schema from version %s to version %s', | ||
await postgrator.getDatabaseVersion(), | ||
await postgrator.getMaxVersion() | ||
) | ||
|
||
await postgrator.migrate() | ||
|
||
console.log('Migrated DB schema to version', await postgrator.getDatabaseVersion()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"name": "@filecoin-station/spark-stats-db-migrations", | ||
"version": "1.0.0", | ||
"type": "module", | ||
"main": "index.js", | ||
"private": true, | ||
"scripts": { | ||
"lint": "standard", | ||
"test": "mocha" | ||
}, | ||
"devDependencies": { | ||
"standard": "^17.1.0" | ||
}, | ||
"dependencies": { | ||
"pg": "^8.11.5", | ||
"postgrator": "^7.2.0" | ||
}, | ||
"standard": { | ||
"env": [ | ||
"mocha" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { DATABASE_URL } from '../lib/config.js' | ||
import { migrateWithPgConfig as migrateStatsDB } from '@filecoin-station/spark-stats-db-migrations' | ||
|
||
console.log('Migrating spark_stats database') | ||
await migrateStatsDB({ connectionString: DATABASE_URL }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const { | ||
// DATABASE_URL points to `spark_stats` database managed by this monorepo | ||
DATABASE_URL = 'postgres://localhost:5432/spark_stats' | ||
} = process.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.