在测试环境上运行迁移时发生Postgresql错误

时间:2018-11-26 11:00:30

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

作为规范套件的一部分,我正在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>'

1 个答案:

答案 0 :(得分:0)

您收到错误消息,因为schema_migrations表中不能包含重复的条目。

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

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

很明显,我不知道您在做什么的原因,但是如果您需要通过创建新迁移来运行旧迁移,那么对于迁移而言,您似乎处于一种有点奇怪的情况。

您可以编辑旧的迁移sql文件,以便在确实需要重复运行它的情况下不执行insert into schema_migrations

相关问题