简体   繁体   English

如何 db:从 docker 中的 mysql 转储中恢复数据库后迁移

[英]how to db:migrate after restoring database from mysql dump in docker

I have two docker containers for mysql and ruby and the ruby container is dependent on mysql container. I have two docker containers for mysql and ruby and the ruby container is dependent on mysql container.

In my docker-compose , I have volume-mounted an sql dump to the mysql container's /docker-entrypoint-initdb.d so that it gets executed upon starting the container for the first time and the database gets populated with some data.在我的docker-compose中,我已将 sql 转储卷安装到mysql容器的/docker-entrypoint-initdb.d中,以便在启动某些数据容器时执行它并首次填充数据容器。

The sql dump gets executed and the data gets restored to the db container as expected, but the problem is, while starting the ruby (with rails) container, it throws out a migration pending error . sql 转储被执行,数据按预期恢复到 db 容器,但问题是,在启动ruby (with rails) 容器时,它抛出了一个migration pending error I understand that this is happening because there is no any schema migration versions recorded in the schema_migrations table as the data was deliberately restored from the sql dump.我知道发生这种情况是因为schema_migrations表中没有记录任何模式迁移版本,因为数据是故意从 sql 转储中恢复的。

But, even running docker-compose run <container> rake db:migrate wouldn't help because it throws out a table already exists error as the tables have already been created.但是,即使运行docker-compose run <container> rake db:migrate也无济于事,因为它会抛出table already exists错误,因为表已经创建。

How do I sync the rails schema migration versions with the actual data in the database?如何将 Rails 架构迁移版本与数据库中的实际数据同步?

I've also tried by manually adding the latest schema version number to the schema_migrations table but still returns migration pending error .我还尝试通过手动将最新的架构版本号添加到schema_migrations表中,但仍然返回migration pending error And, I cannot run docker-compose run <container> rake db:create db:migrate before restoring from sql dump because the files inside the container's /docker-entrypoint-initdb.d will only be executed if the database isn't created yet.而且,在从 sql 转储恢复之前,我无法运行docker-compose run <container> rake db:create db:migrate因为容器的/docker-entrypoint-initdb.d中的文件只有在尚未创建数据库时才会执行。

I think you can dump all data inside schema_migrations along with your initial dump schema, so that it will contain the migration versions and it won't raise an exception upon running rails db:migrate .我认为您可以将schema_migrations中的所有数据连同初始转储模式一起转储,这样它将包含迁移版本,并且在运行rails db:migrate时不会引发异常。

Update: The older version of rails actually checks all schema versions to decide whether it needs migration or not.更新:旧版本的 rails 实际上会检查所有模式版本以决定是否需要迁移。 You can check here for more info.你可以在这里查看更多信息。

      def needs_migration?(connection = Base.connection)
        (migrations(migrations_paths).collect(&:version) - get_all_versions(connection)).size > 0
      end

Hope this will help.希望这会有所帮助。

You can write bundle exec rake db:migrate inside your docker_entrypoint file.您可以在 docker_entrypoint 文件中编写bundle exec rake db:migrate

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

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