Skip to content

Commit 770f943

Browse files
authored
fix: optional key configuration push for npm run migrate (#1471)
2 parents eebbb6b + 8ad564e commit 770f943

File tree

4 files changed

+62
-19
lines changed

4 files changed

+62
-19
lines changed

api/README.md

+12-3
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,19 @@ npm run watch
6666

6767
instead, which will monitor for changes with `nodemon`.
6868

69-
## Initialisation
69+
## Initialisation and migration
7070

7171
Once the services are up and running we need to initialise the CouchDB
72-
database. This is done with the migrate script:
72+
database.
73+
74+
If you want to include pushing the keys (only recommended for local development):
75+
76+
```bash
77+
npm run migrate --keys
78+
```
79+
80+
If you just want to migrate databases without applying any public key
81+
configuration, then exclude this flag
7382

7483
```bash
7584
npm run migrate
@@ -82,7 +91,7 @@ run in the container rather than from your local environment:
8291
docker compose exec conductor npm run migrate
8392
```
8493

85-
This ensures that the correct CouchDB URL is used to access the database. The same
94+
This ensures that the correct CouchDB URL is used to access the database. The same
8695
applies for the commands below.
8796

8897
For development, there is also a script that will populate the database with projects (notebooks

api/src/couchdb/index.ts

+29-13
Original file line numberDiff line numberDiff line change
@@ -449,22 +449,31 @@ export const initialiseDataDb = async ({
449449
};
450450

451451
/**
452-
* Critical method which initialises all databases, including remotely on the configured couch instance.
452+
* Critical method which initialises all databases, including remotely on the
453+
* configured couch instance.
453454
*
454-
* This systematically generates a set of initialisation content from the data model, then applies this initialisation using a helper method in the data model.
455+
* This systematically generates a set of initialisation content from the data
456+
* model, then applies this initialisation using a helper method in the data
457+
* model.
455458
*
456-
* Some local information is injected as part of the config generation step - e.g. conductor name/description.
459+
* Some local information is injected as part of the config generation step -
460+
* e.g. conductor name/description.
457461
*
458462
* Also initialises keys based on the configured key service.
459463
*
460464
* If force = true, documents will always be written, even if it already exists.
461465
*
466+
* If pushKeys = true, will update the public keys
467+
*
462468
* @param force Write on clash
463469
*/
464470
export const initialiseDbAndKeys = async ({
465471
force = false,
472+
pushKeys = true,
466473
}: {
467474
force?: boolean;
475+
// Should we push the key configuration?
476+
pushKeys?: boolean;
468477
}) => {
469478
// Are we in a testing environment?
470479
const isTesting = process.env.NODE_ENV === 'test';
@@ -622,15 +631,19 @@ export const initialiseDbAndKeys = async ({
622631
await initialiseDataDb({projectId, force});
623632
}
624633

625-
// Setup keys
626-
try {
627-
await initialiseJWTKey();
628-
} catch (error) {
629-
console.log(
630-
'something wrong PUTing jwt_keys into the db configuration...',
631-
error
632-
);
633-
throw error;
634+
if (pushKeys) {
635+
// Setup keys
636+
try {
637+
await initialiseJWTKey();
638+
} catch (error) {
639+
console.log(
640+
'something wrong PUTing jwt_keys into the db configuration...',
641+
error
642+
);
643+
throw error;
644+
}
645+
} else {
646+
console.log('Not pushing key configuration.');
634647
}
635648
};
636649

@@ -639,10 +652,13 @@ export const initialiseDbAndKeys = async ({
639652
*/
640653
export const initialiseAndMigrateDBs = async ({
641654
force = false,
655+
pushKeys = true,
642656
}: {
643657
force?: boolean;
658+
// Should we push the key configuration?
659+
pushKeys?: boolean;
644660
}) => {
645-
await initialiseDbAndKeys({force});
661+
await initialiseDbAndKeys({force, pushKeys});
646662

647663
let dbs: {dbType: DATABASE_TYPE; dbName: string; db: PouchDB.Database}[] = [
648664
{db: getAuthDB(), dbType: DatabaseType.AUTH, dbName: AUTH_DB_NAME},

api/src/scripts/migrate.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
/* eslint-disable n/no-process-exit */
22
import {initialiseAndMigrateDBs} from '../couchdb';
33

4+
/**
5+
* Main function to run database initialization and migration
6+
* Accepts optional --keys flag to control whether public keys should be pushed
7+
*/
48
const main = async () => {
59
try {
6-
await initialiseAndMigrateDBs({force: true});
10+
// Check if --keys flag is present in command line arguments
11+
const pushKeys = process.argv.includes('--keys');
12+
13+
// Log whether keys will be configured
14+
console.log(
15+
`Public keys will ${pushKeys ? '' : 'not '}be configured during migration`
16+
);
17+
18+
// Run database initialization and migration with force and pushKeys parameters
19+
await initialiseAndMigrateDBs({
20+
force: true,
21+
pushKeys: pushKeys,
22+
});
23+
724
console.log('Migration completed successfully');
825
process.exit(0);
926
} catch (error) {
@@ -12,4 +29,5 @@ const main = async () => {
1229
}
1330
};
1431

32+
// Execute the main function
1533
main();

localdev.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ else
103103
fi
104104

105105
echo "Initialising database using API container"
106-
echo ">docker compose exec api sh -c \"cd api && npm run migrate\""
107-
docker compose exec api sh -c "cd api && npm run migrate"
106+
echo ">docker compose exec api sh -c \"cd api && npm run migrate --keys\""
107+
docker compose exec api sh -c "cd api && npm run migrate --keys"
108108

109109

110110
echo "Service is setup, to load notebooks and templates follow the below steps"

0 commit comments

Comments
 (0)