dbmanager 0.1.4 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data/CHANGELOG.md +8 -0
  2. data/Guardfile +25 -0
  3. data/README.md +34 -19
  4. data/Rakefile +1 -1
  5. data/dbmanager.gemspec +4 -1
  6. data/features/dump.feature +17 -0
  7. data/features/rake_tasks.feature +20 -0
  8. data/features/readme.md +121 -0
  9. data/features/step_definitions/dump_steps.rb +14 -0
  10. data/features/step_definitions/rake_tasks_steps.rb +18 -0
  11. data/features/support/dummy_app_initialization.rb +21 -0
  12. data/features/support/env.rb +0 -0
  13. data/lib/dbmanager.rb +13 -10
  14. data/lib/dbmanager/adapters.rb +6 -0
  15. data/lib/dbmanager/adapters/mysql.rb +10 -0
  16. data/lib/dbmanager/version.rb +1 -1
  17. data/lib/tasks/dbmanager.rake +2 -0
  18. data/rails_generators/dbmanager/dbmanager_generator.rb +1 -5
  19. data/spec/dummy/.gitignore +21 -0
  20. data/spec/dummy/Gemfile +12 -0
  21. data/spec/dummy/README.rdoc +261 -0
  22. data/spec/dummy/Rakefile +7 -0
  23. data/spec/dummy/app/assets/images/rails.png +0 -0
  24. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  25. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  26. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  27. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  28. data/spec/dummy/app/mailers/.gitkeep +0 -0
  29. data/spec/dummy/app/models/.gitkeep +0 -0
  30. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  31. data/spec/dummy/config.ru +4 -0
  32. data/spec/dummy/config/application.rb +62 -0
  33. data/spec/dummy/config/boot.rb +6 -0
  34. data/spec/dummy/config/database.yml +11 -0
  35. data/spec/dummy/config/environment.rb +5 -0
  36. data/spec/dummy/config/environments/development.rb +37 -0
  37. data/spec/dummy/config/environments/production.rb +67 -0
  38. data/spec/dummy/config/environments/test.rb +37 -0
  39. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  40. data/spec/dummy/config/initializers/inflections.rb +15 -0
  41. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  42. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  43. data/spec/dummy/config/initializers/session_store.rb +8 -0
  44. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  45. data/spec/dummy/config/locales/en.yml +5 -0
  46. data/spec/dummy/config/routes.rb +58 -0
  47. data/spec/dummy/db/schema.rb +16 -0
  48. data/spec/dummy/db/seeds.rb +7 -0
  49. data/spec/dummy/doc/README_FOR_APP +2 -0
  50. data/spec/dummy/lib/assets/.gitkeep +0 -0
  51. data/spec/dummy/lib/tasks/.gitkeep +0 -0
  52. data/spec/dummy/log/.gitkeep +0 -0
  53. data/spec/dummy/public/404.html +26 -0
  54. data/spec/dummy/public/422.html +26 -0
  55. data/spec/dummy/public/500.html +25 -0
  56. data/spec/dummy/public/favicon.ico +0 -0
  57. data/spec/dummy/public/index.html +241 -0
  58. data/spec/dummy/public/robots.txt +5 -0
  59. data/spec/dummy/script/rails +6 -0
  60. data/spec/dummy/test/fixtures/.gitkeep +0 -0
  61. data/spec/dummy/test/functional/.gitkeep +0 -0
  62. data/spec/dummy/test/integration/.gitkeep +0 -0
  63. data/spec/dummy/test/performance/browsing_test.rb +12 -0
  64. data/spec/dummy/test/test_helper.rb +13 -0
  65. data/spec/dummy/test/unit/.gitkeep +0 -0
  66. data/spec/dummy/vendor/assets/javascripts/.gitkeep +0 -0
  67. data/spec/dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  68. data/spec/dummy/vendor/plugins/.gitkeep +0 -0
  69. data/spec/fixtures/{config → rails/config}/database.yml +0 -0
  70. data/spec/fixtures/{config → rails/config}/dbmanager_override.yml +0 -0
  71. data/spec/lib/adapters/mysql_spec.rb +25 -6
  72. data/spec/lib/importable_spec.rb +1 -1
  73. data/spec/lib/yml_parser_spec.rb +1 -1
  74. data/spec/spec_helper.rb +1 -1
  75. metadata +177 -8
