{"id":17968699,"url":"https://github.com/cogini/ecto_extract_migrations","last_synced_at":"2025-03-25T10:32:32.955Z","repository":{"id":56553035,"uuid":"267485380","full_name":"cogini/ecto_extract_migrations","owner":"cogini","description":"Elixir library to generate Ecto migrations from a PostgreSQL schema SQL file. Uses NimbleParsec and macro-style code generation.","archived":false,"fork":false,"pushed_at":"2021-01-22T08:52:09.000Z","size":330,"stargazers_count":17,"open_issues_count":2,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-16T23:37:43.006Z","etag":null,"topics":["ecto","elixir-library","postgresql","sql"],"latest_commit_sha":null,"homepage":"","language":"Elixir","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cogini.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2020-05-28T03:36:31.000Z","updated_at":"2024-06-14T11:52:23.000Z","dependencies_parsed_at":"2022-08-15T20:50:27.408Z","dependency_job_id":null,"html_url":"https://github.com/cogini/ecto_extract_migrations","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cogini%2Fecto_extract_migrations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cogini%2Fecto_extract_migrations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cogini%2Fecto_extract_migrations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cogini%2Fecto_extract_migrations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cogini","download_url":"https://codeload.github.com/cogini/ecto_extract_migrations/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245444387,"owners_count":20616368,"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":["ecto","elixir-library","postgresql","sql"],"created_at":"2024-10-29T14:41:17.750Z","updated_at":"2025-03-25T10:32:32.460Z","avatar_url":"https://github.com/cogini.png","language":"Elixir","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ecto_extract_migrations\n\n[![Module Version](https://img.shields.io/hexpm/v/ecto_extract_migrations.svg)](https://hex.pm/packages/ecto_extract_migrations)\n[![Hex Docs](https://img.shields.io/badge/hex-docs-lightgreen.svg)](https://hexdocs.pm/ecto_extract_migrations/)\n[![Total Download](https://img.shields.io/hexpm/dt/ecto_extract_migrations.svg)](https://hex.pm/packages/ecto_extract_migrations)\n[![License](https://img.shields.io/hexpm/l/ecto_extract_migrations.svg)](https://hex.pm/packages/ecto_extract_migrations)\n[![Last Updated](https://img.shields.io/github/last-commit/cogini/ecto_extract_migrations.svg)](https://github.com/cogini/ecto_extract_migrations/commits/master)\n\nMix task to generate Ecto migrations from a Postgres schema SQL file.\n\nThis lets you take an existing project and move it into Elixir\nwith a proper development workflow.\n\n## Usage\n\n1. Generate a schema-only dump of the database to SQL:\n\n   ```shell\n   pg_dump --schema-only --no-owner postgres://dbuser:dbpassword@localhost/dbname \u003e dbname.schema.sql\n   ```\n\n2. Generate migrations from the SQL file:\n\n   ```shell\n   mix ecto.extract.migrations --sql-file dbname.schema.sql\n   ```\n\n   or, from outside the target project:\n\n   ```shell\n   mix ecto.extract.migrations --sql-file dbname.schema.sql --repo \"MyProject.Repo\" --migrations-path ../myproject/priv/repo/migrations\n   ```\n\n3. Create a test database, run migrations to create the schema, then\nexport it and verify that it matches the original database:\n\n   ```shell\n   createuser --encrypted --pwprompt dbuser\n   dropdb dbname_migrations\n   createdb -Odbuser -Eutf8 dbname_migrations\n\n   mix ecto.migrate --log-sql\n\n   pg_dump --schema-only --no-owner postgres://dbuser@localhost/dbname_migrations \u003e dbname_migrations.sql\n\n   cat dbname.schema.sql | grep -v -E '^--|^$' \u003e old.sql\n   cat dbname_migrations.sql | grep -v -E '^--|^$' \u003e new.sql\n   diff -wu old.sql new.sql\n   ```\n\n## Details\n\nThis was written to migrate a legacy database with hundreds of tables and\nobjects.\n\nThe parser uses [NimbleParsec](https://github.com/dashbitco/nimble_parsec), and\nis based on the SQL grammar, so it is precise (unlike regex) and reasonably\ncomplete. It doesn't support every esoteric option, just what we needed, but\nthat was quite a lot. Patches are welcome.\n\nSupports:\n\n* `ALTER SEQUENCE`\n* `ALTER TABLE`\n* `CREATE EXTENSION`\n* `CREATE FUNCTION`\n* `CREATE INDEX`\n* `CREATE SCHEMA`\n* `CREATE SEQUENCE`\n* `CREATE TABLE`\n* `CREATE TRIGGER`\n* `CREATE TYPE`\n* `CREATE VIEW`\n\n## Installation\n\nAdd `ecto_extract_migrations` to your list of dependencies in `mix.exs`:\n\n```elixir\ndef deps do\n  [\n    {:ecto_extract_migrations, \"~\u003e 0.1.0\"}\n  ]\nend\n```\n\n## Resources\n\nHere are some useful resources for NimbleParsec:\n\n* https://stefan.lapers.be/posts/elixir-writing-an-expression-parser-with-nimble-parsec/\n* https://github.com/slapers/ex_sel\n\n## Alternatives\n\n* [ecto_generator](https://github.com/pmarreck/ecto_generator) generates Ecto schemas by querying\n  the database [information schema](https://www.postgresql.org/docs/13/information-schema.html)\n* [ex_abnf](https://github.com/marcelog/ex_abnf) generates a parser based on an\n  [ABNF grammar](https://en.wikipedia.org/wiki/Augmented_Backus%E2%80%93Naur_form).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcogini%2Fecto_extract_migrations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcogini%2Fecto_extract_migrations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcogini%2Fecto_extract_migrations/lists"}