Database Commands

Azu CLI provides a comprehensive set of database commands for managing your application's database. These commands work with CQL ORM and support PostgreSQL, MySQL, and SQLite.

Overview

All database commands follow the pattern:

azu db:<command> [options]

Available Commands

Command
Description

azu db:create

Create the database

azu db:migrate

Run pending migrations

azu db:rollback

Rollback migrations

azu db:reset

Drop, create, and migrate database

azu db:seed

Run seed data

azu db:status

Show migration status

azu db:setup

Setup database (create and migrate)

azu db:drop

Drop the database

Database Configuration

Environment Variables

Configuration File

azu db:create

Creates the database for the current environment.

Basic Usage

Options

Option
Description
Default

--env <environment>

Target environment

development

--database <name>

Database name

auto-detected

--force

Force creation (drop if exists)

false

Examples

Troubleshooting

azu db:reset

Resets the database by dropping, creating, migrating, and optionally seeding.

Basic Usage

Options

Option
Description
Default

--force

Skip confirmation prompt

false

Examples

azu db:migrate

Runs pending database migrations using CQL's built-in Migrator to update the database schema.

Key Features:

  • Uses CQL's Migrator class for type-safe migrations

  • Automatically updates src/db/schema.cr after running migrations

  • Tracks migrations in cql_schema_migrations table

  • Generates temporary Crystal script for execution

Basic Usage

Options

Option
Description
Default

--env <environment>

Target environment

development

--version <version>

Migrate to specific version

latest

--steps <number>

Number of migrations to run

all

--verbose

Show detailed output

false

Examples

How It Works

  1. CLI generates a temporary Crystal script in the project root

  2. Script loads src/db/schema.cr and all migration files from src/db/migrations/

  3. CQL Migrator runs pending migrations in order using the generated script

  4. Schema file is automatically synchronized with the database

  5. Migration status is tracked in the schema_migrations table

  6. Temporary script is cleaned up after execution

Migration File Example

azu db:rollback

Rolls back database migrations using CQL's Migrator. Automatically updates the schema file after rollback.

Basic Usage

Options

Option
Description
Default

--steps <number>

Number of migrations to rollback

1

--version <version>

Rollback to specific version

-

--verbose

Show detailed output

false

Examples

How It Works

  1. CLI generates a temporary Crystal script in the project root

  2. Script loads src/db/schema.cr and all migration files from src/db/migrations/

  3. CQL Migrator executes the down method for each migration in reverse order

  4. Removes migration records from schema_migrations table

  5. Schema file is automatically synchronized with the database

  6. Temporary script is cleaned up after execution

azu db:reset

Drops, creates, and migrates the database in one command.

Basic Usage

Options

Option
Description
Default

--seed

Run seed data after reset

false

--force

Skip confirmation prompt

false

Examples

azu db:setup

Sets up the database by creating it (if it doesn't exist) and running all migrations.

Basic Usage

Options

Option
Description
Default

--seed

Run seed data after setup

false

Examples

azu db:drop

Drops the database for the current environment.

Basic Usage

Options

Option
Description
Default

--force

Skip confirmation prompt

false

Examples

azu db:seed

Runs seed data to populate the database with initial data.

Basic Usage

Examples

Seed File Structure

azu generate migration

Creates a new CQL migration file with proper Schema DSL syntax.

Basic Usage

Examples

Supported Attribute Types

  • string, textString

  • int32, integerInt32

  • int64Int64

  • float, float64Float64

  • bool, booleanBool

  • datetime, timeTime

  • dateDate

  • jsonJSON::Any

  • uuidUUID

  • references, belongs_to → Foreign key (Int64)

azu db:status

Shows the current migration status.

Basic Usage

Examples

Database Adapters

PostgreSQL

MySQL

SQLite

Common Workflows

Development Workflow

Testing Workflow

Production Workflow

Troubleshooting

Connection Issues

Permission Issues

Migration Issues

Seed Issues

Best Practices

1. Environment Management

2. Migration Safety

3. Seed Data

4. Database URLs


The database commands provide a complete workflow for managing your Azu application's database, from creation to migration to seeding.

Next Steps:

Last updated