Templates

Overview

Sling uses database templates to generate SQL statements for operations like creating tables, inserting data, querying metadata, and type mappings. These templates are built-in for each database connector (PostgreSQL, Snowflake, MySQL, SQL Server, etc.).

Starting in v1.5.3, you can customize these templates by creating user override files in ~/.sling/templates/ directory (or env var $SLING_HOME_DIR/templates). This allows you to:

  • Customize table creation with database-specific options (e.g., compression, partitioning)

  • Modify metadata queries for special schema configurations

  • Override type mappings for your data types

  • Add database-specific optimizations

File Location and Naming

User template override files must be placed in:

~/.sling/templates/{database_type}.yaml

Where {database_type} matches your database connection type exactly.

Examples

  • PostgreSQL: ~/.sling/templates/postgres.yaml

  • Snowflake: ~/.sling/templates/snowflake.yaml

  • MySQL: ~/.sling/templates/mysql.yaml

  • SQL Server: ~/.sling/templates/sqlserver.yaml

  • BigQuery: ~/.sling/templates/bigquery.yaml

  • Redshift: ~/.sling/templates/redshift.yaml

To find your database type, run:

Template Structure

Each template file contains up to 7 sections. You only need to include the sections you want to override:

1. core - SQL DDL/DML Templates

Defines SQL statements for basic operations:

2. metadata - Schema Information Queries

Queries to retrieve database metadata:

3. analysis - Analytical Queries

Used for data profiling and validation:

4. function - Database-Specific Functions

Custom functions available in templates:

5. general_type_map - Generic to Database Type Mapping

Maps generic data types to database-specific types:

6. native_type_map - Database Type to Generic Mapping

Maps database-specific types back to generic types:

7. variable - Database Variables

Database-specific configuration variables:

Examples

Example 1: Custom PostgreSQL Table Creation with Compression

Add compression and optimization to PostgreSQL table creation:

File: ~/.sling/templates/postgres.yaml

This overrides only the create_table template. All other PostgreSQL templates remain unchanged.

Example 2: Custom Snowflake Type Mappings

Override type mappings for Snowflake to use specific precision:

File: ~/.sling/templates/snowflake.yaml

Example 3: Modified Metadata Query for Custom Schemas

Customize the columns query for a specific schema configuration:

File: ~/.sling/templates/snowflake.yaml

Example 4: MySQL with Specific Collation

File: ~/.sling/templates/mysql.yaml

How It Works

Templates are loaded in a layered hierarchy, with each layer overwriting nested keys from the previous:

  1. Base Template: Sling first loads base.yaml, which contains common SQL templates shared across all databases

  2. Database-Specific Template: The database-specific template (e.g., postgres.yaml, snowflake.yaml) is loaded next, overwriting any nested keys from the base template with database-specific implementations

  3. User Override: If a user template exists at ~/.sling/templates/{database_type}.yaml, it is merged last, overwriting any nested keys from the previous layers

  4. Partial Overrides: Only the keys you define override the built-in templates - all other values remain unchanged

  5. Caching: The merged template is cached for performance

  6. Debug Logging: With --debug flag, you can see when overrides are loaded

Built-in Templates Reference

View the default built-in templates at https://github.com/slingdata-io/sling-cli/tree/main/core/dbio/templates. The base.yaml file contains the foundational templates, while database-specific files (e.g., postgres.yaml, snowflake.yaml, mysql.yaml) show how each database overrides the base. These serve as examples when creating your own custom overrides.

Last updated

Was this helpful?