Module: DumpHook
- Defined in:
- lib/dump_hook.rb,
lib/dump_hook/version.rb
Defined Under Namespace
Classes: Settings
Constant Summary collapse
- VERSION =
"0.0.6"
Class Attribute Summary collapse
-
.recreate ⇒ Object
Returns the value of attribute recreate.
-
.settings ⇒ Object
Returns the value of attribute settings.
Class Method Summary collapse
Instance Method Summary collapse
- #create_dirs_if_not_exists ⇒ Object
- #execute_with_dump(name, opts = {}, &block) ⇒ Object
- #full_filename(name, created_on, actual) ⇒ Object
- #mysql_connection_args ⇒ Object
- #pg_connection_args ⇒ Object
- #pg_env ⇒ Object
- #recreate?(filename) ⇒ Boolean
- #restore_dump(filename) ⇒ Object
- #settings ⇒ Object
- #store_dump(filename) ⇒ Object
Class Attribute Details
.recreate ⇒ Object
Returns the value of attribute recreate.
27 28 29 |
# File 'lib/dump_hook.rb', line 27 def recreate @recreate end |
.settings ⇒ Object
Returns the value of attribute settings.
27 28 29 |
# File 'lib/dump_hook.rb', line 27 def settings @settings end |
Class Method Details
Instance Method Details
#create_dirs_if_not_exists ⇒ Object
98 99 100 |
# File 'lib/dump_hook.rb', line 98 def create_dirs_if_not_exists FileUtils.mkdir_p(settings.dumps_location) end |
#execute_with_dump(name, opts = {}, &block) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/dump_hook.rb', line 35 def execute_with_dump(name, opts={}, &block) created_on = opts[:created_on] actual = opts[:actual] || settings.actual create_dirs_if_not_exists filename = full_filename(name, created_on, actual) if !recreate?(filename) && File.exist?(filename) restore_dump(filename) else if created_on Timecop.travel(created_on) elsif actual && settings.remove_old_dumps FileUtils.rm(Dir.glob(full_filename(name, nil, "*"))) end block.call Timecop.return store_dump(filename) end end |
#full_filename(name, created_on, actual) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/dump_hook.rb', line 88 def full_filename(name, created_on, actual) name_with_created_on = name if created_on name_with_created_on = "#{name_with_created_on}_#{created_on.strftime('%Y%m%d')}" elsif actual name_with_created_on = "#{name_with_created_on}_actual#{actual}" end "#{settings.dumps_location}/#{name_with_created_on}.dump" end |
#mysql_connection_args ⇒ Object
102 103 104 105 106 107 108 109 |
# File 'lib/dump_hook.rb', line 102 def mysql_connection_args args = [settings.database] args.concat ["-u", settings.username] if settings.username args.concat ["-p", settings.password] if settings.password args.concat ["-h", settings.host] if settings.host args.concat ["-P", settings.port] if settings.port args end |
#pg_connection_args ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/dump_hook.rb', line 111 def pg_connection_args args = ['-d', settings.database] args.concat(['-U', settings.username]) if settings.username args.concat(['-h', settings.host]) if settings.host args.concat(['-p', settings.port]) if settings.port args end |
#pg_env ⇒ Object
119 120 121 |
# File 'lib/dump_hook.rb', line 119 def pg_env settings.password ? { 'PGPASSWORD' => settings.password } : {} end |
#recreate?(filename) ⇒ Boolean
123 124 125 126 127 128 |
# File 'lib/dump_hook.rb', line 123 def recreate?(filename) DumpHook.recreate ||= [] result = (settings.recreate || ENV["DUMP_HOOK"] == "recreate") && !DumpHook.recreate.include?(filename) DumpHook.recreate << filename if result result end |
#restore_dump(filename) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/dump_hook.rb', line 74 def restore_dump(filename) case settings.database_type when 'postgres' args = ['--single-transaction'] args.concat(pg_connection_args) args << filename Kernel.system(pg_env, "pg_restore", *args) when 'mysql' args = mysql_connection_args args.concat ["-e", "source #{filename}"] Kernel.system("mysql", *args) end end |
#store_dump(filename) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/dump_hook.rb', line 58 def store_dump(filename) case settings.database_type when 'postgres' args = ['-a', '-x', '-O', '-f', filename, '-Fc', '-T', 'schema_migrations', '-T', 'ar_internal_metadata'] args.concat(pg_connection_args) Kernel.system(pg_env, "pg_dump", *args) when 'mysql' args = mysql_connection_args args << "--compress" args.concat ["--result-file", filename] args.concat ["--ignore-table", "#{settings.database}.schema_migrations"] args.concat ["--ignore-table", "#{settings.database}.ar_internal_metadata"] Kernel.system("mysqldump", *args) end end |