mg
Version, currently 0.1.05 versions
- 0.5.0latestJul 11, 2021
- 0.4.0not indexedJul 11, 2021
- 0.3.0not indexedJul 11, 2021
- 0.2.0not indexedJul 11, 2021
- 0.1.0not indexedJul 11, 2021
github.com/hkalexling/mg
A minimal database migration tool for Crystal
Nothing has been indexed for 0.1.0 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.
Installation
# Add this to your shard.yml
dependencies:
mg:
github: hkalexling/mg
version: ~> 0.1.0Then run:
shards installshard.yml
No shard.yml has been indexed for 0.1.0. You can read it on the repository.
Dependencies
Unknown: the shard.yml for this version has not been read yet.
README
This README is the one indexed from the repository at its latest ref, not from the tag for this version.
MG
A minimal database migration tool for Crystal.
Installation
- Add the dependency to your
shard.yml:
dependencies:
mg:
github: hkalexling/mg
- Run
shards install
Usage
First define some database versions by inheriting from MG::Base. Here are two examples:
# 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
end
def 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
# 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
end
def 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.
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 - creator and maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1.0- Tagged
- Jul 11, 2021
- Commit
8d378bf58da4- Indexed
- not yet
Dependents
Repository
github.com/hkalexling/mg
Metadata
- Created
- Aug 12, 2026
- Updated
- Sep 25, 2026
- Synced
- Sep 25, 2026
- Versions
- 5