# Promoting a dashboard is a string replacement

> Model names are global inside a BI instance, so two environments cannot both own the name. What promotion looks like when it is a rename, why the regex is anchored on known model names, and the naming rule the pipeline exists to enforce.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2024-04-06
- Tags: Analytics, Looker, LookML, GitHub Actions, TypeScript, CI/CD
- URL: https://deyanpeev.com/posts/promoting-a-dashboard-is-a-string-replacement

Business intelligence as code is a good idea that stops one step short. The
semantic layer is text files in a repository (models, views, dashboards), so
they diff, review and branch like code. The trouble starts when you try to have
a staging environment.

The tool's deployment model is that a project points at a branch, and the names
declared inside a project are global to the instance. One instance can't hold
two models called `revenue`. So if you want to see a change before customers do,
and you only have one instance, the staging copy cannot be called what the
production copy is called.

The convention that falls out of that is a suffix. In the staging project, every
model file is `<name>-staging.model.lkml` and declares the model
`<name>-staging`. In production, no suffix. Promotion is a rename, and it has to
be applied transitively, because dashboards name the model they read from:

```
dashboard: revenue_totals
  model: revenue-staging      ← has to become `revenue`
```

So the pipeline step that promotes a release is pretty much a string
replacement over a directory. It sounds like a shortcut until you write it and
find that all the decisions that matter are about *which* strings.

# Anchoring the regex on names that exist

The naive version matches anything ending in the suffix. The version that
shipped derives the set of legal model names from the source tree first, and
only then builds the pattern:

```ts
const modelFiles = await getRelativePaths(sourcePath, '**/*.model.lkml')

const modelNames = modelFiles
  .map((file) => file.split('/').pop()?.replace(fullSourceSuffix, ''))
  .filter((name) => name) as Array<string>

const replacementRegex = new RegExp(
  `model:\\s+(${modelNames.map(escapeRegex).join('|')})${sourceModelSuffix}`,
  'g',
)
```

Because of the alternation, a dashboard that references a model this project
doesn't define is left untouched. A generic `-staging$` pattern would have
rewritten it confidently into a name that does not exist, and produced a project
that validates as text and fails at query time. That's a bad place to find out.

It also means the promotion limits itself. Whatever the staging project contains
defines what can be promoted, anything else passes through as a literal, and
all of that costs one glob.

`escapeRegex` is there because model names are file names, and file names are
allowed to contain characters that mean something to a regex engine. It has
never fired in anger, but it should still be there.

# The naming rule is the product

The branch of this action I'd defend hardest is the one that does nothing:

```ts
if (srcFile.endsWith(fullSourceSuffix)) {
  const newDestFile = destFile.replace(fullSourceSuffix, `${destinationSuffix}.model.lkml`)
  await io.cp(srcFile, newDestFile)
} else {
  core.setFailed(
    `Model files do not comply with the naming convention. Please make sure ` +
      `all model files are ending with ${sourceModelSuffix}.`,
  )
  process.exit(1)
}
```

A model file in the staging project that is *not* suffixed is a production
model living in the staging project, and every staging change to it is already
live. Promotion would copy it onto itself and hide the fact.

That failure is silent everywhere else. The BI tool doesn't object, because the
file is valid, the model resolves and the dashboards work. The only place it
can be caught is a build step that knows what the convention is supposed to be.
So the build step is there mainly to enforce the convention, and the file
copying is almost incidental.

The `dry-run` flag went in shortly afterwards, for that reason. With it set,
the action resolves inputs, walks the tree, applies the naming check, logs what
it *would* copy, and writes nothing:

```ts
if (this.wfInputs.dryRun) {
  console.log(`[dry-run] Copying ${srcFile} to ${newDestFile}`)
} else {
  await io.cp(srcFile, newDestFile)
}
```

Now the same action runs on every pull request as validation and on merge as
promotion. Reviewers find out that a model was added without the suffix while
they're still reviewing, and not when somebody tries to release.

# Rebuild, do not merge

The destination is removed before anything is written:

```ts
await io.rmRF(this.wfInputs.destinationPath)
```

It's three lines in, and it is the reason the action is safe to run repeatedly.
A promotion that merges into whatever is already on disk will happily keep a
file that was deleted upstream, and a stale view file in a BI project is still
a definition somebody's dashboard may resolve against. Wiping first makes the
output a pure function of the input. You want that when the same step runs on a
pull request and on main and has to mean the same thing both times.

Views are copied verbatim. They don't carry model references, so there's
nothing in them to rewrite.

# The suffix does not stay in CI

There is a cost I'd warn anyone about before they adopt this pattern. A naming
convention introduced to work around a BI instance limitation leaks into the
application that embeds the dashboards.

The application addresses a dashboard by a qualified identifier, `model::page`.
That model segment is environment-specific now, so the front end has to know the
suffix and apply it:

```ts
export const normalizePagedId = (pageId: string, modelSuffix?: string): string => {
  const modelSeparator = '::'

  if (modelSuffix && pageId.includes(modelSeparator)) {
    const [model, page] = pageId.split(modelSeparator)
    const modelWithSuffix = model.endsWith(modelSuffix) ? model : `${model}${modelSuffix}`

    return `${modelWithSuffix}${modelSeparator}${page}`
  }

  return pageId
}
```

The suffix is served to the browser as part of the embed session payload, next
to the tokens, because the browser has no other way to know which enviroment
it's looking at. The `endsWith` guard is there because identifiers arrive both
already qualified and not, depending on whether they came from a link or from an
API listing, and appending twice produces a model name that doesn't exist.

So a CI naming convention became a runtime concern in a TypeScript front end,
and a piece of environment configuration crossed a network boundary to get
there. That is what sharing one BI instance across two environments costs, and
I'd count it against the licence saving.

# A parser, or a second instance

I'd parse the LookML. There are parsers. We matched on `model:` because that's
the only reference a dashboard file makes and a regex reads in ten seconds, but
it is still a text transformation over a structured language, and the day
someone writes a dashboard with a `model:` inside a string literal the regex is
wrong and nothing says so.

Or, better and more expensive, two instances, and delete all of this. The
suffix exists only because the namespace is shared, and every complication above
(including the one that ends up in the browser) comes from that.

The bundled action itself is unremarkable and that's fine. TypeScript, `ncc`
into a single `dist/index.js`, `node20` in the action manifest, a `testData`
directory with a handful of model, view and dashboard files that exercise the
naming gate. All of it is about a hundred and twenty lines.