@@ -1,3 +1,11 @@
1
+ ## v0.1.6
2
+
3
+ * close issue #5 (should not require 'dbmanager.rb' in the rake file)
4
+
5
+ ## v0.1.5
6
+
7
+ * import task creates the target db if necessary
8
+
1
9
  ## v0.1.4
2
10
 
3
11
  * environments are now listed sorted
@@ -0,0 +1,25 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+ # Capybara request specs
17
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
18
+ end
19
+
20
+
21
+ guard 'cucumber' do
22
+ watch(%r{^features/.+\.feature$})
23
+ watch(%r{^features/support/.+$}) { 'features' }
24
+ watch(%r{^features/step_definitions/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'features' }
25
+ end
data/README.md CHANGED
@@ -33,7 +33,7 @@ rake db:dump
33
33
  ```
34
34
  This rake task will dump the requested db to a file on the local machine.
35
35
 
36
- **You will be prompted to choose the target dir** (defaults to *tmp* in the rails
36
+ You will be prompted to choose the target dir (defaults to *tmp* in the rails
37
37
  root) and the sql file name (sql extension will be added automatically). If the
38
38
  file already exists, it will be overwritten.
39
39
 
@@ -44,7 +44,7 @@ file already exists, it will be overwritten.
44
44
  rake db:import
45
45
  ```
46
46
 
47
- **You will be prompted to choose the source and the target environment db**, and the
47
+ You will be prompted to choose the source and the target environment db, and the
48
48
  source db will be imported into the target db.
49
49
 
50
50
  This task will import a source db to a destination db. Tipical use is to import
@@ -59,47 +59,62 @@ overwrite them unless you explicitly override this setting in the override file
59
59
  choose to overwite. I take no responsibility for misuse or bugs in the code ;-)
60
60
 
61
61
 
62
- #### Override database.yml
62
+ #### Override database.yml and custom configurations
63
63
 
64
64
  Since some settings may be specific to the server environment (ie. host could
65
65
  be a private ip not reachable from elsewhere) you can override the settings in
66
66
  database.yml by adding a dbmanager_override.yml file in your rails config dir.
67
67
 
68
- You can also use this file to tell the dumper to ignore certain tables with
69
- the ignoretables directive:
70
-
71
- ```yaml
72
- beta:
73
- ignoretables:
74
- - users
75
- - prods_view
76
- ```
77
-
78
- Another use is to set some environment as protected, or on the other hand allow
68
+ Tipical use is to set some environment as protected, or on the other hand allow
79
69
  overwriting if it's protected by default (ie. production env).
80
70
 
81
- For example if you want to override the following setting, and make the database
82
- protected from overwriting:
71
+ If you want to override the following setting in the database.yml file making
72
+ the database protected from overwriting and changing the host address to a
73
+ public one:
83
74
 
84
75
  ```yaml
85
76
  beta:
86
77
  host: 192.168.0.1
87
78
  ```
88
- you should put this in dbmanager_override.yml:
79
+ you should put this in config/dbmanager_override.yml:
89
80
 
90
81
  ```yaml
91
82
  beta:
92
- host: 234.234.234.234
93
83
  protected: true
84
+ host: 234.234.234.234
94
85
  ```
95
86
 
96
- Instead, if you want to make the production env writable you should add this:
87
+ Instead, if you want to make the production env writable you should add this to
88
+ the config/dbmanager_override.yml file:
97
89
 
98
90
  ```yaml
99
91
  production:
100
92
  protected: false
101
93
  ```
102
94
 
