Skip to content

Commit 6f1e742

Browse files
committed
Add @masthead-data/dataform-plugin as a dependency and refactor reservation setter
1 parent a16bd7e commit 6f1e742

3 files changed

Lines changed: 22 additions & 64 deletions

File tree

includes/reservations.js

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const default_reservation = null
1+
const reservations = require('@masthead-data/dataform-plugin')
2+
23
const RESERVATION_CONFIG = [
34
{
45
tag: 'high_slots',
@@ -24,61 +25,7 @@ const RESERVATION_CONFIG = [
2425
}
2526
]
2627

27-
const RESERVATION_SETS = RESERVATION_CONFIG.map(cfg => ({
28-
...cfg,
29-
set: new Set(cfg.actions)
30-
}))
31-
32-
33-
/**
34-
* Extracts action name from Dataform context object
35-
* @param {Object} ctx - Dataform context object
36-
* @returns {string|null} The extracted action name or null if not found
37-
*/
38-
function getActionName(ctx) {
39-
if (!ctx) return null
40-
41-
if (typeof ctx.self === 'function' && !ctx?.operation) {
42-
// Try primary method: ctx.self()
43-
const selfName = ctx.self()
44-
if (selfName) return selfName.replace(/`/g, '').trim()
45-
} else if (ctx?.operation?.proto?.target) {
46-
// Fallback: construct from proto target
47-
const t = ctx?.operation?.proto?.target
48-
return t ? [t.database, t.name].join('.') : null
49-
}
50-
51-
return null
52-
}
53-
54-
/**
55-
* Determines the appropriate reservation for a given action name
56-
* @param {string} actionName - The action name (without backticks)
57-
* @returns {string|null} The reservation identifier or null if no reservation assignment needed
58-
*/
59-
function getReservation(actionName) {
60-
if (!actionName || typeof actionName !== 'string') return default_reservation
61-
62-
for (const { reservation, set } of Object.values(RESERVATION_SETS)) {
63-
if (set.has(actionName)) {
64-
return reservation
65-
}
66-
}
67-
68-
return default_reservation
69-
}
70-
71-
72-
/**
73-
* Generates the reservation SQL statement for a given Dataform context
74-
* @param {Object} ctx - Dataform context object with self() method and/or proto.target
75-
* @returns {string} The SQL statement to set reservation or empty string
76-
*/
77-
function reservation_setter(ctx) {
78-
const actionName = getActionName(ctx)
79-
const reservation = getReservation(actionName)
80-
return reservation ? `SET @@reservation='${reservation}';` : ''
81-
}
28+
const reservation_setter = reservations.createReservationSetter(RESERVATION_CONFIG)
8229

8330
module.exports = {
8431
reservation_setter

package-lock.json

Lines changed: 17 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"superlint": "docker run --platform linux/amd64 -e DEFAULT_BRANCH=main -e VALIDATE_GIT_COMMITLINT=false -e VALIDATE_TERRAFORM_TERRASCAN=false -e VALIDATE_TERRAFORM_TFLINT=false -e FIX_JSON_PRETTIER=true -e IGNORE_GITIGNORED_FILES=true -e VALIDATE_ALL_CODEBASE=true -e VALIDATE_JSCPD=false -e RUN_LOCAL=true -v ./:/tmp/lint ghcr.io/super-linter/super-linter:slim-latest"
88
},
99
"dependencies": {
10-
"@dataform/core": "3.0.30"
10+
"@dataform/core": "3.0.30",
11+
"@masthead-data/dataform-plugin": "0.0.1"
1112
},
1213
"devDependencies": {
1314
"eslint": "^9.35.0",

0 commit comments

Comments
 (0)