Skip to content

Commit 564e55d

Browse files
committed
Restructure TEST_TABLE for tests.
1 parent 2a7a572 commit 564e55d

File tree

21 files changed

+200
-100
lines changed

21 files changed

+200
-100
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './Logger.js';
22
export { Logger } from 'winston';
3+
export { createLogger, format, transports } from 'winston';

libs/lib-services/src/migrations/AbstractMigrationAgent.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { LockManager } from '../locks/LockManager.js';
2-
import { logger } from '../logger/Logger.js';
2+
import { logger as defaultLogger, Logger } from '../logger/logger-index.js';
33
import * as defs from './migration-definitions.js';
44

55
export type MigrationParams<Generics extends MigrationAgentGenerics = MigrationAgentGenerics> = {
66
count?: number;
77
direction: defs.Direction;
88
migrationContext?: Generics['MIGRATION_CONTEXT'];
9+
logger?: Logger;
910
};
1011

1112
type WriteLogsParams = {
@@ -20,10 +21,12 @@ export type MigrationAgentGenerics = {
2021
export type RunMigrationParams<Generics extends MigrationAgentGenerics = MigrationAgentGenerics> = MigrationParams & {
2122
migrations: defs.Migration<Generics['MIGRATION_CONTEXT']>[];
2223
maxLockWaitMs?: number;
24+
logger?: Logger;
2325
};
2426

2527
type ExecuteParams = RunMigrationParams & {
2628
state?: defs.MigrationState;
29+
logger: Logger;
2730
};
2831

2932
export const DEFAULT_MAX_LOCK_WAIT_MS = 3 * 60 * 1000; // 3 minutes
@@ -46,9 +49,11 @@ export abstract class AbstractMigrationAgent<Generics extends MigrationAgentGene
4649
async run(params: RunMigrationParams) {
4750
await this.init();
4851

52+
const logger = params.logger ?? defaultLogger;
53+
4954
const { direction, migrations, migrationContext } = params;
5055
// Only one process should execute this at a time.
51-
logger.info('Acquiring lock for migrations');
56+
logger.debug('Acquiring lock for migrations');
5257
const lockHandle = await this.locks.acquire({ max_wait_ms: params.maxLockWaitMs ?? DEFAULT_MAX_LOCK_WAIT_MS });
5358

5459
if (!lockHandle) {
@@ -75,22 +80,24 @@ export abstract class AbstractMigrationAgent<Generics extends MigrationAgentGene
7580
direction,
7681
migrations,
7782
state,
78-
migrationContext
83+
migrationContext,
84+
logger
7985
});
8086

8187
await this.writeLogsToStore({
8288
log_stream: logStream,
8389
state
8490
});
8591
} finally {
86-
logger.info('Releasing migration lock');
92+
logger.debug('Releasing migration lock');
8793
await releaseLock();
8894
process.removeListener('beforeExit', releaseLock);
89-
logger.info('Done with migrations');
95+
logger.debug('Done with migrations');
9096
}
9197
}
9298

