{"id":13691425,"url":"https://github.com/amberframework/micrate","last_synced_at":"2025-04-10T04:58:43.445Z","repository":{"id":42215181,"uuid":"59595168","full_name":"amberframework/micrate","owner":"amberframework","description":"Database migration tool written in Crystal","archived":false,"fork":false,"pushed_at":"2024-02-05T14:39:44.000Z","size":130,"stargazers_count":157,"open_issues_count":4,"forks_count":30,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-03T01:08:46.763Z","etag":null,"topics":["cli-command","crystal","crystal-api","database-migrations","goose","micrate-cli"],"latest_commit_sha":null,"homepage":null,"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/amberframework.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2016-05-24T17:43:55.000Z","updated_at":"2024-12-26T06:15:35.000Z","dependencies_parsed_at":"2024-03-03T07:34:49.696Z","dependency_job_id":"6dca13c2-f68f-461c-8aab-7624c6dfbe6b","html_url":"https://github.com/amberframework/micrate","commit_stats":null,"previous_names":["juanedi/micrate"],"tags_count":26,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amberframework%2Fmicrate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amberframework%2Fmicrate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amberframework%2Fmicrate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amberframework%2Fmicrate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amberframework","download_url":"https://codeload.github.com/amberframework/micrate/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248161265,"owners_count":21057554,"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":["cli-command","crystal","crystal-api","database-migrations","goose","micrate-cli"],"created_at":"2024-08-02T17:00:44.868Z","updated_at":"2025-04-10T04:58:43.400Z","avatar_url":"https://github.com/amberframework.png","language":"Crystal","readme":"# micrate\n\nMicrate is a database migration tool written in Crystal.\n\nIt is inspired by [goose](https://bitbucket.org/liamstask/goose/). Some code was ported from there too, so check it out.\n\nMicrate currently supports migrations for Postgres, Mysql and SQLite3, but it should be easy to add support for any other database engine with an existing [crystal-db API](https://github.com/crystal-lang/crystal-db) driver.\n\n## Command line\n\nTo install the standalone binary tool check out the releases page, or use homebrew:\n\n```\n$ brew tap amberframework/micrate\n$ brew install micrate\n```\n\nExecute `micrate help` for usage instructions. Micrate will connect to the database specified by the `DATABASE_URL` environment variable.\n\nTo create a new migration use the `scaffold` subcommand. For example, `micrate scaffold add_users_table` will create a new SQL migration file with a name such as `db/migrations/20160524162446_add_users_table.sql` that looks like this:\n\n```sql\n-- +micrate Up\n-- SQL in section 'Up' is executed when this migration is applied\n\n\n-- +micrate Down\n-- SQL section 'Down' is executed when this migration is rolled back\n```\n\nComments that start with `+micrate` are interpreted by micrate when running your migrations. In this case, the `Up` and `Down` directives are used to indicate which SQL statements must be run when applying or reverting a migration. You can now go along and write your migration like this:\n\n```sql\n-- +micrate Up\nCREATE TABLE users(id INT PRIMARY KEY, email VARCHAR NOT NULL);\n\n-- +micrate Down\nDROP TABLE users;\n```\nNow run it using `micrate up`. This command will execute all pending migrations:\n\n```\n$ micrate up\nMigrating db, current version: 0, target: 20160524162947\nOK   20160524162446_add_users_table.sql\n\n$ micrate dbversion # at any time you can find out the current version of the database\n20160524162446\n```\n\nIf you ever need to roll back the last migration, you can do so by executing `micrate down`. There's also `micrate redo` which rolls back the last migration and applies it again. Last but not least: use `micrate status` to find out the state of each migration:\n\n```\n$ micrate status\nApplied At                  Migration\n=======================================\n2016-05-24 16:31:07 UTC  -- 20160524162446_add_users_table.sql\nPending                  -- 20160524163425_add_address_to_users.sql\n```\n\nIf using complex statements that might contain semicolons, you must give micrate a hint on how to split the script into separate statements. You can do this with `StatementBegin` and `StatementEnd` directives: (thanks [goose](https://bitbucket.org/liamstask/goose/) for this!)\n\n```\n-- +micrate Up\n-- +micrate StatementBegin\nCREATE OR REPLACE FUNCTION histories_partition_creation( DATE, DATE )\nreturns void AS $$\nDECLARE\n  create_query text;\nBEGIN\n  FOR create_query IN SELECT\n      'CREATE TABLE IF NOT EXISTS histories_'\n      || TO_CHAR( d, 'YYYY_MM' )\n      || ' ( CHECK( created_at \u003e= timestamp '''\n      || TO_CHAR( d, 'YYYY-MM-DD 00:00:00' )\n      || ''' AND created_at \u003c timestamp '''\n      || TO_CHAR( d + INTERVAL '1 month', 'YYYY-MM-DD 00:00:00' )\n      || ''' ) ) inherits ( histories );'\n    FROM generate_series( $1, $2, '1 month' ) AS d\n  LOOP\n    EXECUTE create_query;\n  END LOOP;  -- LOOP END\nEND;         -- FUNCTION END\n$$\nlanguage plpgsql;\n-- +micrate StatementEnd\n```\n\n## API\n\nTo use the Crystal API, add this to your application's `shard.yml`:\n\n```yaml\ndependencies:\n  micrate:\n    github: amberframework/micrate\n```\n\nThis allows you to programatically use micrate's features. You'll see the `Micrate` module has an equivalent for every CLI command. If you need to use micrate's CLI without installing the tool (which could be convenient in a CI environment), you can write a runner script as follows:\n\n```crystal\n#! /usr/bin/env crystal\n#\n# To build a standalone command line client, require the\n# driver you wish to use and use `Micrate::Cli`.\n#\n\nrequire \"micrate\"\nrequire \"pg\"\n\nMicrate::DB.connection_url = \"postgresql://...\"\nMicrate::Cli.run\n```\n\n## Contributing\n\n1. Fork it ( https://github.com/amberframework/micrate/fork )\n2. Create your feature branch (git checkout -b my-new-feature)\n3. Commit your changes (git commit -am 'Add some feature')\n4. Push to the branch (git push origin my-new-feature)\n5. Create a new Pull Request\n\n## Contributors\n\n- [juanedi](https://github.com/juanedi)  - creator, maintainer\n","funding_links":[],"categories":["Database Tools","Crystal"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famberframework%2Fmicrate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famberframework%2Fmicrate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famberframework%2Fmicrate/lists"}