Database Configuration

Database configuration in Azu CLI manages database connections, CQL ORM settings, migration configurations, and database-specific options. This includes connection pooling, adapter settings, and environment-specific database configurations.

Overview

Database configuration supports multiple database adapters and environments:

  • PostgreSQL: Primary database adapter

  • MySQL: Alternative database adapter

  • SQLite: Development and testing database

  • Connection Pooling: Configurable connection management

  • Migrations: Database schema management

  • Seeds: Database seeding configuration

Database Configuration Structure

Base Database Configuration

# config/database.yml
database:
  # Connection settings
  url: <%= ENV["DATABASE_URL"] %>
  adapter: <%= ENV["DB_ADAPTER"] || "postgresql" %>
  host: <%= ENV["DB_HOST"] || "localhost" %>
  port: <%= ENV["DB_PORT"] || 5432 %>
  username: <%= ENV["DB_USERNAME"] || "postgres" %>
  password: <%= ENV["DB_PASSWORD"] || "" %>
  database: <%= ENV["DB_NAME"] || "myapp_development" %>

  # Connection pooling
  pool_size: <%= ENV["DB_POOL_SIZE"] || 10 %>
  pool_timeout: <%= ENV["DB_POOL_TIMEOUT"] || 5000 %>
  pool_checkout_timeout: <%= ENV["DB_POOL_CHECKOUT_TIMEOUT"] || 5000 %>

  # SSL configuration
  ssl_mode: <%= ENV["DB_SSL_MODE"] || "prefer" %>
  ssl_cert: <%= ENV["DB_SSL_CERT"] %>
  ssl_key: <%= ENV["DB_SSL_KEY"] %>
  ssl_ca: <%= ENV["DB_SSL_CA"] %>

  # Performance settings
  statement_timeout: <%= ENV["DB_STATEMENT_TIMEOUT"] || 30000 %>
  idle_in_transaction_timeout: <%= ENV["DB_IDLE_TIMEOUT"] || 30000 %>

  # Logging
  logging: <%= ENV["DB_LOGGING"] || false %>
  log_level: <%= ENV["DB_LOG_LEVEL"] || "info" %>

  # Migration settings
  migrations:
    directory: db/migrations/
    table: schema_migrations
    lock_timeout: 10000

  # Seed configuration
  seeds:
    directory: db/seeds/
    files:
      - main_seed.cr
      - test_data.cr

Database Adapters

PostgreSQL Configuration

MySQL Configuration

SQLite Configuration

Environment-Specific Configuration

Development Environment

Test Environment

Production Environment

CQL ORM Configuration

Model Configuration

Migration Configuration

Connection Pooling

Pool Configuration

Pool Management

Database Seeding

Seed Configuration

Seed File Example

Database Monitoring

Monitoring Configuration

Monitoring Implementation

Database Commands

Database Management Commands

Configuration Commands

Environment Variables

Database Environment Variables

Best Practices

Configuration Management

  1. Environment Variables: Use environment variables for sensitive data

  2. Connection Pooling: Configure appropriate pool sizes for your workload

  3. SSL: Always use SSL in production

  4. Monitoring: Enable query and connection monitoring

  5. Backups: Configure regular database backups

Performance

  1. Pool Sizing: Size connection pools based on application needs

  2. Query Optimization: Monitor and optimize slow queries

  3. Indexing: Ensure proper database indexing

  4. Connection Management: Properly manage database connections

  5. Caching: Use appropriate caching strategies

Security

  1. Credentials: Never commit database credentials to version control

  2. SSL: Use SSL connections in production

  3. Access Control: Restrict database access appropriately

  4. Audit Logging: Enable audit logging for sensitive operations

  5. Backup Security: Secure database backups

Last updated