Skip to content

Commit e0200a3

Browse files
committed
Merge remote-tracking branch 'origin/main' into request-parameters
2 parents 0a250e3 + 52533a8 commit e0200a3

File tree

5 files changed

+36
-8
lines changed

5 files changed

+36
-8
lines changed

.changeset/loud-islands-argue.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@powersync/service-core': patch
3+
'powersync-open-service': patch
4+
---
5+
6+
Add logging and hard exit to migration script

DEVELOP.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,22 @@ pnpm build
2020

2121
The PowerSync service requires Postgres and MongoDB server connections. These configuration details can be specified in a `powersync.yaml` (or JSON) configuration file.
2222

23-
See the [Self hosting demo](https://github.com/powersync-ja/self-host-demo) for examples of starting these services.
23+
See the [Self hosting demo](https://github.com/powersync-ja/self-host-demo) for demos of starting these services.
2424

2525
A quick method for running all required services with a handy backend and frontend is to run the following in a checked-out `self-host-demo` folder.
2626

2727
```bash
28-
docker compose up --scale powersync=0
28+
docker compose -f demos/nodejs/docker-compose.yaml up --scale powersync=0
2929
```
3030

3131
Note: The `mongo` hostname specified in the MongoDB replica set needs to be accessible by your host machine if using the Mongo service above.
3232

33+
One method to obtain access is to add the following to `/etc/hosts` (on Unix-like operating systems)
34+
35+
```
36+
127.0.0.1 mongo
37+
```
38+
3339
This will start all the services defined in the Self hosting demo except for the PowerSync service - which will be started from this repository.
3440

3541
## Local Configuration

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ The service can be started using the public Docker image. See the image [notes](
4545

4646
The PowerSync service code is located in the `service` folder. This project is used to build the `journeyapps/powersync-service` Docker image.
4747

48-
# Notes
48+
# Developing
4949

50-
This mono repo currently relies on `restricted` packages. Currently this repo can only be built in CI. These dependencies will be removed soon.
50+
See the [notes](./DEVELOP.md) for local development instructions.

packages/service-core/src/entry/commands/migrate-action.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Command } from 'commander';
33
import { extractRunnerOptions, wrapConfigCommand } from './config-command.js';
44
import { migrate } from '../../migrations/migrations.js';
55
import { Direction } from '../../migrations/definitions.js';
6+
import { logger } from '@powersync/lib-services-framework';
67

78
const COMMAND_NAME = 'migrate';
89

@@ -17,9 +18,16 @@ export function registerMigrationAction(program: Command) {
1718
.action(async (direction: Direction, options) => {
1819
const runnerConfig = extractRunnerOptions(options);
1920

20-
await migrate({
21-
direction,
22-
runner_config: runnerConfig
23-
});
21+
try {
22+
await migrate({
23+
direction,
24+
runner_config: runnerConfig
25+
});
26+
27+
process.exit(0);
28+
} catch (e) {
29+
logger.error(`Migration failure`, e);
30+
process.exit(1);
31+
}
2432
});
2533
}

packages/service-core/src/migrations/migrations.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import * as locks from '../locks/locks-index.js';
88
import { Direction } from './definitions.js';
99
import { createMongoMigrationStore } from './store/migration-store.js';
1010
import { execute, writeLogsToStore } from './executor.js';
11+
import { logger } from '@powersync/lib-services-framework';
1112

1213
const DEFAULT_MONGO_LOCK_COLLECTION = 'locks';
1314
const MONGO_LOCK_PROCESS = 'migrations';
@@ -63,6 +64,7 @@ export const migrate = async (options: MigrationOptions) => {
6364
const { storage } = config;
6465

6566
const client = db.mongo.createMongoClient(storage);
67+
logger.info('Connecting to MongoDB');
6668
await client.connect();
6769

6870
const clientDB = client.db(storage.database);
@@ -73,6 +75,7 @@ export const migrate = async (options: MigrationOptions) => {
7375
});
7476

7577
// Only one process should execute this at a time.
78+
logger.info('Acquiring lock');
7679
const lockId = await manager.acquire();
7780

7881
if (!lockId) {
@@ -92,13 +95,15 @@ export const migrate = async (options: MigrationOptions) => {
9295
process.addListener('beforeExit', releaseLock);
9396

9497
try {
98+
logger.info('Loading migrations');
9599
const migrations = await loadMigrations(MIGRATIONS_DIR, runner_config);
96100

97101
// Use the provided config to connect to Mongo
98102
const store = createMongoMigrationStore(clientDB);
99103

100104
const state = await store.load();
101105

106+
logger.info('Running migrations');
102107
const logStream = execute({
103108
direction: direction,
104109
migrations,
@@ -111,8 +116,11 @@ export const migrate = async (options: MigrationOptions) => {
111116
state
112117
});
113118
} finally {
119+
logger.info('Releasing lock');
114120
await releaseLock();
121+
logger.info('Closing database');
115122
await client.close(true);
116123
process.removeListener('beforeExit', releaseLock);
124+
logger.info('Done with migrations');
117125
}
118126
};

0 commit comments

Comments
 (0)