ImportSchema
ImportSchema provides a mix task that generates models from existing database schema.
$ mix import_schema
This task has only been tested with Postgres and does not come with any guarantee of completeness or accuracy. ImportSchema is intended to help bootstrap the process of replatforming webapps into Elixir applications. It uses opinionated defaults that have worked for projects I've worked on. Pull requests to expand the functionality are welcome.
Options
-r,--repo- the repo in your app to pull the schema from. If no repo is provided, the mix task will attempt to detect the repo and prompt the user to confirm or enter the correct repo name.$ mix import_schema -repo MyApp.Repo-sp,--strip-prefix- a table prefix that will be stripped from module and file names.For example, if a table is named
webapp_users, the repo isMyApp.Repo, and the desired module name isMyApp.Userand the desired file name isuser.exinstead ofMyApp.WebappUserandwebapp_user.ex:$ mix import_schema --strip-prefix webapp_-f,--force- overwrite any existing models without prompting. The default without--forcewill prompt the user to see if they want to overwrite any models.$ mix import_schema --force-i,--ignore- Table names to skip.%wildcards work as the query uses aLIKEstatement.For example, if the
logstable or any table starting withtempshould not generate models:$ mix import_schema --ignore logs --ignore temp%
Features
The models are generated under the
lib/MY_APP/models/directoryColumn data types are inferred from
information_schemabelongs_toassociations are inferred frominformation_schema
Installation
The package can be installed by adding import_schema to your list of dependencies in mix.exs:
def deps do
[
{:import_schema, "~> 0.1.0"}
]
end