7
A
回答
3
在非Rails项目中有一个使用Rails数据库迁移的gem。它的名字是“standalone_migrations”
这里是一个链接
4
1
如果你不喜欢用耙,但仍获得的系统的一部分ActiveRecord :: Migration,那么你可以使用以下来处理普通红宝石(没有任何导轨)的起伏:
require 'active_record'
require 'benchmark'
# Migration method, which does not uses files in db/migrate but in-memory migrations
# Based on ActiveRecord::Migrator::migrate
def migrate(migrations, target_version = nil)
direction = case
when target_version.nil?
:up
when (ActiveRecord::Migrator::current_version == target_version)
return # do nothing
when ActiveRecord::Migrator::current_version > target_version
:down
else
:up
end
ActiveRecord::Migrator.new(direction, migrations, target_version).migrate
puts "Current version: #{ActiveRecord::Migrator::current_version}"
end
# MigrationProxy deals with loading Migrations from files, we reuse it
# to create instances of the migration classes we provide
class MigrationClassProxy < ActiveRecord::MigrationProxy
def initialize(migrationClass, version)
super(migrationClass.name, version, nil, nil)
@migrationClass = migrationClass
end
def mtime
0
end
def load_migration
@migrationClass.new(name, version)
end
end
# Hash of all our migrations
migrations = {
2016_08_09_2013_00 =>
class CreateSolutionTable < ActiveRecord::Migration[5.0]
def change
create_table :solution_submissions do |t|
t.string :problem_hash, index: true
t.string :solution_hash, index: true
t.float :resemblance
t.timestamps
end
end
self # Necessary to get the class instance into the hash!
end,
2016_08_09_2014_16 =>
class CreateProductFields < ActiveRecord::Migration[5.0]
# ...
self
end
}.map { |key,value| MigrationClassProxy.new(value, key) }
ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => 'XXX.db'
)
# Play all migrations (rake db:migrate)
migrate(migrations, migrations.last.version)
# ... or undo them (rake db:migrate VERSION=0)
migrate(migrations, 0)
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class SolutionSubmission < ApplicationRecord
end
相关问题
- 1. Rails迁移未生成迁移文件
- 2. 无法在rails上生成迁移2.3.4
- 3. Rails - 从模型生成迁移脚本
- 4. Rails迁移以生成日历表
- 5. Rails的生成迁移文件支架
- 6. 从模式生成Rails迁移
- 7. “迁移:创建”和“迁移:生成”之间有什么区别?
- 8. 学说1.2生成的迁移-Diff,产生唯一的外键迁移
- 9. 自动生成新迁移
- 10. Rails的迁移产生不产生列
- 11. Rails 5 - 在rake任务中生成并运行迁移
- 12. 有没有办法不生成在rails中的迁移文件
- 13. 我无法在rails中生成模型或迁移
- 14. 在Rails迁移中迁移数据
- 15. 插入Rails的两个现有的迁移之间的迁移
- 16. Rails迁移问题 - 外键未创建
- 17. Rails ActiveRecord迁移成功迁移后不创建一些列
- 18. Rails迁移表
- 19. rails HABTM迁移
- 20. Rails迁移
- 21. 从Rails模式更新生成迁移,而不是以其他方式生成
- 22. Rails 3生成迁移 - 没有向上或向下的方法
- 23. Ruby on Rails自定义迁移生成器
- 24. 如何生成迁移以更改Rails中的数据库列
- 25. 插件中的生成器和迁移(rails 3)
- 26. 命令'rails生成迁移CreateUsers'的哪个方法调用?
- 27. 生成的Rails模型 - 迁移将如何运行?
- 28. 如何使用Rails :: Generators.invoke(“active_record:migration”)生成创建表迁移文件?
- 29. 额外changeColumns产生的迁移-DIFF
- 30. Cakephp 3.0由迁移生成的外键烘烤错误
虽然这个链接可以回答这个问题,最好在这里包含答案的重要部分,并提供参考链接。如果链接页面更改,则仅链接答案可能会失效。 – 2012-10-04 08:24:06