dumpable 0.2.6 → 0.2.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +36 -5
- data/VERSION +1 -1
- data/dumpable.gemspec +2 -2
- data/lib/dumpable/dumper.rb +4 -1
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -1,8 +1,31 @@
|
|
1
1
|
= dumpable
|
2
2
|
|
3
|
-
|
3
|
+
The situation is all too common. There is an error in production that you want to reproduce in your local
|
4
|
+
development environment. Only problem is that the production environment has database data that you don't have locally.
|
5
|
+
|
6
|
+
So then you generally only have one option: dump your entire production database, or a subset of your production
|
7
|
+
database to a sql file and import the dump into your local database.
|
8
|
+
|
9
|
+
What if time is of the essense, and the production database is huge? What if you could dump only the
|
10
|
+
specific records and their associations that you need to reproduce the record causing problems in your local database.
|
11
|
+
|
12
|
+
Dumpable allows you to do just that. You can dump single rows (or a collection of rows),
|
13
|
+
and choose which associations to dump with those rows.
|
14
|
+
|
15
|
+
== Installation
|
16
|
+
Add the following to your Gemfile:
|
17
|
+
gem 'dumpable'
|
4
18
|
|
5
19
|
== Usage
|
20
|
+
The dumpable gem will automatically add the class method #dumpable and the instance method #dump into all your
|
21
|
+
ActiveRecord models.
|
22
|
+
|
23
|
+
By default, calling dump on any record will output the sql insert statement necessary to create that record.
|
24
|
+
It will not generate the insert statements for any of the record's associations. These can either be specified
|
25
|
+
class level by calling #dumpable with the set of assocations to dump or a run-time in your call to #dump.
|
26
|
+
|
27
|
+
Both cases will be demonstrated below.
|
28
|
+
|
6
29
|
class User < ActiveRecord::Base
|
7
30
|
has_many :posts
|
8
31
|
has_many :roles
|
@@ -26,21 +49,29 @@ Then, from the console, you can do something like:
|
|
26
49
|
User.first.dump
|
27
50
|
This will spit out all the MySQL insertion code for the user, it's roles, it's posts, and each post's comments
|
28
51
|
|
29
|
-
If you want
|
52
|
+
If you want each record to pad the ids generated in the insertion code, this can be done as follows:
|
30
53
|
User.first.dump(:id_padding => 1000)
|
31
54
|
This will ensure that the id of the above user is padded by 1000, so a user id of 1 will generate an insert statement with an id of 1001.
|
32
55
|
|
33
|
-
You may wonder why the id is included as part of the insert statement. This is to ensure that all associations maintain their
|
34
|
-
For example, if TODO
|
56
|
+
You may wonder why the id is included as part of the insert statement. This is to ensure that all associations maintain their relationship to one another.
|
35
57
|
|
36
58
|
All ActiveRecord models are dumpable automatically, so you could also call:
|
37
59
|
Post.dump
|
60
|
+
# or
|
61
|
+
Post.dump(:dumps => :comments)
|
38
62
|
|
39
63
|
You can also dump all the entries in the table by calling:
|
40
64
|
Post.dump
|
41
|
-
# or
|
65
|
+
# or with conditions
|
42
66
|
Post.where(:published => true).dump
|
43
67
|
|
68
|
+
== Dumping to a sql file
|
69
|
+
User.dump(:file => PATH_TO_FILE)
|
70
|
+
|
71
|
+
== Dumping multiple different record types
|
72
|
+
Dumpable.dump(Post, Comment.limit(10), Role.where(:active => true)) # will output to console
|
73
|
+
Dumpable.dump(Post, Comment.limit(10), Role.where(:active => true), :file => "dump.sql") # will output to file
|
74
|
+
|
44
75
|
== Gotchas
|
45
76
|
At the moment, Dumpable will not work on any complex relationships. This includes has_many :through, and has_and_belongs_to_many.
|
46
77
|
It also will not work on models that have a different primary key than id or on models with complex keys.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.7
|
data/dumpable.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "dumpable"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.7"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Andrew Hunter"]
|
12
|
-
s.date = "
|
12
|
+
s.date = "2014-01-17"
|
13
13
|
s.description = "Generate the SQL to insert a single record and all of its dependencies"
|
14
14
|
s.email = "andrew.hunter@livingsocial.com"
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/dumpable/dumper.rb
CHANGED
@@ -63,10 +63,13 @@ module Dumpable
|
|
63
63
|
|
64
64
|
# http://invisipunk.blogspot.com/2008/04/activerecord-raw-insertupdate.html
|
65
65
|
def generate_insert_query(object)
|
66
|
+
skip_columns = Array(@options[:skip_columns] || (object.class.respond_to?(:dumpable_options) && object.class.dumpable_options[:skip_columns])).map(&:to_s)
|
66
67
|
cloned_attributes = object.attributes.clone
|
67
68
|
return nil unless cloned_attributes["id"].present?
|
68
69
|
cloned_attributes["id"] += @id_padding
|
69
|
-
key_values = cloned_attributes.collect
|
70
|
+
key_values = cloned_attributes.collect do |key,value|
|
71
|
+
[key, dump_value_string(value)] unless skip_columns.include?(key.to_s)
|
72
|
+
end.compact
|
70
73
|
keys = key_values.collect{ |item| "`#{item[0]}`" }.join(", ")
|
71
74
|
values = key_values.collect{ |item| item[1].to_s }.join(", ")
|
72
75
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dumpable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2014-01-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -145,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
145
145
|
version: '0'
|
146
146
|
segments:
|
147
147
|
- 0
|
148
|
-
hash: -
|
148
|
+
hash: -4228121051279904829
|
149
149
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
150
150
|
none: false
|
151
151
|
requirements:
|