首页 > 解决方案 > 在测试环境中运行迁移时出现 Postgresql 错误

问题描述

作为规范套件的一部分,我正在 Rails 测试环境中运行迁移。当我运行迁移时,它会引发以下错误,我完全不确定为什么会发生此错误,对于只有一个迁移,我正在从备份文件运行迁移,如下所示。

我正在使用 ruby​​ 版本 1.9.3 postgres 版本 9.1.24

class StructureSnapshot < ActiveRecord::Migration
 def up
  execute IO.read("db/sql/20150422144950_structure_snapshot.sql")
 end
end

以下是运行迁移时的错误...

--INSERT INTO schema_migrations (version) VALUES ('20150422144950');
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord- 4.1.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'
/scratch/aime/workspace/SRMA-API Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `block in execute'
/scratch/aime/workspace/SRMA-API- Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activesupport-4.1.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
/scratch/aime/workspace/SRMA-API- Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in `execute'
/scratch/aime/workspace/SRMA-API- Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:649:in `block in method_missing'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `block in say_with_time'
/scratch/aime/installs/ruby-1.9.3-p550/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:621:in `say_with_time'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:641:in `method_missing'
/scratch/aime/workspace/SRMA-API-Rspec/db/migrate/20150422144950_structure_snapshot.rb:3:in `up'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:598:in `exec_migration'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/scratch/aime/installs/ruby-1.9.3-p550/lib/ruby/1.9.1/benchmark.rb:280:in `measure'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:578:in `block in migrate'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:577:in `migrate'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:752:in `migrate'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:991:in `block in execute_migration_in_transaction'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:1037:in `block in ddl_transaction'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/transactions.rb:208:in `transaction'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:1037:in `ddl_transaction'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:952:in `block in migrate'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `each'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/also_migrate-0.3.6/lib/also_migrate/migrator.rb:24:in `migrate_with_also_migrate'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/scratch/aime/installs/ruby-1.9.3-p550/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/bin/rake:23:in `load'
/scratch/aime/workspace/SRMA-API-Rspec/vendor/bundle/ruby/1.9.1/bin/rake:23:in `<top (required)>'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/cli/exec.rb:74:in `load'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/cli/exec.rb:74:in `kernel_load'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/cli/exec.rb:28:in `run'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/cli.rb:424:in `exec'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/vendor/thor/lib/thor.rb:387:in `dispatch'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/cli.rb:27:in `dispatch'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/vendor/thor/lib/thor/base.rb:466:in `start'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/cli.rb:18:in `start'
/scratch/aime/installs/ruby-1.9.3-p550/bin/bundle:30:in `block in <main>'
/scratch/aime/installs/ruby-1.9.3-p550/bundle/gems/bundler-1.16.5/lib/bundler/friendly_errors.rb:124:in `with_friendly_errors'
/scratch/aime/installs/ruby-1.9.3-p550/bin/bundle:22:in `<main>'

标签: ruby-on-railsdatabasepostgresqlruby-on-rails-4postgresql-9.1

解决方案


您收到错误是因为 schema_migrations 表中不能有重复的条目。

迁移 20150422144950 之前已经运行过(可能很久以前!),并且已记录为已被插入到 schema_migrations 表中的时间戳运行。

通常,迁移代码将检查迁移是否已经运行 - 如果它已经运行,那么它将跳过该迁移。因为您只是为旧迁移运行 sql(尽管是在新迁移中),所以您绕过了这些检查。

显然我不知道你在做什么背后的原因,但如果你需要通过创建一个新的迁移来运行旧的迁移,那么你似乎在迁移方面有点好奇。

insert into schema_migrations您可以编辑旧的迁移 sql 文件,这样如果您确实需要重复运行它,它就不会这样做。


推荐阅读