Skip to main content

Working with Environment Variables

You can define environment variables to use as values in your SML object files. Environment variables enable you to do things like deploy models across different data warehouses and environments (for example, development, staging, and production).

note

Environment variables are currently only supported in connection and dataset files.

Environment variables are defined in an .env file, which is stored at the root level of your repository. Once an environment variable has been defined, you can use it to replace values in other SML object files.

The following sections describe how to work with .env files in AtScale.

Add an .env file

To add an .env file to your repository:

  1. In Design Center, open the Workspace panel.
  2. Click the new object button and select .env.

The .env file appears in the Workspace panel at the root level of your repository, and is automatically added to your .gitignore file.

note

You can only have one .env file in a repository at a time.

Define environment variables

Once you have created an .env file, you can start defining your environment variables. Variables are defined using the following syntax:

VARIABLE="value"

You can define values of any data type. When defining string values, you must include quotes ("). You can also comment out lines in your file with #.

For example, to define a variable for a database connection label:

LABEL="Postgres"

Within SML object files, you can reference your environment variables using the syntax ${variable-name}.

To continue with our example, you could add the following to your connection file. This would set the label property to Postgres.

label: ${LABEL}
note

You cannot use environment variables in object_type or unique_name SML properties.

You can also use environment variables to partially define properties. For example, if your database connection names all use the same base followed by numbers, you could define the base as an environment variable:

CONNECTION="db_con"

And then use the variable to partially define values in your connection files:

as_connection: ${CONNECTION}01

Examples

Connection example

The following is an example of an .env file with variables for a connection file:

# Database Connection Configuration
CONNECTION="con1"
DB_NAME="atscale"
DB_SCHEMA="as_adventure"

# Connection Metadata
LABEL="Postgres"

And below is a sample connection file using the variables defined above:

unique_name: Postgres
object_type: connection
as_connection: ${CONNECTION}
label: ${LABEL}
database: ${DB_NAME}
schema: ${DB_SCHEMA}

Dataset example

The following is an example of an .env file with variables for a dataset file:

# Dialect variables
DATABASE="connection1"
SCHEMA="schema1"

And below is a sample from a dataset file, showing the variables defined above in the dialects property:

dialects:
- dialect: Postgresql
sql: >-
SELECT * FROM "${DATABASE}"."${SCHEMA}"."dataset"