diff --git a/lib/ftac/sql/splitBatches.test.ts b/lib/ftac/sql/splitBatches.test.ts index 8bf2407..30df778 100644 --- a/lib/ftac/sql/splitBatches.test.ts +++ b/lib/ftac/sql/splitBatches.test.ts @@ -6,4 +6,5 @@ it('splits on GO lines and drops empties', () => { text: { value: 'A', type: 'Expression' }, type: 'NonQuery' }, { text: { value: 'B', type: 'Expression' }, type: 'NonQuery' }, ])); +it('types every batch as asked', () => expect(splitBatches('A\nGO\nB', 'Query').map((b) => b.type)).toEqual(['Query', 'Query'])); it('rejects a batch that would be evaluated', () => expect(() => splitBatches('@x')).toThrow()); diff --git a/lib/ftac/sql/splitBatches.ts b/lib/ftac/sql/splitBatches.ts index 873ea71..5832d33 100644 --- a/lib/ftac/sql/splitBatches.ts +++ b/lib/ftac/sql/splitBatches.ts @@ -1,10 +1,10 @@ import { literal } from '../expr/literal.ts'; import type { ScriptBatch } from '../types.ts'; -/** Split T-SQL on GO into Script activity batches. Each batch must be a literal. */ -export const splitBatches = (sql: string): ScriptBatch[] => +/** Split T-SQL on GO into Script activity batches of one type. Each batch must be a literal. */ +export const splitBatches = (sql: string, type: ScriptBatch['type'] = 'NonQuery'): ScriptBatch[] => sql .split(/^\s*GO\s*$/im) .map((b) => b.trim()) .filter(Boolean) - .map((b) => ({ text: literal(b), type: 'NonQuery' })); + .map((b) => ({ text: literal(b), type })); diff --git a/lib/ftac/yaml/actions/script.test.ts b/lib/ftac/yaml/actions/script.test.ts index bf52f34..8d64750 100644 --- a/lib/ftac/yaml/actions/script.test.ts +++ b/lib/ftac/yaml/actions/script.test.ts @@ -12,3 +12,10 @@ it('example compiles to a Script against the warehouse with GO-split batches', ( }); it('rejects a batch that would be evaluated as an expression', () => expect(() => a.compile({ step: { id: 's' }, ctx: { env: {}, datasets: {} }, needs: [], policy: {} as never, with: { warehouse: 'w', sql: "SELECT '@{x}'" } })).toThrow(/interpolation/)); +it('types every batch Query with query: true', () => + expect( + a.compile({ step: { id: 's' }, ctx: { env: {}, datasets: {} }, needs: [], policy: {} as never, with: { warehouse: 'w', sql: 'SELECT 1\nGO\nSELECT 2', query: true } }).typeProperties.scripts + ).toEqual([ + { text: { value: 'SELECT 1', type: 'Expression' }, type: 'Query' }, + { text: { value: 'SELECT 2', type: 'Expression' }, type: 'Query' }, + ])); diff --git a/lib/ftac/yaml/actions/script.ts b/lib/ftac/yaml/actions/script.ts index 643b4fd..655b293 100644 --- a/lib/ftac/yaml/actions/script.ts +++ b/lib/ftac/yaml/actions/script.ts @@ -11,6 +11,7 @@ export const scriptAction: Action = { warehouse: 'The token name of the warehouse; gld_datawarehouse emits {{ .gld_datawarehouse_id }}.', sql: 'The T-SQL, GO-separated.', timeout: 'Per-block execution timeout, HH:MM:SS. Default 02:00:00.', + query: 'true when the batches return rows (Fabric script type Query). Default false (NonQuery).', }, example: `- id: merge_gold uses: fabric/script @@ -21,5 +22,5 @@ export const scriptAction: Action = { GO MERGE History.Period ...`, compile: ({ step, ctx, needs, policy, with: w }) => - script(step.id, { store: warehouse(compileText(String(w.warehouse), ctx, `${step.id}.warehouse`)), scripts: splitBatches(String(w.sql)), timeout: w.timeout }, { needs, policy }), + script(step.id, { store: warehouse(compileText(String(w.warehouse), ctx, `${step.id}.warehouse`)), scripts: splitBatches(String(w.sql), w.query ? 'Query' : 'NonQuery'), timeout: w.timeout }, { needs, policy }), };