{"id":17228763,"url":"https://github.com/hkalexling/mg","last_synced_at":"2025-10-06T20:10:28.837Z","repository":{"id":40682386,"uuid":"330177963","full_name":"hkalexling/mg","owner":"hkalexling","description":"A minimal database migration tool for Crystal","archived":false,"fork":false,"pushed_at":"2021-07-11T01:28:33.000Z","size":79,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-23T01:06:10.215Z","etag":null,"topics":["crystal","database","migration-tool"],"latest_commit_sha":null,"homepage":"https://hkalexling.github.io/mg/","language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hkalexling.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-01-16T14:14:10.000Z","updated_at":"2023-08-21T04:14:17.000Z","dependencies_parsed_at":"2022-09-01T06:42:27.102Z","dependency_job_id":null,"html_url":"https://github.com/hkalexling/mg","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hkalexling%2Fmg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hkalexling%2Fmg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hkalexling%2Fmg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hkalexling%2Fmg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hkalexling","download_url":"https://codeload.github.com/hkalexling/mg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248809040,"owners_count":21164893,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["crystal","database","migration-tool"],"created_at":"2024-10-15T04:44:59.244Z","updated_at":"2025-10-06T20:10:23.819Z","avatar_url":"https://github.com/hkalexling.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MG\n\nA minimal database migration tool for Crystal.\n\n## Installation\n\n1. Add the dependency to your `shard.yml`:\n\n```yaml\ndependencies:\n mg:\n   github: hkalexling/mg\n```\n\n2. Run `shards install`\n\n## Usage\n\nFirst define some database versions by inheriting from `MG::Base`. Here are two examples:\n\n```crystal\n# migration/users.1.cr\nclass CreateUser \u003c MG::Base\n  def up : String\n    \u003c\u003c-SQL\n    CREATE TABLE users (\n     username TEXT NOT NULL,\n     password TEXT NOT NULL,\n     email TEXT NOT NULL\n    );\n    SQL\n  end\n\n  def down : String\n    \u003c\u003c-SQL\n    DROP TABLE users;\n    SQL\n  end\n\n  # Optional lifecycle method to be executed after the `up` query.\n  def after_up(conn : DB::Connection)\n    puts \"Table users created\"\n  end\n\n  # Optional lifecycle method to be executed after the `down` query.\n  def after_down(conn : DB::Connection)\n    puts \"Table users dropped\"\n  end\nend\n```\n\n```crystal\n# migration/users_index.2.cr\nclass UserIndex \u003c MG::Base\n  def up : String\n    \u003c\u003c-SQL\n    CREATE UNIQUE INDEX username_idx ON users (username);\n    CREATE UNIQUE INDEX email_idx ON users (email);\n    SQL\n  end\n\n  def down : String\n    \u003c\u003c-SQL\n    DROP INDEX username_idx;\n    DROP INDEX email_idx;\n    SQL\n  end\nend\n```\n\nNote that the migration files must be named as `[filename].[non-negative-version-number].cr`.\n\nNow require the relevant files and the migrations in your application code, and start the migration.\n\n```crystal\nrequire \"mg\"\nrequire \"sqlite3\"\nrequire \"./migration/*\"\n\nLog.setup \"mg\", :debug\nDB.open \"sqlite3://file.db\" do |db|\n  mg = MG::Migration.new db\n\n  # Migrates to the latest version (in our case, 2)\n  mg.migrate\n\n  # Migrates to a specific version\n  mg.migrate to: 1\n\n  # Migrates down to version 0\n  mg.migrate to: 0\n\n  # Returns the current version\n  puts mg.user_version # 0\nend\n```\n\n## Contributors\n\n- [Alex Ling](https://github.com/hkalexling) - creator and maintainer\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhkalexling%2Fmg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhkalexling%2Fmg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhkalexling%2Fmg/lists"}