Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/ftac/sql/splitBatches.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
6 changes: 3 additions & 3 deletions lib/ftac/sql/splitBatches.ts
Original file line number Diff line number Diff line change
@@ -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 }));
7 changes: 7 additions & 0 deletions lib/ftac/yaml/actions/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
]));
3 changes: 2 additions & 1 deletion lib/ftac/yaml/actions/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }),
};
Loading