95
+ On mysql you can instruct the dumper to ignore certain tables using the
96
+ ignoretables directive:
97
+
98
+ ```yaml
99
+ beta:
100
+ ignoretables:
101
+ - users
102
+ - prods_view
103
+ ```
104
+
105
+
106
+ ## Documentation
107
+
108
+ You can find some more documentation on the workings of the gem on relish:
109
+ https://www.relishapp.com/spaghetticode/dbmanager/docs
110
+
111
+
112
+ ## Tests
113
+
114
+ run rspec tests: ```rake```
115
+
116
+ run cucumber tests: ```cucumber```
117
+
103
118
 
104
119
  ### TODO
105
120
 
data/Rakefile CHANGED
@@ -4,5 +4,5 @@ Bundler::GemHelper.install_tasks
4
4
  require 'bundler/gem_tasks'
5
5
  require 'rspec/core/rake_task'
6
6
 
7
- Rspec::Core::RakeTask.new(:spec)
7
+ RSpec::Core::RakeTask.new(:spec)
8
8
  task :default => :spec
@@ -8,7 +8,7 @@ Gem::Specification.new do |s|
8
8
  s.platform = Gem::Platform::RUBY
9
9
  s.authors = ["andrea longhi"]
10
10
  s.email = ["andrea.longhi@mikamai.com"]
11
- s.homepage = ""
11
+ s.homepage = "https://github.com/spaghetticode/dbmanager"
12
12
  s.summary = %q{database manager}
13
13
  s.description = %q{helps manage db dumps and imports via rake tasks}
14
14
 
@@ -23,4 +23,7 @@ Gem::Specification.new do |s|
23
23
 
24
24
  s.add_development_dependency 'rspec'
25
25
  s.add_development_dependency 'rake'
26
+ s.add_development_dependency 'cucumber'
27
+ s.add_development_dependency 'guard-rspec'
28
+ s.add_development_dependency 'guard-cucumber'
26
29
  end
