Class: DBSupplier::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/db_supplier/migrator.rb

Class Method Summary collapse

Class Method Details

.clientObject



86
87
88
89
90
91
92
93
# File 'lib/db_supplier/migrator.rb', line 86

def client
  ac = @access_token || ENV['GITHUB_ACCESS_TOKEN'] || (raise RuntimeError, 'undefined access_token')

  @client ||= begin
                Octokit.api_endpoint = @github_api_endpoint if @github_api_endpoint
                Octokit::Client.new(access_token: ac)
              end
end

.configurationsObject



9
10
11
# File 'lib/db_supplier/migrator.rb', line 9

def configurations
  @configurations
end

.configurations=(config = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/db_supplier/migrator.rb', line 13

def configurations=(config={})
  @configurations = config

  @schema_repository = config[:schema_repository]
  @schema_ref        = config[:schema_ref] || 'master'
  @schema_files      = config[:schema_files].symbolize_keys
  @access_token      = config[:access_token]

  @github_api_endpoint = config[:github_api_endpoint]

  @logger = config[:logger] || Logger.new(STDOUT)
end

.databasesObject



95
96
97
# File 'lib/db_supplier/migrator.rb', line 95

def databases
  @schema_files.try(:keys) || []
end

.fetch_sql(db_name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/db_supplier/migrator.rb', line 60

def fetch_sql(db_name)
  migration_file_paths = @schema_files.try(:fetch, db_name.to_sym) || (raise RuntimeError, "undefined #{db_name} schemat")
  repository = @schema_repository || (raise RuntimeError, 'undefined schema repository')

  Array(migration_file_paths).map do |path|
    client.contents(
      repository,
      ref: @schema_ref,
      path: path,
      headers: {
        accept: 'application/vnd.github.VERSION.raw'
      }
    )
  end
end

.migrateObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/db_supplier/migrator.rb', line 26

def migrate
  @logger.info "----- migrate start -----"

  databases.each do |database|
    @logger.info "----- #{database} migrate start -----"

    if (ActiveRecord.const_defined?(:Import))
      connection = ActiveRecord::Base.establish_connection_without_activerecord_import(database).connection
    else
      connection = ActiveRecord::Base.establish_connection(database).connection
    end

    @logger.debug "----- connected -----"

    sqls = fetch_sql(database)
    sqls.each do |sql|
      statements = sql.split(/;/)

      statements.each do |query|
        next if query == "\n\n"

        @logger.debug "----- query execute -----"
        connection.execute(query)
        @logger.debug query
        @logger.debug "----- query success -----"
      end
    end

    @logger.info "----- #{database} migrate finished -----"
  end

  @logger.info "----- migrate finished -----"
end

.show_sqls(db_name = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/db_supplier/migrator.rb', line 76

def show_sqls(db_name=nil)
  return fetch_sql(db_name) if db_name

  sqls = databases.map do |db_name|
    fetch_sql(db_name)
  end

  return sqls.join("\n")
end