简体   繁体   English

Phoenix-管理多个数据库的迁移混合任务的问题

[英]Phoenix - issues managing migration mix tasks for multiple databases

I've set up my Phoenix app so that it connects to two databases. 我已经设置了Phoenix应用程序,以便它可以连接到两个数据库。 I'm having a little trouble managing my migrations and the various Ecto mix tasks - specifically, creating new migrations and rolling back migrations. 我在管理迁移和各种Ecto mix任务时遇到了一些麻烦-特别是创建新迁移和回滚迁移。

First off, here's the repo configuration from config/dev.exs 首先,这是config/dev.exs

config :phoenix_backend, PhoenixBackend.Repo,
    adapter: Ecto.Adapters.Postgres,
    username: "postgres",
    password: "postgres",
    database: "phoenix_backend_dev",
    hostname: "localhost",
    pool_size: 10

config :phoenix_backend, PhoenixBackend.SearchRepo,
   adapter: Ecto.Adapters.Postgres,
   username: "postgres",
   password: "postgres",
   database: "phoenix_backend_search_dev",
   hostname: "localhost",
   pool_size: 10

Initially, my app just had one repo, the default Repo , I added SearchRepo later on. 最初,我的应用只有一个Repo ,即默认Repo ,后来又添加了SearchRepo

When I first set up SearchRepo , I ran $ mix gen.model to create a new model in that database. 当我第一次设置SearchRepo ,我运行$ mix gen.model在该数据库中创建一个新模型。 The migration went into priv/repo/migrations , and then I adjusted the auto-generated module definition to point at my new database - I changed 迁移进入priv/repo/migrations ,然后调整了自动生成的模块定义以指向我的新数据库-我更改了

defmodule PhoenixBackend.Repo.Migrations.CreateUserRole do

to

defmodule PhoenixBackend.SearchRepo.Migrations.CreateSearch do

When I ran $ mix ecto.migrate , I got the error 当我运行$ mix ecto.migrate ,出现了错误

Could not find migrations directory "priv/search_repo/migrations" for repo PhoenixBackend.SearchRepo.

So I created that directory, moved the migration into it, and ran the migration again - perfect it worked! 因此,我创建了该目录,将迁移移入其中,然后再次运行迁移-完美地工作了!

So this brings me to the problem(s)... 所以这给我带来了问题...

I now have two migration directories in priv - priv/repo/migrations and priv/search_repo/migrations . 我现在在priv - priv/repo/migrationspriv/search_repo/migrations有两个迁移目录。

  1. If I run $ mix ecto.migrate new_migration , this will create a migration file for new_migration in both directories. 如果我运行$ mix ecto.migrate new_migration ,这将在两个目录中为new_migration创建一个迁移文件。

  2. If I run $ mix ecto.rollback , this only rolls back migrations from priv/repo/migrations . 如果我运行$ mix ecto.rollback ,则只会回滚priv/repo/migrations

  3. If I run $ mix ecto.migrate , this runs the migrations from both directories. 如果我运行$ mix ecto.migrate ,则这将运行两个目录的迁移。

So what I'd like to know is, how can I standardize this behavior? 所以我想知道的是,如何规范这种行为? Is the problem with the commands that I'm running, or is there something in the configuration of the app that I need to change to point at these different directories? 我正在运行的命令是否有问题,或者应用程序的配置中是否需要更改以指向这些不同的目录?

I would love to be able to create migrations and point them to the specific directories, and I'd love to be able to specify which databases migrate and rollback point to. 我希望能够创建迁移并将它们指向特定目录,并且希望能够指定迁移和回滚指向的数据库。 How can I customize this behavior? 如何自定义此行为?

The option you are looking for is -r CustomRepo 您正在寻找的选项是-r CustomRepo

$ mix ecto.gen.migration add_something -r MyApp.SearchRepo
$ mix ecto.migrate -r MyApp.SearchRepo

Its all in the mix help. 所有这些在混合帮助中。 To get that 为了得到

$ mix help ecto.gen.migration
$ mix help ecto.migrate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM