How luislavena/drift works
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).