人们如何编写涉及Paperclip的Rails迁移?我觉得我可能会遗漏一些明显的东西,因为我现在已经编写了自己的迁移助手黑客,这使得它变得更容易并且还可以处理必要的文件系统更改.当然,您应该在部署到生产之前在开发(和临时)环境中测试运行这些类型的迁移.
Paperclip迁移重命名,添加和删除帮助器
Paperclip更改路径迁移帮助程序(不是真正的数据库迁移,但认为它非常适合)
有没有更好的解决方案或最佳实践?有些人似乎创造了耙子任务等,感觉非常麻烦.
在MySQL(和其他SQL数据库)中,将注释添加到目的可能不明确的表或列中会很有帮助.(例如,搜索MySQL的"注释" 的创建表语法.)
有没有办法在ActiveRecord Migration中执行此操作?我试过没有结果.
create_table :stuff do |t|
t.integer :obscure_column, :comment => "Explanatory comment"
end
Run Code Online (Sandbox Code Playgroud)
我正在使用Rails 3.1.
我有一个模型令牌,它有一个字段token_number,我需要自动递增(从1001开始),当且仅当用户不提供它.问题是,由于用户可以选择提供此字段,因此无法完全查询数据库并请求最大的token_number.我在这个论坛上找到了一个答案,但我确信必须有一个更好的方法来执行它而不是执行SQL语句?在Ruby on Rails中自动增加非主键字段
我为Rails 3应用程序编写了一些迁移,但我想更改迁移的顺序.如何更改迁移顺序或顺序?是否像使用似乎是时间戳的重命名迁移文件一样简单?
我知道这是一个奇怪的问题,但基本上,我弄乱了我的迁移并删除了一些旧的迁移,现在我需要在创建一个新表之前删除一个表.我也知道我可以在create-the-new-table迁移中包含drop语句,但我很想知道如何重新排序迁移.
我已经设置了一个新的数据库,用于在PostgreSQL中安装Discourse.当我运行rake db:migrate时,它会创建大多数表,但它会失败:
-- execute("INSERT INTO archetypes (name_key, created_at, updated_at) VALUES ('poll', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)")
-> 0.0009s
-- add_column(:forum_threads, :archetype_id, :integer, {:default=>1, :null=>false})
-> 0.0209s
== CreateArchetypes: migrated (0.0424s) ======================================
== AddMetaDataToForumThreads: migrating ======================================
-- execute("CREATE EXTENSION IF NOT EXISTS hstore")
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: could not open extension control file "/usr/share/postgresql/9.1/extension/hstore.control": No such file or directory
: CREATE EXTENSION IF NOT EXISTS hstore
/usr/local/rvm/gems/ruby-1.9.3-p385/bundler/gems/MiniProfiler-d149f34fcdb6/Ruby/lib/patches/sql_patches.rb:155:in `exec'
/usr/local/rvm/gems/ruby-1.9.3-p385/bundler/gems/MiniProfiler-d149f34fcdb6/Ruby/lib/patches/sql_patches.rb:155:in `async_exec'
/usr/local/rvm/gems/ruby-1.9.3-p385/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:652:in `block in execute' …Run Code Online (Sandbox Code Playgroud) 我将迁移推送到生产数据库时遇到问题.
问题:
我已将其迁移到生产数据库:
MacBook-Air-Mac:app msc $ rake db:migrate RAILS_ENV ="production"[RailsAdmin]默认情况下禁用RailsAdmin初始化.如果需要,请传递SKIP_RAILS_ADMIN_INITIALIZER = false.== AddLengthColumnToBooks:迁移========================================= - add_column( :books,:length,:integer) - > 0.0017s == AddLengthColumnToBooks:migrated(0.0019s)============================ ====
考虑到新的数据库模式现在正在生产中,我已经部署了执行某些操作的代码:length.
在生产中,我收到以下错误:
未定义的方法`length ='for#
我做了heroku rollback并将应用程序降级到最新的可靠版本.
然后(可能有点太晚了)我发现我必须heroku restart在应用程序中加载新索引.我这样做了好几次.
我打开控制台然后检查Book.column_names,但没有length
我heroku run rake db:migrate接着又做heroku restart了一次,没有变化.
我已经尝试将另一列迁移到生产数据库,但根本没有得到任何消息,甚至没有得到第2页的消息.
更新
根据Philipe的答案,我做了一些额外的步骤:
git add db/schema.rb,git add db/migrate/20130325103953_add_length_column_to_books.rb
和'git add db/migrate/20130401041910_add_duration_column_to_books.rb'.Git的回答是:要提交的更改:(使用"git reset HEAD ..."取消暂停)
新文件:db/migrate/20130325103953_add_length_column_to_books.rb新文件:db/migrate/20130401041910_add_duration_column_to_books.rb modified:db/schema.rb
然后我做了git commit -m "Updating the schema". …
目前我正在使用庞大的rails应用程序和多个分支,每个分支都有一个新功能.它发生了很多功能需要迁移,在将它与master合并之前应该不会出现问题:schema.rb已经使用dev数据库的信息进行了更新!
澄清:
1. Branch A has migration create_table_x
2. Branch B has migration create_table_y
3. Branch A adds another create_table_z and runs db:migrate
4. You want to merge Branch A with Master and you see table_x, table_y and table_z in the schema.rb of Branch A.
Run Code Online (Sandbox Code Playgroud)
在分支中的每次迁移之前重置+种子数据库或为每个分支创建数据库都不是一个选项.由于2 GB SQL数据的巨大规模,它将无法使用.
我的问题:
是否真的需要将schema.rb保留在存储库中,因为每次迁移都会重建它?
如果是这样,是否可以从迁移而不是数据库转储构建架构?
如果架构稳定,是否允许在Rails应用程序中删除(或存档)旧的迁移文件?
我的迁移很多,我怀疑某处可能存在一些问题,因为我偶尔会在Heroku上迁移数据库时遇到问题.
我有以下迁移
class LinkDoctorsAndSpecializations < ActiveRecord::Migration
def up
add_reference :doctors, :doctor_specialization, polymorphic: true, index: true
end
def down
remove_reference :doctors, :doctor_specialization, polymorphic: true
end
end
Run Code Online (Sandbox Code Playgroud)
当我跑,rake db:migrate我得到错误
Index name 'index_doctors_on_doctor_specialization_type_and_doctor_specialization_id' on table 'doctors' is too long; the limit is 63 characters
那么如何在使用add_reference时指定索引名称,就像我们指定的方式一样 add_index :table, :column, :name => 'index name'
ruby activerecord ruby-on-rails rails-migrations ruby-on-rails-4
我错误地从表格的id字段中删除了自动增量选项.谁能告诉我如何通过迁移重新插入自动增量选项?