9399
protected async *execute(params: ExecuteParams): AsyncGenerator<defs.ExecutedMigration> {
100+
const logger = params.logger;
94101
const internalMigrations = await this.loadInternalMigrations();
95102
let migrations = [...internalMigrations, ...params.migrations];
96103

modules/module-mongodb-storage/src/utils/test-utils.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { mongo } from '@powersync/lib-service-mongodb';
22
import { PowerSyncMongo } from '../storage/implementation/db.js';
3-
import { TestStorageOptions } from '@powersync/service-core';
3+
import { TestStorageConfig, TestStorageOptions } from '@powersync/service-core';
44
import { MongoReportStorage } from '../storage/MongoReportStorage.js';
55
import { MongoBucketStorage } from '../storage/MongoBucketStorage.js';
66
import { MongoSyncBucketStorageOptions } from '../storage/implementation/MongoSyncBucketStorage.js';
@@ -12,22 +12,25 @@ export type MongoTestStorageOptions = {
1212
};
1313

1414
export function mongoTestStorageFactoryGenerator(factoryOptions: MongoTestStorageOptions) {
15-
return async (options?: TestStorageOptions) => {
16-
const db = connectMongoForTests(factoryOptions.url, factoryOptions.isCI);
15+
return {
16+
factory: async (options?: TestStorageOptions) => {
17+
const db = connectMongoForTests(factoryOptions.url, factoryOptions.isCI);
1718

18-
// None of the tests insert data into this collection, so it was never created
19-
if (!(await db.db.listCollections({ name: db.bucket_parameters.collectionName }).hasNext())) {
20-
await db.db.createCollection('bucket_parameters');
21-
}
19+
// None of the tests insert data into this collection, so it was never created
20+
if (!(await db.db.listCollections({ name: db.bucket_parameters.collectionName }).hasNext())) {
21+
await db.db.createCollection('bucket_parameters');
22+
}
2223

23-
// Full migrations are not currently run for tests, so we manually create this
24-
await db.createCheckpointEventsCollection();
24+
// Full migrations are not currently run for tests, so we manually create this
25+
await db.createCheckpointEventsCollection();
2526

26-
if (!options?.doNotClear) {
27-
await db.clear();
28-
}
27+
if (!options?.doNotClear) {
28+
await db.clear();
29+
}
2930

30-
return new MongoBucketStorage(db, { slot_name_prefix: 'test_' }, factoryOptions.internalOptions);
31+
return new MongoBucketStorage(db, { slot_name_prefix: 'test_' }, factoryOptions.internalOptions);
32+
},
33+
tableIdStrings: false
3134
};
3235
}
3336

modules/module-mongodb-storage/test/src/__snapshots__/storage.test.ts.snap

Lines changed: 0 additions & 25 deletions
This file was deleted.

modules/module-mongodb-storage/test/src/storage_sync.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { storage } from '@powersync/service-core';
2-
import { register, TEST_TABLE, test_utils } from '@powersync/service-core-tests';
2+
import { register, test_utils } from '@powersync/service-core-tests';
33
import { describe, expect, test } from 'vitest';
44
import { INITIALIZED_MONGO_STORAGE_FACTORY } from './util.js';
55

66
describe('sync - mongodb', () => {
77
register.registerSyncTests(INITIALIZED_MONGO_STORAGE_FACTORY);
8+
const TEST_TABLE = test_utils.makeTestTable('test', ['id'], INITIALIZED_MONGO_STORAGE_FACTORY);
89

910
// The split of returned results can vary depending on storage drivers
1011
test('large batch (2)', async () => {
@@ -19,7 +20,7 @@ describe('sync - mongodb', () => {
1920
- SELECT id, description FROM "%"
2021
`
2122
);
22-
await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY();
23+
await using factory = await INITIALIZED_MONGO_STORAGE_FACTORY.factory();
2324
const bucketStorage = factory.getInstance(sync_rules);
2425

2526
const result = await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {

modules/module-postgres-storage/src/utils/test-utils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { PostgresMigrationAgent } from '../migrations/PostgresMigrationAgent.js'
33
import { normalizePostgresStorageConfig, PostgresStorageConfigDecoded } from '../types/types.js';
44
import { PostgresReportStorage } from '../storage/PostgresReportStorage.js';
55
import { PostgresBucketStorageFactory } from '../storage/PostgresBucketStorageFactory.js';
6+
import { logger as defaultLogger, createLogger, transports } from '@powersync/lib-services-framework';
67

78
export type PostgresTestStorageOptions = {
89
url: string;
@@ -31,19 +32,28 @@ export function postgresTestSetup(factoryOptions: PostgresTestStorageOptions) {
3132

3233
const mockServiceContext = { configuration: { storage: BASE_CONFIG } } as unknown as ServiceContext;
3334

35+
// Migration logs can get really verbose in tests, so only log warnings and up.
36+
const logger = createLogger({
37+
level: 'warn',
38+
format: defaultLogger.format,
39+
transports: [new transports.Console()]
40+
});
41+
3442
await migrationManager.migrate({
3543
direction: framework.migrations.Direction.Down,
3644
migrationContext: {
3745
service_context: mockServiceContext
38-
}
46+
},
47+
logger
3948
});
4049

4150
if (direction == framework.migrations.Direction.Up) {
4251
await migrationManager.migrate({
4352
direction: framework.migrations.Direction.Up,
4453
migrationContext: {
4554
service_context: mockServiceContext
46-
}
55+
},
56+
logger
4757
});
4858
}
4959
};
@@ -80,7 +90,8 @@ export function postgresTestSetup(factoryOptions: PostgresTestStorageOptions) {
8090
throw ex;
8191
}
8292
},
83-
migrate
93+
migrate,
94+
tableIdStrings: true
8495
};
8596
}
8697

modules/module-postgres-storage/test/src/__snapshots__/storage.test.ts.snap

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Postgres Sync Bucket Storage Compact > compacting (1) 1`] = `
4+
[
5+
{
6+
"checkpoint": {
7+
"buckets": [],
8+
"last_op_id": "0",
9+
"streams": [
10+
{
11+
"errors": [],
12+
"is_default": true,
13+
"name": "by_user",
14+
},
15+
],
16+
"write_checkpoint": undefined,
17+
},
18+
},
19+
{
20+
"checkpoint_complete": {
21+
"last_op_id": "0",
22+
},
23+
},
24+
]
25+
`;
26+
27+
exports[`Postgres Sync Bucket Storage Compact > compacting (1) 2`] = `
28+
[
29+
{
30+
"checkpoint_diff": {
31+
"last_op_id": "1",
32+
"removed_buckets": [],
33+
"updated_buckets": [
34+
{
35+
"bucket": "by_user["user1"]",
36+
"checksum": 0,
37+
"count": 0,
38+
"priority": 3,
39+
"subscriptions": [
40+
{
41+
"default": 0,
42+
},
43+
],
44+
},
45+
],
46+
"write_checkpoint": undefined,
47+
},
48+
},
49+
{
50+
"checkpoint_complete": {
51+
"last_op_id": "1",
52+
},
53+
},
54+
]
55+
`;
56+
57+
exports[`Postgres Sync Bucket Storage Compact > compacting (1) 3`] = `
58+
[
59+
{
60+
"checkpoint": {
61+
"buckets": [
62+
{
63+
"bucket": "mybucket[]",
64+
"checksum": -93886621,
65+
"count": 2,
66+
"priority": 3,
67+
"subscriptions": [
68+
{
69+
"default": 0,
70+
},
71+
],
72+
},
73+
],
74+
"last_op_id": "2",
75+
"streams": [
76+
{
77+
"errors": [],
78+
"is_default": true,
79+
"name": "mybucket",
80+
},
81+
],
82+
"write_checkpoint": undefined,
83+
},
84+
},
85+
]
86+
`;
87+
88+
exports[`Postgres Sync Bucket Storage Compact > compacting (2) 1`] = `
89+
[
90+
{
91+
"token_expires_in": 0,
92+
},
93+
]
94+
`;

modules/module-postgres-storage/test/src/migrations.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ describe('Migrations', () => {
2121
register.registerMigrationTests(MIGRATION_AGENT_FACTORY);
2222

2323
it('Should have tables declared', async () => {
24-
const { db } = await POSTGRES_STORAGE_FACTORY();
24+
const { db } = await POSTGRES_STORAGE_FACTORY.factory();
2525

2626
const tables = await db.sql`
2727
SELECT

modules/module-postgres-storage/test/src/storage.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { storage } from '@powersync/service-core';
2-
import { register, TEST_TABLE, test_utils } from '@powersync/service-core-tests';
2+
import { register, test_utils } from '@powersync/service-core-tests';
33
import { describe, expect, test } from 'vitest';
44
import { POSTGRES_STORAGE_FACTORY } from './util.js';
55

@@ -32,11 +32,11 @@ describe('Postgres Sync Bucket Storage - pg-specific', () => {
3232
- SELECT id, description FROM "%"
3333
`
3434
);
35-
await using factory = await POSTGRES_STORAGE_FACTORY();
35+
await using factory = await POSTGRES_STORAGE_FACTORY.factory();
3636
const bucketStorage = factory.getInstance(sync_rules);
3737

3838
const result = await bucketStorage.startBatch(test_utils.BATCH_OPTIONS, async (batch) => {
39-
const sourceTable = TEST_TABLE;
39+
const sourceTable = test_utils.makeTestTable('test', ['id'], POSTGRES_STORAGE_FACTORY);
4040

4141
const largeDescription = '0123456789'.repeat(2_000_00);
4242

0 commit comments

Comments
 (0)