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

Instance Method Details

#create_db(conf) ⇒ Object



17
18
19
20
21
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 17

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

#default_migration_pathObject



12
13
14
15
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 12

def default_migration_path
  ActiveRecord::Migrator.migrations_paths.first.freeze
  #'./db/migrate'.freeze
end

#drop_db(conf) ⇒ Object



46
47
48
49
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 46

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



37
38
39
40
41
42
43
44
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 37

def dump_schema(conf)
  ActiveRecord::Base.establish_connection(conf)
  require 'active_record/schema_dumper'
  schemaDumpOut = conf[:schema_dump_path] || File.expand_path('./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

Raises:



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 51

def generate_migration(conf, name, &block)
 
  raise MigrateError, "Migration name is required" if is_empty?(name)

  #name = "create_#{name.pluralize}"

  migPath = conf[:migration_spec_path] || File.expand_path(default_migration_path)
  FileUtils.mkdir_p(migPath) if not File.exist?(migPath)

  timestamp = Time.now.strftime("%Y%m%d%H%M%S%6N")
  # rails only take 14 characters as timestamp
  timestamp = timestamp[0...14]
  path = File.expand_path(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

Raises:



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ca/data_store/ar/migration/migration_helper.rb', line 23

def migrate(conf)
  ActiveRecord::Base.establish_connection(conf)

  migPath = conf[:migration_spec_path] || File.expand_path(default_migration_path)
  raise MigrateError, "Given migration spec path '#{migPath}' does not exist." if not File.exist?(migPath)

  teLogger.debug "Migration spec path : #{migPath}"
  # for AR <= 4
  #ActiveRecord::Migrator.migrate(migPath)
  # for AR >= 5
  ActiveRecord::Base.connection.migration_context.migrate
  dump_schema(conf)
end