Configure Multiple Environments

This guide shows you how to configure different database connections for development, test, and production environments.

Environment Detection

CRYSTAL_ENV = ENV["CRYSTAL_ENV"]? || "development"

Environment-Based Configuration

require "cql"

CRYSTAL_ENV = ENV["CRYSTAL_ENV"]? || "development"

DATABASE_URL = case CRYSTAL_ENV
when "production"
  ENV["DATABASE_URL"]? || raise "DATABASE_URL required in production"
when "test"
  ENV["TEST_DATABASE_URL"]? || "postgres://localhost/myapp_test"
else
  ENV["DATABASE_URL"]? || "postgres://localhost/myapp_development"
end

ADAPTER = case CRYSTAL_ENV
when "test"
  # Use SQLite for faster tests
  CQL::Adapter::SQLite
else
  CQL::Adapter::Postgres
end

MyDB = CQL::Schema.define(:my_db, adapter: ADAPTER, uri: DATABASE_URL) do
end

Separate Database Per Environment

Using Configuration Files

Set Environment

Environment-Specific Features

Verify Environment

Last updated

Was this helpful?