Class: DBFork

Inherits:
Object
  • Object
show all
Includes:
Commands, Logging
Defined in:
lib/database_fork/db_fork.rb

Direct Known Subclasses

MysqlFork

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Commands

#execute_commands, #record_command, #reset_commands!

Methods included from Logging

#log_debug, #log_info

Constructor Details

#initialize(root_dir, connection, env, branch_name, logger) ⇒ DBFork

Returns a new instance of DBFork.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/database_fork/db_fork.rb', line 35

def initialize(root_dir, connection, env, branch_name, logger)
  @root_dir = root_dir
  @connection = connection
  @env = env
  @branch_name = branch_name
  @logger = logger

  @character_set = nil
  @collation = nil

  reset_commands!
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



48
49
50
# File 'lib/database_fork/db_fork.rb', line 48

def commands
  @commands
end

Instance Method Details

#character_setObject



90
91
92
# File 'lib/database_fork/db_fork.rb', line 90

def character_set
  @character_set || 'utf8'
end

#collationObject



94
95
96
# File 'lib/database_fork/db_fork.rb', line 94

def collation
  @collation || 'utf8_unicode_ci'
end

#connection_parametersObject

implement this is your adapter:

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/database_fork/db_fork.rb', line 14

def connection_parameters
  raise NotImplementedError
end

#create_databaseObject

Raises:

  • (NotImplementedError)


22
23
24
# File 'lib/database_fork/db_fork.rb', line 22

def create_database
  raise NotImplementedError
end

#create_dumpObject

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/database_fork/db_fork.rb', line 18

def create_dump
  raise NotImplementedError
end

#delete_dump_fileObject



86
87
88
# File 'lib/database_fork/db_fork.rb', line 86

def delete_dump_file
  record_command "rm #{dump_file}", 'cleanup'
end

#dump_fileObject



98
99
100
# File 'lib/database_fork/db_fork.rb', line 98

def dump_file
  File.join(@root_dir, 'tmp', "dump_#{source_db}.sql")
end

#exists?(dry_run = false) ⇒ Boolean

implement this is your adapter:

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


9
10
11
# File 'lib/database_fork/db_fork.rb', line 9

def exists?(dry_run = false)
  raise NotImplementedError
end

#export_env(dry_run = false) ⇒ Object



79
80
81
82
83
84
# File 'lib/database_fork/db_fork.rb', line 79

def export_env(dry_run = false)
  reset_commands!
  filename = File.join(@root_dir, 'tmp', "DATABASE_FORK_#{@env.upcase}")
  record_command "echo #{target_name} > #{filename}", "setting DATABASE_FORK_#{@env.upcase}"
  execute_commands unless dry_run
end

#fork(dry_run = false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/database_fork/db_fork.rb', line 50

def fork(dry_run = false)
  reset_commands!

  log_info "creating database fork '#{target_name}' from #{source_db}"

  create_dump
  create_database
  import_dump
  delete_dump_file

  execute_commands unless dry_run
end

#import_dumpObject

Raises:

  • (NotImplementedError)


26
27
28
# File 'lib/database_fork/db_fork.rb', line 26

def import_dump
  raise NotImplementedError
end

#query_default_settings(dry_run = false) ⇒ Object

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/database_fork/db_fork.rb', line 30

def query_default_settings(dry_run = false)
  raise NotImplementedError
end

#reset_env(dry_run = false) ⇒ Object



72
73
74
75
76
77
# File 'lib/database_fork/db_fork.rb', line 72

def reset_env(dry_run = false)
  reset_commands!
  filename = File.join(@root_dir, 'tmp', "DATABASE_FORK_#{@env.upcase}")
  record_command "rm #{filename}", "removing DATABASE_FORK_#{@env.upcase}"
  execute_commands unless dry_run
end

#source_dbObject



68
69
70
# File 'lib/database_fork/db_fork.rb', line 68

def source_db
  @connection['database']
end

#target_nameObject



63
64
65
# File 'lib/database_fork/db_fork.rb', line 63

def target_name
  "#{source_db}_#{@branch_name}"
end