bootstrap-db 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bootstrap-db.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tom Meier
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # Bootstrap-DB
2
+
3
+ ## Purpose
4
+
5
+ Collection of rake tasks for speeding up the capture and load of database snapshots (dumps).
6
+ Currently accepting MySQL & Postgres databases and specifically gear to Rails applications.
7
+
8
+ ## Add this line to your application's Gemfile:
9
+
10
+ ```
11
+ gem 'bootstrap-db'
12
+ ```
13
+
14
+ ## Commands (with example usage)
15
+
16
+ ### Database Dump
17
+
18
+ ```
19
+ rake bootstrap:db:dump #Dump default to db/bootstrap/bootstrap_data.sql
20
+ rake bootstrap:db:dump Rails.env=production #Dump specific Rails environment using database.yml
21
+ rake bootstrap:db:dump FILE=db/bootstrap/live_database.dump #Dump to specific location
22
+ rake bootstrap:db:dump FILE_NAME=live_database.dump #Dump specific file to default bootstrap location
23
+ ```
24
+
25
+ #### Additional options:
26
+
27
+ * Dump file with comma delimited tables ignored:
28
+ ```
29
+ rake bootstrap:db:dump IGNORE_TABLES='messages,incidents'
30
+ ```
31
+
32
+ ### Database Load
33
+
34
+ Load, and overwrite, current database environment with a passed file name.
35
+
36
+ ```
37
+ rake bootstrap:db:load #Load default from db/bootstrap/bootstrap_data.sql
38
+ rake bootstrap:db:load Rails.env=production #Load specific Rails environment using database.yml
39
+ rake bootstrap:db:load FILE=db/bootstrap/live_database_dump.sql #Load from specific dump
40
+ rake bootstrap:db:load FILE_NAME=live_database_dump.sql #Load specific file from default bootstrap location
41
+ ```
42
+
43
+ ### Additional options:
44
+
45
+ Pass in any additional parameters that your database accepts:
46
+ * eg. mysqldump *--help* / pg_dump *--help*
47
+
48
+ ```
49
+ rake bootstrap:db:dump ADDITIONAL_PARAMS='-d,-t'
50
+ rake bootstrap:db:load ADDITIONAL_PARAMS='-d,-t'
51
+ ```
52
+
53
+ Pass 'VERBOSE=true' if you'd like to see more information. For example:
54
+
55
+ ```
56
+ rake bootstrap:db:dump VERBOSE=true
57
+ ```
58
+
59
+ ## Requirements
60
+
61
+ * Rails
62
+ * config/database.yml exists and set correctly
63
+ * database.yml has a 'host' value set for environments
64
+ * mysql/postgresql
65
+
66
+
67
+ ## TODO (if we end up using this more)
68
+
69
+ * This has been quickly rebuilt from a ridiculously old project of mine (http://github.com/tommeier/bootstrap). This should be refactored into proper objects and expose classes as well as rake tasks. Fully tested.
70
+ * Proper mapping of parameter for dumping (eg. --username) to database config
71
+ * List required attributes for each database (like `host` and raise on missing)
72
+ * Load config once, and apply a bunch of custom items. For instance, mysql default bootstrap should be a *.sql file and the default for postgres should be *.dump for faster loads
73
+
74
+ ## Contributing
75
+
76
+ 1. Fork it
77
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
78
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
79
+ 4. Create some tests
80
+ 5. Push to the branch (`git push origin my-new-feature`)
81
+ 6. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bootstrap/db/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bootstrap-db"
8
+ spec.version = Bootstrap::Db::VERSION
9
+ spec.authors = ["Tom Meier"]
10
+ spec.email = ["tom@venombytes.com"]
11
+ spec.description = %q{Database dump and loader}
12
+ spec.summary = %q{Database dump and loader}
13
+ spec.homepage = "http://github.com/tommeier/bootstrap-db"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'railties'
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,47 @@
1
+ module Bootstrap
2
+ module Db
3
+ class Config
4
+ DB_CONFIG_PATH = 'config/database.yml'
5
+
6
+ attr_accessor :settings, :adapter, :dump_name, :dump_path, :dump_dir
7
+
8
+ def initialize(settings)
9
+ self.settings = settings
10
+ self.adapter = settings[Rails.env]["adapter"].to_sym
11
+
12
+ self.dump_path = ENV['FILE'] || File.join(default_dump_path, ENV['FILE_NAME'] || default_dump_name)
13
+ self.dump_name = File.basename(self.dump_path)
14
+ self.dump_dir = File.dirname(self.dump_path)
15
+ end
16
+
17
+ def self.load!(configuration_path = File.join(Rails.root, DB_CONFIG_PATH))
18
+ unless File.exists?(configuration_path)
19
+ raise "Error - Please ensure your '#{File.basename(configuration_path)}' exists"
20
+ end
21
+
22
+ config = YAML::load(ERB.new(IO.read(configuration_path)).result)
23
+
24
+ unless config[Rails.env]["host"]
25
+ raise "Please ensure your '#{File.basename(configuration_path)}' file has a host for the database. eg. host = localhost"
26
+ end
27
+
28
+ new(config)
29
+ end
30
+
31
+ private
32
+
33
+ def default_dump_path
34
+ @default_dump_path ||= File.join(Rails.root, 'db', 'bootstrap')
35
+ end
36
+
37
+ def default_dump_name
38
+ @default_dump_name ||= case self.adapter
39
+ when :postgresql
40
+ 'bootstrap_data.dump' #Custom format 'c'
41
+ else
42
+ 'bootstrap_data.sql' #MySQL
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,7 @@
1
+ module Bootstrap
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load "bootstrap/db/tasks/bootstrap-db.rake"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ module Bootstrap
2
+ module Db
3
+ module RakeHelper
4
+
5
+ def log(output)
6
+ STDERR.puts "[bootstrap-db] #{output}"
7
+ end
8
+
9
+ def display_and_execute(command)
10
+ log(command) if ENV['VERBOSE'] == 'true'
11
+ execute_command(command)
12
+ end
13
+
14
+ def execute_command(command)
15
+ output = `#{command} 2>&1`
16
+ raise "Error : #{output}" unless $?.success?
17
+ output
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,88 @@
1
+ require File.join(File.dirname(__FILE__), '../rake_helper')
2
+
3
+ namespace :bootstrap do
4
+ namespace :db do
5
+ include Bootstrap::Db::RakeHelper
6
+
7
+ desc "Dump the current database to a SQL file"
8
+ task :dump => :environment do
9
+ config = Bootstrap::Db::Config.load!
10
+
11
+ settings = config.settings[Rails.env]
12
+ ignore_tables = ENV['IGNORE_TABLES'].split(',') if ENV['IGNORE_TABLES']
13
+ passed_params = ENV['ADDITIONAL_PARAMS'].split(',') if ENV['ADDITIONAL_PARAMS']
14
+
15
+ #Create directories if they don't exist
16
+ Dir.mkdir config.dump_dir unless File.exists?(config.dump_dir)
17
+
18
+ log "Generating dump of database: '#{config.dump_name}'"
19
+
20
+ case config.adapter
21
+ when :mysql
22
+ #mysqldump --help
23
+ default_sql_attrs = "-q --add-drop-table --add-locks --extended-insert --lock-tables --single-transaction"
24
+ if ignore_tables
25
+ ignore_tables.each do |table_name|
26
+ default_sql_attrs += " --ignore-table=#{settings["database"]}.#{table_name.strip}"
27
+ end
28
+ end
29
+
30
+ if passed_params
31
+ passed_params.each do |param|
32
+ default_sql_attrs += " #{param}"
33
+ end
34
+ end
35
+
36
+ password_attrs = " -p#{settings["password"]}" if settings["password"]
37
+ #--all-tablespaces
38
+ display_and_execute("mysqldump #{default_sql_attrs} -h #{settings["host"]} -u #{settings["username"]}#{password_attrs} #{settings["database"]} > #{config.dump_path}")
39
+
40
+ when :postgresql
41
+ #pg_dump --help
42
+ default_sql_attrs = "--clean --format=c"
43
+
44
+ if ignore_tables.present?
45
+ ignore_tables.each do |table_name|
46
+ default_sql_attrs += " --exclude-table=#{settings["database"]}.#{table_name.strip}"
47
+ end
48
+ end
49
+
50
+ if passed_params.present?
51
+ passed_params.each do |param|
52
+ default_sql_attrs += " #{param}"
53
+ end
54
+ end
55
+
56
+ user_attribute = " --username=#{settings["username"]}" if settings['username']
57
+
58
+ display_and_execute("pg_dump #{default_sql_attrs} --host=#{settings["host"]} --port=#{settings["port"] || 5432}#{user_attribute} --file=#{config.dump_path} #{settings["database"]}")
59
+ else
60
+ raise "Error : Task not supported by '#{settings['adapter']}'"
61
+ end
62
+ log "Dump completed --> '#{config.dump_path}'"
63
+ end
64
+
65
+ desc "Load a SQL dump into the current environment"
66
+ task :load => :environment do
67
+ config = Bootstrap::Db::Config.load!
68
+ settings = config.settings[Rails.env]
69
+ raise "Unable to find dump at location - '#{config.dump_path}'" unless File.exists?(config.dump_path)
70
+
71
+ log "Loading dump: '#{config.dump_name}'"
72
+
73
+ case config.adapter
74
+ when :mysql
75
+ password_attrs = " -p#{settings["password"]}" if settings["password"]
76
+ display_and_execute("mysql -f -h #{settings["host"]} -u #{settings["username"]}#{password_attrs.to_s} #{settings["database"]} < #{config.dump_path}")
77
+ when :postgresql
78
+ default_sql_attrs = "--exit-on-error --clean --single-transaction --format=c"
79
+ user_attribute = " --username=#{settings["username"]}" if settings['username']
80
+ display_and_execute("pg_restore #{default_sql_attrs} --host=#{settings["host"]} --port=#{settings["port"] || 5432} --dbname=#{settings["database"]}#{user_attribute} #{config.dump_path}")
81
+ else
82
+ raise "Task not supported by '#{settings['adapter']}'"
83
+ end
84
+
85
+ log "Database load completed..."
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ module Bootstrap
2
+ module Db
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ require 'bootstrap/db/version'
2
+
3
+ module Bootstrap
4
+ module Db
5
+ end
6
+ end
7
+
8
+ require 'bootstrap/db/config'
9
+ require 'bootstrap/db/railtie'
10
+
11
+
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bootstrap-db
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tom Meier
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: railties
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Database dump and loader
63
+ email:
64
+ - tom@venombytes.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - bootstrap-db.gemspec
75
+ - lib/bootstrap/db.rb
76
+ - lib/bootstrap/db/config.rb
77
+ - lib/bootstrap/db/railtie.rb
78
+ - lib/bootstrap/db/rake_helper.rb
79
+ - lib/bootstrap/db/tasks/bootstrap-db.rake
80
+ - lib/bootstrap/db/version.rb
81
+ homepage: http://github.com/tommeier/bootstrap-db
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.23
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Database dump and loader
106
+ test_files: []