Skip to content

Commit 6027ae6

Browse files
authored
feat: scripts for creating blue-green dbs and copying cache (#85)
## Description We add two more scripts on 'scripts/migrations' for creation of Blue and Green databases in Postgres and cache data copying between them ## Checklist before requesting a review - [ ] I have conducted a self-review of my code. - [ ] I have conducted a QA. - [ ] If it is a core feature, I have included comprehensive tests. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced new commands to automate database setup and cache synchronization for blue-green deployment, streamlining database management. - **Documentation** - Provided a detailed guide outlining procedures for managing PostgreSQL databases in a blue-green deployment setup. - **Chores** - Added new scripts for copying cache data and creating databases, enhancing database management capabilities. - Updated dependencies to support PostgreSQL operations. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 5a13088 commit 6027ae6

File tree

8 files changed

+490
-2
lines changed

8 files changed

+490
-2
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"clean": "turbo run clean",
1616
"db:cache:migrate": "pnpm run --filter @grants-stack-indexer/migrations db:cache:migrate",
1717
"db:cache:reset": "pnpm run --filter @grants-stack-indexer/migrations db:cache:reset",
18+
"db:copy-cache": "pnpm run --filter @grants-stack-indexer/migrations db:copy-cache",
19+
"db:create-databases": "pnpm run --filter @grants-stack-indexer/migrations db:create-databases",
1820
"db:migrate": "pnpm run --filter @grants-stack-indexer/migrations db:migrate",
1921
"db:reset": "pnpm run --filter @grants-stack-indexer/migrations db:reset",
2022
"dev": "turbo run dev",

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/hasura-config/src/services/hasura.api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export class HasuraMetadataApi<Tables extends readonly string[]> {
142142

143143
// Object relationship: in the child table
144144
const objectRelationshipPayload = {
145-
name: singularize(camelize(parentTable.name)), // name derived from parent table
145+
name: singularize(camelize(parentTable.name, true)), // name derived from parent table
146146
table: childTable,
147147
source: "default",
148148
using: {
@@ -160,7 +160,7 @@ export class HasuraMetadataApi<Tables extends readonly string[]> {
160160
);
161161

162162
const arrayRelationshipPayload = {
163-
name: camelize(childTable.name), // name derived from child table
163+
name: camelize(childTable.name, true), // name derived from child table
164164
table: parentTable,
165165
source: "default",
166166
using: {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Blue-Green Database Operations
2+
3+
This utility provides scripts for managing PostgreSQL databases in a blue-green deployment strategy. It handles the creation and maintenance of two fixed databases: **GitcoinDatalayerBlue** and **GitcoinDatalayerGreen**.
4+
5+
## Features
6+
7+
The scripts offer two main operations:
8+
9+
1. **Database Creation** - Create both blue and green databases if they don't exist
10+
2. **Cache Data Copy** - Copy external services cache data from one database to the other
11+
12+
## Database Creation
13+
14+
Use this operation to create the required blue and green databases if they don't already exist.
15+
16+
```bash
17+
# Create both databases if they don't exist
18+
pnpm db:create-databases
19+
```
20+
21+
### How Database Creation Works
22+
23+
1. The script reads the PostgreSQL connection details from the `DATABASE_URL` environment variable
24+
2. It checks if the databases `GitcoinDatalayerBlue` and `GitcoinDatalayerGreen` exist
25+
3. If either database doesn't exist, it creates it
26+
4. This operation can be run multiple times without error, as it only creates databases when needed
27+
28+
## Cache Data Copy
29+
30+
Use this operation to copy cache data between blue and green databases. This is essential for maintaining cache consistency during blue-green deployments.
31+
32+
```bash
33+
# Copy cache data from blue to green
34+
pnpm db:copy-cache --copyFrom=blue
35+
36+
# Copy cache data from green to blue
37+
pnpm db:copy-cache --copyFrom=green
38+
39+
# Using the shorthand parameter
40+
pnpm db:copy-cache -f blue
41+
```
42+
43+
### How Cache Data Copy Works
44+
45+
1. The script reads the PostgreSQL connection details from the `DATABASE_URL` environment variable
46+
2. It handles the two specific cache tables: `price_cache` and `metadata_cache`
47+
3. For each table:
48+
- It truncates the target table
49+
- Copies all data from the source to the target table
50+
- Processes data in batches to avoid memory issues with large tables
51+
52+
### Cache Tables
53+
54+
The script only copies the following tables, which contain cached data from external services:
55+
56+
- `price_cache`: Stores token price information
57+
- `metadata_cache`: Stores token metadata
58+
59+
All other tables are managed through the regular migration process and are not part of the blue-green deployment cache copying strategy.

scripts/migrations/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"clean": "rm -rf dist/",
1919
"db:cache:migrate": "tsx src/migrateDb.script.ts --migrationsFolder external_services_cache",
2020
"db:cache:reset": "tsx src/resetDb.script.ts --migrationsFolder external_services_cache",
21+
"db:copy-cache": "tsx src/copyCache.script.ts",
22+
"db:create-databases": "tsx src/createDatabases.script.ts",
2123
"db:migrate": "tsx src/migrateDb.script.ts",
2224
"db:reset": "tsx src/resetDb.script.ts",
2325
"format": "prettier --check \"{src,test}/**/*.{js,ts,json}\"",
@@ -32,10 +34,12 @@
3234
"@grants-stack-indexer/shared": "workspace:*",
3335
"dotenv": "16.4.5",
3436
"kysely": "0.27.4",
37+
"pg": "8.13.0",
3538
"yargs": "17.7.2",
3639
"zod": "3.23.8"
3740
},
3841
"devDependencies": {
42+
"@types/pg": "8.11.10",
3943
"@types/yargs": "17.0.33",
4044
"tsx": "4.19.2"
4145
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Shared constants for blue-green deployment database operations
3+
*/
4+
5+
// Constant database names
6+
export const BLUE_DB = "GitcoinDatalayerBlue";
7+
export const GREEN_DB = "GitcoinDatalayerGreen";
8+
9+
// These are the only two cache tables we need to handle
10+
export const CACHE_TABLES = ["price_cache", "metadata_cache"];
11+
12+
/**
13+
* Interface for database connection details
14+
*/
15+
export interface ConnectionDetails {
16+
host: string;
17+
port: string;
18+
user: string;
19+
password: string;
20+
}
21+
22+
/**
23+
* Extract connection details from DATABASE_URL
24+
*/
25+
export const extractConnectionDetails = (url: string): ConnectionDetails => {
26+
// Parse DATABASE_URL (format: postgres://user:password@host:port/dbname)
27+
// Allow for different protocol variants: postgre, postgres, postgresql
28+
const connectionRegex = /(?:postgre(?:s|sql)?):\/\/([^:]+):([^@]+)@([^:]+):([^\/]+)\/.*/;
29+
const match = url.match(connectionRegex);
30+
31+
if (!match || match.length < 5) {
32+
throw new Error(`Invalid DATABASE_URL format: ${url}`);
33+
}
34+
35+
return {
36+
user: match[1] as string,
37+
password: match[2] as string,
38+
host: match[3] as string,
39+
port: match[4] as string,
40+
};
41+
};

0 commit comments

Comments
 (0)