@@ -0,0 +1,17 @@
1
+ Feature: dump database
2
+ As a dbmanager gem user
3
+ I want to dump any of my app databases to a sql file
4
+ so that I can use the file later
5
+
6
+ After adding the gem, when running **rake db:dump**
7
+ you can choose the environment db to dump, and the
8
+ path/filename you want for the dump file. After the
9
+ task has completed you will find the dump in the
10
+ expected location.
11
+
12
+ Scenario: Dump database to user specified file
13
+ Given I go to the dummy rails app folder
14
+
15
+ When I run the task "db:dump" with input "1 tmp/dbmanager_dummy_dev.sql"
16
+ Then a sql dump should be created in "tmp/dbmanager_dummy_dev.sql"
17
+ And the dump file should include expected schema
@@ -0,0 +1,20 @@
1
+ Feature: Dbmanager rake task listed in rake -T
2
+ As a dbmanager gem user
3
+ I want the dbmanager rake tasks to be added to my application
4
+ so that I can execute them.
5
+
6
+ **Dbmanager** adds 2 rake tasks to your application:
7
+
8
+ db:dump that interactively dumps the content of a db
9
+ db:import that interactively import a db into another
10
+
11
+
12
+ Scenario: db:dump task
13
+ Given I go to the dummy rails app folder
14
+ When I execute "rake -T"
15
+ Then I should see "db:dump" among the listed tasks
16
+
17
+ Scenario: db:import task
18
+ Given I go to the dummy rails app folder
19
+ When I execute "rake -T"
20
+ Then I should see "db:import" among the listed tasks
@@ -0,0 +1,121 @@
1
+ ## Dbmanager
2
+
3
+ [![Build Status](https://secure.travis-ci.org/spaghetticode/dbmanager.png)](http://travis-ci.org/spaghetticode/dbmanager)
4
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/spaghetticode/dbmanager)
5
+
6
+ This gem will add some convenience rake tasks that will help you manage database
7
+ dumps and imports. At the moment only the mysql adapter is available.
8
+
9
+ The gems works both on rails 2.x and 3.x applications, but due to rails 2.x
10
+ limitations you have to run a generator, see the usage section
11
+
12
+
13
+ ### Usage
14
+
15
+ Add the gem to your gemfile:
16
+
17
+ ```ruby
18
+ gem 'dbmanager'
19
+ ```
20
+
21
+ If you're on a rails 2.x application you also need to run:
22
+
23
+ ```ruby
24
+ script/generate dbmanager
25
+ ```
26
+ that will copy the gem rake tasks file into the lib/tasks directory.
27
+
28
+
29
+ #### Database Dumps
30
+
31
+ ```ruby
32
+ rake db:dump
33
+ ```
34
+ This rake task will dump the requested db to a file on the local machine.
35
+
36
+ You will be prompted to choose the target dir (defaults to *tmp* in the rails
37
+ root) and the sql file name (sql extension will be added automatically). If the
38
+ file already exists, it will be overwritten.
39
+
40
+
41
+ #### Database Imports
42
+
43
+ ```ruby
44
+ rake db:import
45
+ ```
46
+
47
+ You will be prompted to choose the source and the target environment db, and the
48
+ source db will be imported into the target db.
49
+
50
+ This task will import a source db to a destination db. Tipical use is to import
51
+ the production db into your development one. All environments containing the
52
+ string 'production' in their name are protected by default, which means you cannot
53
+ overwrite them unless you explicitly override this setting in the override file
54
+ (see next section for more info).
55
+
56
+ #### BEWARE
57
+
58
+ **the import process is destructive** so be careful on which environment you
59
+ choose to overwite. I take no responsibility for misuse or bugs in the code ;-)
60
+
61
+
62
+ #### Override database.yml and custom configurations
63
+
64
+ Since some settings may be specific to the server environment (ie. host could
65
+ be a private ip not reachable from elsewhere) you can override the settings in
66
+ database.yml by adding a dbmanager_override.yml file in your rails config dir.
67
+
68
+ Tipical use is to set some environment as protected, or on the other hand allow
69
+ overwriting if it's protected by default (ie. production env).
70
+
71
+ If you want to override the following setting in the database.yml file making
72
+ the database protected from overwriting and changing the host address to a
73
+ public one:
74
+
75
+ ```yaml
76
+ beta:
77
+ host: 192.168.0.1
78
+ ```
79
+ you should put this in config/dbmanager_override.yml:
80
+
81
+ ```yaml
82
+ beta:
83
+ protected: true
84
+ host: 234.234.234.234
85
+ ```
86
+
87
+ Instead, if you want to make the production env writable you should add this to
88
+ the config/dbmanager_override.yml file:
89
+
90
+ ```yaml
91
+ production:
92
+ protected: false
93
+ ```
94
+
95
+ On mysql you can instruct the dumper to ignore certain tables using the
96
+ ignoretables directive:
97
+
98
+ ```yaml
99
+ beta:
100
+ ignoretables:
101
+ - users
102
+ - prods_view
103
+ ```
104
+
105
+
106
+ ## Documentation
107
+
108
+ You can find some more documentation on the workings of the gem on relish:
109
+ https://www.relishapp.com/spaghetticode/dbmanager/docs
110
+
111
+
112
+ ## Tests
113
+
114
+ run rspec tests: ```rake```
115
+
116
+ run cucumber tests: ```cucumber```
117
+
118
+
119
+ ### TODO
120
+
121
+ * Add more db adapters
@@ -0,0 +1,14 @@
1
+ When /^I interactively execute "(.*?)"$/ do |command|
2
+ system command
3
+ end
4
+
5
+ Then /^an? sql dump should be created in "(.*?)"$/ do |path|
6
+ File.file?(path).should be_true
7
+ end
8
+
9
+ Then /^the dump file should include expected schema$/ do
10
+ dump = File.read('tmp/dbmanager_dummy_dev.sql')
11
+ File.open('db/structure.sql').each do |line|
12
+ dump.should include(line) unless line =~ /INSERT INTO schema_migrations/
13
+ end
14
+ end
@@ -0,0 +1,18 @@
1
+ Given /^I go to the dummy rails app folder$/ do
2
+ Dir.chdir File.expand_path(DUMMY_PATH, __FILE__)
3
+ end
4
+
5
+ When /^I execute "(.*?)"$/ do |command|
6
+ @output = `#{command}`
7
+ end
8
+
9
+ Then /^I should see "(.*?)" among the listed tasks$/ do |task_name|
10
+ @output.should include(task_name)
11
+ end
12
+
13
+ When /^I run the task "(.*?)" with input "(.*?)"$/ do |task, params|
14
+ File.open STDIN_STUB, 'w' do |f|
15
+ params.split(' ').each {|param| f.puts param}
16
+ end
17
+ step "I execute \"rake #{task} < #{STDIN_STUB}\""
18
+ end
@@ -0,0 +1,21 @@
1
+ # makes sure the dummy app is properly initialized:
2
+ # bundles required gems
3
+ # drops/creates the databases
4
+ # migrates the development database
5
+ # dumps db structure for reference
6
+ # removes old sql dumps in tmp dir
7
+
8
+ STDIN_STUB = 'STDIN_stub'
9
+ DUMMY_PATH = '../../../spec/dummy'
10
+ Dir.chdir File.expand_path(DUMMY_PATH, __FILE__) do
11
+ system 'bundle install'
12
+ system 'rm -f tmp/*sql'
13
+ %w[
14
+ db:drop:all
15
+ db:create:all
16
+ db:migrate
17
+ db:structure:dump
18
+ ].each do |command|
19
+ system "bundle exec rake #{command}"
20
+ end
21
+ end
File without changes
@@ -1,4 +1,17 @@
1
+ require 'active_support/deprecation'
2
+ require 'active_support/core_ext/module'
3
+ require 'active_support/ordered_hash'
4
+ require 'active_support/core_ext/enumerable'
5
+ require 'active_support/core_ext/object/blank'
6
+
1
7
  module Dbmanager
