|
| 1 | +import { runCommand } from '@oclif/test'; |
| 2 | +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { expect, onTestFinished, test } from 'vitest'; |
| 6 | + |
| 7 | +import { root } from '../helpers/root.js'; |
| 8 | + |
| 9 | +test('migrates from sync rules to sync streams', async () => { |
| 10 | + const testDirectory = mkdtempSync(join(tmpdir(), 'migrate-test-')); |
| 11 | + onTestFinished(() => rmSync(testDirectory, { recursive: true })); |
| 12 | + |
| 13 | + const inputFile = join(testDirectory, 'input.yaml'); |
| 14 | + const outputFile = join(testDirectory, 'output.yaml'); |
| 15 | + writeFileSync( |
| 16 | + inputFile, |
| 17 | + ` |
| 18 | +bucket_definitions: |
| 19 | + user_lists: |
| 20 | + parameters: SELECT request.user_id() as user_id |
| 21 | + data: |
| 22 | + - SELECT * FROM lists WHERE owner_id = bucket.user_id |
| 23 | +` |
| 24 | + ); |
| 25 | + |
| 26 | + const result = await runCommand(`migrate sync-rules --input-file ${inputFile} --output-file ${outputFile}`, { root }); |
| 27 | + expect(result.error).toBeUndefined(); |
| 28 | + |
| 29 | + const transformed = readFileSync(outputFile).toString('utf-8'); |
| 30 | + expect(transformed) |
| 31 | + .toStrictEqual(`# Adds YAML Schema support for VSCode users with the YAML extension installed. This enables features like validation and autocompletion based on the provided schema. |
| 32 | +# yaml-language-server: $schema=https://unpkg.com/@powersync/service-sync-rules@latest/schema/sync_rules.json |
| 33 | +config: |
| 34 | + edition: 3 |
| 35 | +streams: |
| 36 | + # This Sync Stream has been translated from bucket definitions. There may be more efficient ways to express these queries. |
| 37 | + # You can add additional queries to this list if you need them. |
| 38 | + # For details, see the documentation: https://docs.powersync.com/sync/streams/overview |
| 39 | + migrated_to_streams: |
| 40 | + auto_subscribe: true |
| 41 | + queries: |
| 42 | + - SELECT * FROM lists WHERE owner_id = auth.user_id() |
| 43 | +`); |
| 44 | +}); |
0 commit comments