我想改变表格中表格的结构,我需要用旧字段的值来回填一些新的字段。因此,我想在迁移中使用存储库。但是,看来我需要加载Hanami模型才能使用存储库,因为Hanami在运行迁移时没有加载它。
所以,现在我有一个:
require_relative '../../lib/my_app/repositories/entity_repository'
require_relative '../../lib/my_app/entities/my_entity'
Mutex.new.synchronize do
Hanami::Model.load!
end
Hanami::Model.migration do
change do
def backfill_data!
repo = EntityRepository.new
repo.all.map do |entity|
repo.update entity.id, new_field_as_date: Date.new(entity.old_field)
end
end
backfill_data!
end
end但是当运行这个迁移时,我得到
bundler: failed to load command: hanami (/Users/user/.rbenv/versions/2.4.1/bin/hanami)
Hanami::Model::Error: key not found: :person
# which is an association with the entity mentioned on the code所以,我现在不知道该怎么做。最大的问题是,如何迁移关于Hanami迁移的数据?
发布于 2017-10-05 14:43:42
我不知道这个具体的问题,但是我强烈反对您在迁移中使用存储库。因为存储库与数据库表的当前架构紧密相关,因此如果将来运行相同的迁移,它可能无法工作。
您应该直接使用数据库设施。这将保证迁移始终正常工作:
Hanami::Model.migration do
up do
run "UPDATE users SET last_login_at=(last_login_at - INTERVAL '1 day')"
end
down do
end
endhttps://stackoverflow.com/questions/46536033
复制相似问题