8
+ autoload :Environment, 'dbmanager/environment'
9
+ autoload :YmlParser, 'dbmanager/yml_parser'
10
+ autoload :Runner, 'dbmanager/runner'
11
+ autoload :Importable, 'dbmanager/importable'
12
+ autoload :Dumpable, 'dbmanager/dumpable'
13
+ autoload :Adapters, 'dbmanager/adapters'
14
+
2
15
  class EnvironmentProtectedError < StandardError
3
16
  def initialize(message=nil)
4
17
  super message || 'sorry the environment is protected from writing'
@@ -36,13 +49,3 @@ module Dbmanager
36
49
  execute(command) or raise CommandError
37
50
  end
38
51
  end
39
-
40
- require 'active_support/deprecation'
41
- require 'active_support/core_ext/module'
42
- require 'active_support/ordered_hash'
43
- require 'active_support/core_ext/enumerable'
44
- require 'active_support/core_ext/object/blank'
45
-
46
- %w[environment yml_parser adapters/mysql adapters/mysql2 runner importable dumpable].each do |string|
47
- require File.expand_path "../dbmanager/#{string}", __FILE__
48
- end
@@ -0,0 +1,6 @@
1
+ module Dbmanager
2
+ module Adapters
3
+ autoload :Mysql, 'dbmanager/adapters/mysql'
4
+ autoload :Mysql2, 'dbmanager/adapters/mysql2'
5
+ end
6
+ end
@@ -52,6 +52,7 @@ module Dbmanager
52
52
 
53
53
  def run
54
54
  Dumper.new(source, tmp_file).run
55
+ Dbmanager.execute! create_db_if_missing_command
55
56
  Dbmanager.execute! import_command
56
57
  ensure
57
58
  remove_tmp_file
@@ -64,6 +65,15 @@ module Dbmanager
64
65
  def remove_tmp_file
65
66
  Dbmanager.execute "rm #{tmp_file}"
66
67
  end
68
+
69
+ def create_db_if_missing_command
70
+ # it is safe to hardcode bundle exec here?
71
+ "#{bundle} rake db:create RAILS_ENV=#{target.name}"
72
+ end
73
+
74
+ def bundle
75
+ Dbmanager.execute('which bundle') ? 'bundle exec' : nil
76
+ end
67
77
  end
68
78
  end
69
79
  end