本地回滚数据库不会更新服务器Rails 7中的postgres

问题描述 投票:0回答:1

我在我的服务器上运行 Rails 7,即使当我添加新表时,它确实会迁移并且我可以在服务器数据库中看到它。但是当我在开发环境中通过回滚进行更改然后迁移时,它并没有更新服务器的数据库。

数据库.yml

# SQLite. Versions 3.8.0 and up are supported.
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem "sqlite3"
#
default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  <<: *default
  database: db/development.sqlite3

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  <<: *default
  database: db/test.sqlite3

production:
  <<: *default
  database: db/production.sqlite3

部署/生产.rb:

server 'ip_here', user: 'user_here', roles: %w{app db web}

我也尝试过:

cap production deploy:migrate

但是没有用。另外,当我查找服务器中是否存在新更新的迁移文件时,它们确实存在。

ruby-on-rails ruby postgresql vps ruby-on-rails-7
1个回答
0
投票

我在开发环境中回滚进行了更改,然后进行了迁移,并没有更新服务器的数据库。

您误解了迁移的部署方式。

简而言之,我的建议是:

  • 请勿编辑已部署的迁移。 (除非你有充分的理由,并且知道自己在做什么!)
  • 不要在生产环境中回滚数据库。 (除非你有充分的理由,并且你知道自己在做什么!)
  • 要更改生产数据库,请部署新迁移

您没有共享此问题所涉及的特定迁移文件(旧版本和新版本),但例如您可能想使用:

change_column
rename_column
rename_index
rename_table

或者您可以分两步进行更改,例如

remove_column
...
add_column


生产系统为什么不知道应该回滚然后重新迁移?

每个迁移文件都有一个特定的版本号,例如

20240502100000
。部署到生产并迁移后,生产数据库将此迁移存储为“已应用”。

当您编辑文件并重新部署它时,从生产系统的角度来看,它已经应用了此迁移。它不知道文件已被编辑,并且您从未运行显式命令来回滚并再次迁移。

这就是为什么创建新的迁移文件通常是最好的主意。

© www.soinside.com 2019 - 2024. All rights reserved.