Module: Ca::DataStore::Ar::MigrationHelper
- Includes:
- TR::CondUtils, TeLogger::TeLogHelper
- Defined in:
- lib/ca/data_store/ar/migration/migration_helper.rb
Instance Method Summary collapse
- #create_db(conf) ⇒ Object
- #drop_db(conf) ⇒ Object
- #dump_schema(conf) ⇒ Object
- #generate_migration(conf, name, &block) ⇒ Object
- #migrate(conf) ⇒ Object
-
#migrate_for_rails(conf) ⇒ Object
rails has done too many customization on top of ActiverRecord such as supporting engine.
Instance Method Details
#create_db(conf) ⇒ Object
12 13 14 15 16 |
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 12 def create_db(conf) ActiveRecord::Base.establish_connection(conf) ActiveRecord::Base.connection.create_database(conf['database']) if ActiveRecord::Base.connection.respond_to?(:create_database) end |
#drop_db(conf) ⇒ Object
57 58 59 60 |
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 57 def drop_db(conf) ActiveRecord::Base.establish_connection(conf) ActiveRecord::Base.connection.drop_database(conf['database']) if ActiveRecord::Base.connection.respond_to?(:drop_database) end |
#dump_schema(conf) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 48 def dump_schema(conf) ActiveRecord::Base.establish_connection(conf) require 'active_record/schema_dumper' schemaDumpOut = conf[:schema_dump_path] || File.('./schema.rb') File.open(schemaDumpOut,"w:utf-8") do |f| ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, f) end end |
#generate_migration(conf, name, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 62 def generate_migration(conf, name, &block) raise MigrateError, "Migration name is required" if is_empty?(name) migPath = conf["migration_path"] || File.("./db/migrate") FileUtils.mkdir_p(migPath) if not File.exist?(migPath) teLogger.debug "migPath : #{migPath}" = Time.now.strftime("%Y%m%d%H%M%S%6N") # rails only take 14 characters as timestamp = [0...14] path = File.(File.join(migPath,"#{timestamp}_#{name}.rb")) migration_class = name.split("_").map(&:capitalize).join arVer = ActiveRecord.version.to_s tv = arVer.split(".")[0..1] File.open(path, 'w') do |file| file.write "\nclass \#{migration_class} < ActiveRecord::Migration[\#{tv.join('.')}]\n def change \n \#{block ? block.call(:table_setup) : \"\"}\n end\nend\n EOF\n end \n\n teLogger.debug \"Migration \#{path} written\"\n\n path\nend\n" |
#migrate(conf) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 18 def migrate(conf) teLogger.debug "Migrating : #{conf}" ActiveRecord::Base.establish_connection(conf) # for AR <= 4 #ActiveRecord::Migrator.migrate(migPath) # for AR >= 5 ctx = ActiveRecord::Base.connection.migration_context # only keep the one in the config file if not_empty?(conf["migration_path"]) ctx.migrations_paths.clear ctx.migrations_paths << conf["migration_path"] end teLogger.debug "Migrations Paths : #{ctx.migrations_paths.inspect}" ctx.migrate #ActiveRecord::Base.connection.migration_context.migrate dump_schema(conf) end |
#migrate_for_rails(conf) ⇒ Object
rails has done too many customization on top of ActiverRecord such as supporting engine. Short cuting that is not a wise choice
41 42 43 44 45 46 |
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 41 def migrate_for_rails(conf) # detacting if Rails is present if defined?(Rails::Application) end end |