data_dumper 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/README.textile ADDED
@@ -0,0 +1,79 @@
1
+ h1. DataDumper
2
+
3
+ h2. Installation
4
+
5
+ # Gemfile
6
+ gem "data_dumper", :group => [:development, :test]
7
+
8
+ h2. Usage
9
+
10
+ You're knee deep in a debugger session, and you can't understand why something's wrong. You wish you could fire up your application against the test database, but sadly, the process which is running the tests is within a transaction, and thus the actual data is opaque. What can you do?
11
+
12
+ # Somewhere deep in your tests
13
+ test "the frobble touches the widget" do
14
+ debugger
15
+ assert_equal 42, frobble.widget_id
16
+ end
17
+
18
+ You've been on this <code>assert_equal</code> call for the past hour wondering...:
19
+
20
+ [814, 823] in test/unit/widget_test.rb
21
+ 814 frobble.save!
22
+ 815 end
23
+ 816
24
+ 817 test "the frobble touches the widget" do
25
+ 818 debugger
26
+ => 819 assert_equal 42, frobble.widget_id
27
+ 820 end
28
+ 821
29
+ 822 test "the widget touched the frobble in turn" do
30
+ 823 assert widget.touched_by_frobble?
31
+ test/unit/widget_test.rb:819
32
+ => 819 assert_equal 42, frobble.widget_id
33
+ (rdb:112)
34
+
35
+ Luckily, you have the DataDumper gem lying around on your machine (pre-declared in your Gemfile!):
36
+
37
+ (rdb:112) require "data_dumper"
38
+ (rdb:113) File.mkdir(Rails.root + "dump")
39
+ (rdb:114) DataDumper.dump(Rails.root + "dump")
40
+
41
+ Then, finish your failing tests, and from the trusty command line:
42
+
43
+ $ rails console
44
+ > require "data_dumper"
45
+ > DataDumper.load(Rails.root + "dump")
46
+ > exit
47
+
48
+ $ rails server
49
+
50
+ Any and all data from your test database will be loaded in your development environment. You can now explore your model with your trusty application, to find out what's really going on.
51
+
52
+ h2. Caveats
53
+
54
+ This tool is known to work with Rails 2.3.5, PostgreSQL 8.4 and REE 1.8.7 2011.03. Anything else is up for you to test.
55
+
56
+ h2. LICENSE
57
+
58
+ (The MIT License)
59
+
60
+ Copyright (c) 2011 François Beausoleil (francois@teksol.info)
61
+
62
+ Permission is hereby granted, free of charge, to any person obtaining
63
+ a copy of this software and associated documentation files (the
64
+ 'Software'), to deal in the Software without restriction, including
65
+ without limitation the rights to use, copy, modify, merge, publish,
66
+ distribute, sublicense, and/or sell copies of the Software, and to
67
+ permit persons to whom the Software is furnished to do so, subject to
68
+ the following conditions:
69
+
70
+ The above copyright notice and this permission notice shall be
71
+ included in all copies or substantial portions of the Software.
72
+
73
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
74
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
75
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
76
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
77
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
78
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
79
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "data_dumper/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "data_dumper"
7
+ s.version = DataDumper::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["François Beausoleil"]
10
+ s.email = ["francois@teksol.info"]
11
+ s.homepage = "https://github.com/francois/data_dumper"
12
+ s.summary = %q{Quick and dirty tool to dump data from an active test session to a development server}
13
+ s.description = %q{From within your tests, if you can't understand what's going on, maybe you need to dump your data to your development database and use it from there?}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'activerecord', '>= 2.3'
21
+ end
@@ -0,0 +1,49 @@
1
+ module DataDumper
2
+ def self.dump(path)
3
+ ActiveRecord::Base.connection.tables.each do |table_name|
4
+ quoted_output_filename = ActiveRecord::Base.connection.quote(File.expand_path(File.join(path, table_name + ".sql")))
5
+ ActiveRecord::Base.connection.execute(<<-EOSQL)
6
+ COPY #{table_name} TO #{quoted_output_filename} WITH CSV
7
+ EOSQL
8
+ end
9
+ end
10
+
11
+ def self.load(path)
12
+ conn = ActiveRecord::Base.connection
13
+ ActiveRecord::Base.transaction do
14
+ files = Dir[File.join(path, "*.sql")]
15
+
16
+ puts "Disabling triggers in all tables to be loaded"
17
+ files.each do |filename|
18
+ table_name = File.basename(filename, ".sql")
19
+ conn.execute "ALTER TABLE #{table_name} DISABLE TRIGGER ALL"
20
+ end
21
+
22
+ puts "Truncating existing data"
23
+ table_names = files.map do |filename|
24
+ File.basename(filename, ".sql")
25
+ end
26
+ conn.execute "TRUNCATE #{table_names.join(", ")} CASCADE"
27
+
28
+ puts "Loading new data from #{path.inspect}..."
29
+ files.each do |filename|
30
+ # Has to be absolute, since it's the server process itself which will load the data
31
+ quoted_filename = conn.quote(File.expand_path(filename))
32
+ table_name = File.basename(filename, ".sql")
33
+
34
+ puts table_name
35
+ conn.execute "COPY #{table_name} FROM #{quoted_filename} CSV"
36
+ end
37
+
38
+ puts "Enabling triggers in all tables"
39
+ files.each do |filename|
40
+ table_name = File.basename(filename, ".sql")
41
+ conn.execute "ALTER TABLE #{table_name} ENABLE TRIGGER ALL"
42
+ end
43
+
44
+ end
45
+
46
+ puts "Analyzing the data, for best performance"
47
+ conn.execute "ANALYZE"
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module DataDumper
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_dumper
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - "Fran\xC3\xA7ois Beausoleil"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-31 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activerecord
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 2
32
+ - 3
33
+ version: "2.3"
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description: From within your tests, if you can't understand what's going on, maybe you need to dump your data to your development database and use it from there?
37
+ email:
38
+ - francois@teksol.info
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - README.textile
49
+ - data_dumper.gemspec
50
+ - lib/data_dumper.rb
51
+ - lib/data_dumper/version.rb
52
+ has_rdoc: true
53
+ homepage: https://github.com/francois/data_dumper
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options: []
58
+
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.6.2
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Quick and dirty tool to dump data from an active test session to a development server
86
+ test_files: []
87
+