Class: DbAgent::DbHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/db_agent/db_handler.rb,
lib/db_agent/db_handler/mssql.rb,
lib/db_agent/db_handler/mysql.rb,
lib/db_agent/db_handler/postgresql.rb

Direct Known Subclasses

MSSQL, MySQL, PostgreSQL

Defined Under Namespace

Classes: MSSQL, MySQL, PostgreSQL

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ DbHandler

Returns a new instance of DbHandler.



4
5
6
7
8
9
10
11
12
13
14
# File 'lib/db_agent/db_handler.rb', line 4

def initialize(options)
  @config = options[:config]
  @superconfig = options[:superconfig]
  @root_folder = options[:root]
  @backup_folder = options[:backup] || options[:root]/'backups'
  @schema_folder = options[:schema] || options[:root]/'schema'
  @migrations_folder = options[:migrations] || options[:root]/'migrations'
  @data_folder = options[:data] || options[:root]/'data'
  @viewpoints_folder = options[:viewpoints] || options[:root]/'viewpoints'
  require_viewpoints!
end

Instance Attribute Details

#backup_folderObject (readonly)

Returns the value of attribute backup_folder.



16
17
18
# File 'lib/db_agent/db_handler.rb', line 16

def backup_folder
  @backup_folder
end

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/db_agent/db_handler.rb', line 15

def config
  @config
end

#data_folderObject (readonly)

Returns the value of attribute data_folder.



17
18
19
# File 'lib/db_agent/db_handler.rb', line 17

def data_folder
  @data_folder
end

#migrations_folderObject (readonly)

Returns the value of attribute migrations_folder.



16
17
18
# File 'lib/db_agent/db_handler.rb', line 16

def migrations_folder
  @migrations_folder
end

#schema_folderObject (readonly)

Returns the value of attribute schema_folder.



16
17
18
# File 'lib/db_agent/db_handler.rb', line 16

def schema_folder
  @schema_folder
end

#superconfigObject (readonly)

Returns the value of attribute superconfig.



15
16
17
# File 'lib/db_agent/db_handler.rb', line 15

def superconfig
  @superconfig
end

#viewpoints_folderObject (readonly)

Returns the value of attribute viewpoints_folder.



17
18
19
# File 'lib/db_agent/db_handler.rb', line 17

def viewpoints_folder
  @viewpoints_folder
end

Class Method Details

.factor(options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/db_agent/db_handler.rb', line 94

def self.factor(options)
  case options[:config][:adapter]
  when 'postgres'
    PostgreSQL.new(options)
  when 'mssql'
    MSSQL.new(options)
  when 'mysql'
    MySQL.new(options)
  else
    PostgreSQL.new(options)
  end
end

Instance Method Details

#backupObject

Raises:

  • (NotImplementedError)


33
34
35
# File 'lib/db_agent/db_handler.rb', line 33

def backup
  raise NotImplementedError
end

#createObject

Raises:

  • (NotImplementedError)


25
26
27
# File 'lib/db_agent/db_handler.rb', line 25

def create
  raise NotImplementedError
end

#dropObject

Raises:

  • (NotImplementedError)


29
30
31
# File 'lib/db_agent/db_handler.rb', line 29

def drop
  raise NotImplementedError
end

#migrate(version = nil) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/db_agent/db_handler.rb', line 78

def migrate(version = nil)
  Sequel.extension :migration
  if (sf = migrations_folder/'superuser').exists?
    Sequel::Migrator.run(sequel_superdb, migrations_folder/'superuser', table: 'superuser_migrations', target: version)
  end
  Sequel::Migrator.run(sequel_db, migrations_folder, target: version)
end

#pingObject



19
20
21
22
23
# File 'lib/db_agent/db_handler.rb', line 19

def ping
  puts "Using #{config}"
  sequel_db.test_connection
  puts "Everything seems fine!"
end

#replObject

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/db_agent/db_handler.rb', line 37

def repl
  raise NotImplementedError
end

#require_viewpoints!Object



121
122
123
124
# File 'lib/db_agent/db_handler.rb', line 121

def require_viewpoints!
  f = viewpoints_folder.expand_path
  Path.require_tree(f) if f.directory?
end

#restore(t, args) ⇒ Object

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/db_agent/db_handler.rb', line 74

def restore(t, args)
  raise NotImplementedError
end

#sequel_dbObject



107
108
109
# File 'lib/db_agent/db_handler.rb', line 107

def sequel_db
  @sequel_db ||= ::Sequel.connect(config)
end

#sequel_superdbObject



111
112
113
114
# File 'lib/db_agent/db_handler.rb', line 111

def sequel_superdb
  raise "No superconfig set" if superconfig.nil?
  @sequel_superdb ||= ::Sequel.connect(superconfig)
end

#spyObject

Raises:

  • (NotImplementedError)


90
91
92
# File 'lib/db_agent/db_handler.rb', line 90

def spy
  raise NotImplementedError
end

#system(cmd, *args) ⇒ Object



116
117
118
119
# File 'lib/db_agent/db_handler.rb', line 116

def system(cmd, *args)
  puts cmd
  ::Kernel.system(cmd, *args)
end

#waitObject



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/db_agent/db_handler.rb', line 60

def wait
  15.downto(0) do |i|
    begin
      puts "Using #{config}"
      sequel_db.test_connection
      puts "Database is there. Great."
      break
    rescue Sequel::Error
      raise if i==0
      sleep(1)
    end
  end
end

#wait_serverObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/db_agent/db_handler.rb', line 41

def wait_server
  require 'net/ping'
  raise "No host found" unless config[:host]
  check = Net::Ping::External.new(config[:host])
  puts "Trying to ping `#{config[:host]}`"
  15.downto(0) do |i|
    print "."
    if check.ping?
      print "\nServer found.\n"
      break
    elsif i == 0
      print "\n"
      raise "Server not found, I give up."
    else
      sleep(1)
    end
  end
end