Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/hkalexling/mg
A minimal database migration tool for Crystal
https://github.com/hkalexling/mg
crystal database migration-tool
Last synced: 21 days ago
JSON representation
A minimal database migration tool for Crystal
- Host: GitHub
- URL: https://github.com/hkalexling/mg
- Owner: hkalexling
- License: mit
- Created: 2021-01-16T14:14:10.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-07-11T01:28:33.000Z (over 3 years ago)
- Last Synced: 2024-05-23T01:06:10.215Z (6 months ago)
- Topics: crystal, database, migration-tool
- Language: Crystal
- Homepage: https://hkalexling.github.io/mg/
- Size: 77.1 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MG
A minimal database migration tool for Crystal.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
mg:
github: hkalexling/mg
```2. Run `shards install`
## Usage
First define some database versions by inheriting from `MG::Base`. Here are two examples:
```crystal
# migration/users.1.cr
class CreateUser < MG::Base
def up : String
<<-SQL
CREATE TABLE users (
username TEXT NOT NULL,
password TEXT NOT NULL,
email TEXT NOT NULL
);
SQL
enddef down : String
<<-SQL
DROP TABLE users;
SQL
end# Optional lifecycle method to be executed after the `up` query.
def after_up(conn : DB::Connection)
puts "Table users created"
end# Optional lifecycle method to be executed after the `down` query.
def after_down(conn : DB::Connection)
puts "Table users dropped"
end
end
``````crystal
# migration/users_index.2.cr
class UserIndex < MG::Base
def up : String
<<-SQL
CREATE UNIQUE INDEX username_idx ON users (username);
CREATE UNIQUE INDEX email_idx ON users (email);
SQL
enddef down : String
<<-SQL
DROP INDEX username_idx;
DROP INDEX email_idx;
SQL
end
end
```Note that the migration files must be named as `[filename].[non-negative-version-number].cr`.
Now require the relevant files and the migrations in your application code, and start the migration.
```crystal
require "mg"
require "sqlite3"
require "./migration/*"Log.setup "mg", :debug
DB.open "sqlite3://file.db" do |db|
mg = MG::Migration.new db# Migrates to the latest version (in our case, 2)
mg.migrate# Migrates to a specific version
mg.migrate to: 1# Migrates down to version 0
mg.migrate to: 0# Returns the current version
puts mg.user_version # 0
end
```## Contributors
- [Alex Ling](https://github.com/hkalexling) - creator and maintainer