# luislavena/drift architecture

> Architecture diagram of the GitHub repository luislavena/drift, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/luislavena/drift
- Repository: https://github.com/luislavena/drift

## Overview

##### Main Components
- CLI Layer: Represented by the main executable (src/cli.cr) and its associated command modules (e.g., src/drift/commands/migrate.cr, new.cr, rollback.cr, reset.cr). This layer handles user input, command parsing, and orchestrates migration operations.
- Migration Handling Core: Comprising key classes such as:
- Drift::Migration – encapsulates an individual SQL migration file.
- Drift::Context – aggregates and manages a collection of migrations loaded from the filesystem (or embedded).
- Drift::Migrator – orchestrates the application or rollback of migrations while interfacing with the database.
- Embedding Migrations Support: The embed mechanism provided via the Drift.embed_as macro (as seen in the README) which bundles migration files into the final binary.
- File System & SQL Migration Files: There is an external “database/migrations” directory (default location) where migration SQL files reside. Highlight that these files are parsed for special comments (drift:migrate and drift:rollback) and executed accordingly.

##### Relationships and Interactions
- The CLI Layer delegates commands (migrate, new, rollback, status, reset) to the underlying migration handling core.
- The Drift::Migrator interacts with the database: it reads/writes to the “drift_migrations” table, determines which migrations are pending by consulting both the migrations on disk and records in the database.
- The Context component serves as a repository of migrations (either loaded dynamically from a folder or via the embedding process) and assists the migrator in mapping applied migrations to their corresponding SQL files.
- When embedding migrations via Drift.embed_as, a dedicated component (or a macro-generated helper) provides an in-memory collection of migrations eliminating file system dependency at runtime.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Clearly divide the CLI command processing from the core migration logic.
- Modularity: Each part (migration parsing, embedding, orchestrating database operations) is self-contained and communicates via well-defined interfaces.
- Reusability & Extensibility: The library is framework agnostic and designed to be integrated into other projects, so the architecture supports both CLI and library modes.
- Testability: With a dedicated “spec” directory that contains tests for context, migration, and migrator, the architecture emphasizes testing of the core functionalities.

##### Relevant Technologies and Libraries
- Crystal Language: The project is implemented in Crystal using its standard library for file IO and database connections.
- Crystal DB: The common API for database abstraction which the library depends on.
- SQLite3: Initially supported as the only database adapter.
- GitHub Actions and other CI/CD configurations in the .github/workflows directory, which are part of the development process (although peripheral to the main migration logic).
- YAML and configuration files (e.g., shard.yml, changie.yaml) that indicate how the library is packaged and versioned.

- From CLI → Migrator (indicating command invocation such as “migrate”, “rollback”).
- From Migrator → Context and Database (show the lookup of migrations and the application/rollback processes).
- From the Embedded Component/File System → Context (illustrating how migrations are loaded).
- Annotations indicating key protocols (e.g., plain SQL execution, file loading, and the use of the Crystal DB adapter).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Presentation Layer"
        CLI_Main["CLI Main"]
        CLI_Commands["CLI Command Modules"]
    end

    subgraph "Core Business Logic"
        Migrator["Drift::Migrator"]
        Context["Drift::Context"]
        Migration["Drift::Migration"]
        Embed["Embed Migrations"]
    end

    subgraph "Data/SQL Layer"
        SQLFiles["SQL Migration Files"]
        Database["Database (drift_migrations table)"]
    end

    CLI_Main -->|"parses"| CLI_Commands
    CLI_Commands -->|"invoke_commands"| Migrator
    Migrator -->|"loads_migrations"| Context
    Context -->|"aggregates"| Migration
    SQLFiles -->|"provides_SQL"| Context
    Embed -->|"provides_embedded_SQL"| Context
    Migrator -->|"applies/rollbacks_SQL"| Database

    click CLI_Main "https://github.com/luislavena/drift/blob/main/src/cli.cr"
    click CLI_Commands "https://github.com/luislavena/drift/tree/main/src/drift/commands"
    click Migrator "https://github.com/luislavena/drift/blob/main/src/drift/migrator.cr"
    click Context "https://github.com/luislavena/drift/blob/main/src/drift/context.cr"
    click Migration "https://github.com/luislavena/drift/blob/main/src/drift/migration.cr"
    click Embed "https://github.com/luislavena/drift/blob/main/src/drift/embed.cr"
    click SQLFiles "https://github.com/luislavena/drift/tree/main/spec/fixtures/sequence"

    classDef cli fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef core fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef external fill:#fff3cd,stroke:#856404,stroke-width:2px;

    class CLI_Main,CLI_Commands cli;
    class Migrator,Context,Migration,Embed core;
    class SQLFiles,Database external;
```
