models-to-sql-rails 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c18b60c7971694627a2e1b273688134030ec21b6
4
+ data.tar.gz: a8f46346393fa8b05ed11d393544f4f37d1d4ce3
5
+ SHA512:
6
+ metadata.gz: 87080b2f35d0923e9d92c79c8a0939456ff03827f125b87a5fdca17a0863f607d1a7ac4fc96517a96e943cf7655729781f2ffabee38258dfb32fb0dc00212459
7
+ data.tar.gz: ccff590f8be39b73dd4c8c2ff391fdaf0f6325d587e3bb0468f6ac0e28420a16450da5cfbda8fa6632ae3b22a1b081f808adc93d74db8c36e1fad58713171c5c
@@ -0,0 +1,99 @@
1
+ Introduction
2
+ ---------------
3
+ Models to SQL Rails is a gem that allows you to dump ActiveRecord models graphs back into SQL or Rails fixtures format. With this gem you can easily convert a model or an array of models to a script like:
4
+ ```sql
5
+ INSERT INTO modelName ('title','description') values ('Awesome Title', 'Wow, amaze description, much doge.');
6
+ ```
7
+ This was just a example, this is a powerful and simple gem that can solve a lot of problems.
8
+
9
+ Usage
10
+ ---------------
11
+ If you want use this gem in your project, you must add the following line to your `gemfile`:
12
+ ```ruby
13
+ gem 'models-to-sql-rails', :git => 'git://github.com/paladini/models-to-sql-rails.git'
14
+ ```
15
+ Then, you must open your terminal (Ctrl+Alt+T) and run the following command on your project root path:
16
+ ```shell
17
+ bundle install
18
+ ```
19
+ Now you're ready to start coding - easy, no?
20
+
21
+ Examples
22
+ --------------
23
+
24
+ Imagine that you have a scenario like that:
25
+
26
+ ```ruby
27
+ require 'models_to_sql'
28
+
29
+ items = []
30
+ for i in 0..10
31
+ items << Item.new(
32
+ :name => generate_random_name(),
33
+ :description => generate_random_description(),
34
+ :url => generate_random_url(),
35
+ :type => "Goods",
36
+ :category => "Electronics",
37
+ :image_url => generate_random_image_url()
38
+ )
39
+ end
40
+
41
+ ###################################
42
+ # #
43
+ # Now let's play with our gem #
44
+ # #
45
+ ###################################
46
+
47
+ items.to_sql_insert
48
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UKlRBjdsyzLtjCL', 'GWpDhkVtkUHkazW', 'CGeCMJjTQGPFUbc', 'Goods', 'Electronics', 'kQWPeRytZAedVnF');
49
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UbOwdYzzFAKckNF', 'nlrMddCWRkYznBH', 'HOdGfDQBHmxSvSW', 'Goods', 'Electronics', 'PtWCKyhxrMFZVJd');
50
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('NvQEUPVgKrPPaQh', 'uTPhqUvVTClimXA', 'HcTtNNoDjncnAIN', 'Goods', 'Electronics', 'LvtpaxTlLWblyar');
51
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('GXSujkenwzvUqcS', 'QRjnvJROfZVSVEj', 'JoGNZMrjmUFlqVM', 'Goods', 'Electronics', 'yVJxiNEWFhQJbKv');
52
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('gtJRWGAFOAvPzMa', 'TOFcwXlFxlLroTo', 'IlNmsRvShgbYace', 'Goods', 'Electronics', 'yLIBqYQjVIBeRcB');
53
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('dUMiXZOzASvMyAv', 'nNuTUCjRsxNNoUU', 'qumOEoEpwGrJcjA', 'Goods', 'Electronics', 'PwbjaGayRZdwlKv');
54
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('heuqrYERTtdUfgS', 'ZYckUtadljKbTBA', 'iFwqJYbqYYgEJNv', 'Goods', 'Electronics', 'BHHOXSFORpDsZBU');
55
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('jFjywdyxTWrWrvC', 'MaJfdgHSENuHkrW', 'hnRJJwsSKHYCnvo', 'Goods', 'Electronics', 'fkKuTpgmvCYEzOK');
56
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('JOIlNUFUVcMtQrP', 'PjLMkYvyFYGlRlr', 'MHucPxQxylqLQia', 'Goods', 'Electronics', 'quWbZUHSXwLvClY');
57
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UxtBOSKroxWtShX', 'ORVtVgfCTonQIZH', 'SjWgwuZBxLLHKCe', 'Goods', 'Electronics', 'drOiSxLZAkFtbCW');
58
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('juAiBiHnFybnjlL', 'ouDFQXnSkRIMQaR', 'hgifUygkuXAWPLp', 'Goods', 'Electronics', 'dKVCzaOCJwmxIim');
59
+
60
+ items[0].to_sql_insert
61
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UKlRBjdsyzLtjCL', 'GWpDhkVtkUHkazW', 'CGeCMJjTQGPFUbc', 'Goods', 'Electronics', 'kQWPeRytZAedVnF');
62
+
63
+ items.each do |item|
64
+ item.to_sql_insert
65
+ end
66
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UKlRBjdsyzLtjCL', 'GWpDhkVtkUHkazW', 'CGeCMJjTQGPFUbc', 'Goods', 'Electronics', 'kQWPeRytZAedVnF');
67
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UbOwdYzzFAKckNF', 'nlrMddCWRkYznBH', 'HOdGfDQBHmxSvSW', 'Goods', 'Electronics', 'PtWCKyhxrMFZVJd');
68
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('NvQEUPVgKrPPaQh', 'uTPhqUvVTClimXA', 'HcTtNNoDjncnAIN', 'Goods', 'Electronics', 'LvtpaxTlLWblyar');
69
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('GXSujkenwzvUqcS', 'QRjnvJROfZVSVEj', 'JoGNZMrjmUFlqVM', 'Goods', 'Electronics', 'yVJxiNEWFhQJbKv');
70
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('gtJRWGAFOAvPzMa', 'TOFcwXlFxlLroTo', 'IlNmsRvShgbYace', 'Goods', 'Electronics', 'yLIBqYQjVIBeRcB');
71
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('dUMiXZOzASvMyAv', 'nNuTUCjRsxNNoUU', 'qumOEoEpwGrJcjA', 'Goods', 'Electronics', 'PwbjaGayRZdwlKv');
72
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('heuqrYERTtdUfgS', 'ZYckUtadljKbTBA', 'iFwqJYbqYYgEJNv', 'Goods', 'Electronics', 'BHHOXSFORpDsZBU');
73
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('jFjywdyxTWrWrvC', 'MaJfdgHSENuHkrW', 'hnRJJwsSKHYCnvo', 'Goods', 'Electronics', 'fkKuTpgmvCYEzOK');
74
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('JOIlNUFUVcMtQrP', 'PjLMkYvyFYGlRlr', 'MHucPxQxylqLQia', 'Goods', 'Electronics', 'quWbZUHSXwLvClY');
75
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('UxtBOSKroxWtShX', 'ORVtVgfCTonQIZH', 'SjWgwuZBxLLHKCe', 'Goods', 'Electronics', 'drOiSxLZAkFtbCW');
76
+ # => INSERT INTO "items" ("name", "description", "url", "type", "category", "image_url") VALUES('juAiBiHnFybnjlL', 'ouDFQXnSkRIMQaR', 'hgifUygkuXAWPLp', 'Goods', 'Electronics', 'dKVCzaOCJwmxIim');
77
+
78
+ ```
79
+
80
+ Simple? If have any issue or problem, [talk to us now](https://github.com/vivrass/models-to-sql/issues).
81
+
82
+ Original documentation
83
+ --------------
84
+
85
+ Original README from this [outdated gem](https://github.com/dsabanin/models-to-sql-rails-plugin). Have some useful informations that we don't have time to describe here.
86
+
87
+ After installation, each AR model has to_sql method that can take following options:
88
+
89
+ :ignore_associations_for - do not dump associations with specified models. (default: empty)
90
+ :ignore_models - do not dump specified models. Array of ruby Class objects is used. (default: empty)
91
+ :ignore_tables - do not dump specified tables (default: empty)
92
+ :debug - debugging mode (default: false)
93
+
94
+
95
+ Contribute
96
+ -------------
97
+
98
+ If you do like to contribute to our project, please feel free to Fork our project or talk with us by [creating a new issue](https://github.com/vivrass/models-to-sql/issues/new). Rails 3.2 port by [Martin Provencher](https://github.com/vivrass) and Rails 4.0 port + documentation by [Fernando Paladini](https://github.com/paladini).
99
+
@@ -0,0 +1,41 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |s|
8
+ s.name = "models_to_sql"
9
+ s.summary = %Q{Dump ActiveRecord models graphs back into SQL or Rails fixtures format}
10
+ s.homepage = "https://github.com/vivrass/canada-provinces-select"
11
+ s.description = "Dump ActiveRecord models graphs back into SQL or Rails fixtures format"
12
+ s.authors = ["PagerDuty"]
13
+
14
+ s.add_runtime_dependency "rails", '>= 1.2'
15
+
16
+ s.files.exclude 'init.rb'
17
+ s.files.exclude 'models_to_sql.gemspec'
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib' << 'test'
26
+ t.pattern = 'test/**/*_test.rb'
27
+ t.verbose = false
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |t|
33
+ t.libs << 'test'
34
+ t.test_files = FileList['test/**/*_test.rb']
35
+ t.verbose = true
36
+ end
37
+ rescue LoadError
38
+ puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+
41
+ task :default => :test
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,121 @@
1
+ class Array
2
+
3
+ def to_sql_insert(*anything)
4
+ bananas = map do |leaf|
5
+ leaf.to_sql_insert(*anything)
6
+ end
7
+ SQLMonkey.sort_out_bananas(bananas)
8
+ end
9
+
10
+ end
11
+
12
+ module ActiveRecord
13
+ class Base
14
+
15
+ def to_sql_insert(baton={})
16
+ TreeClimber.new(self, baton).climb_with(SQLMonkey)
17
+ end
18
+
19
+ def to_sql_file(filepath, options={})
20
+ File.open(filepath, "w") do |fp|
21
+ fp << to_sql_insert(options)
22
+ end
23
+ end
24
+
25
+ end
26
+ end
27
+
28
+ module SQLMonkey
29
+
30
+ extend self
31
+
32
+ def harvest(model, baton)
33
+ bananas = []
34
+ if baton[:add_deletes]
35
+ bananas << "DELETE FROM #{model.class.quoted_table_name} WHERE #{model.connection.quote_column_name(model.class.primary_key)} = #{model.quoted_id}"
36
+ end
37
+ bananas << "INSERT INTO #{model.class.quoted_table_name} " +
38
+ "(#{model.send(:quoted_column_names).join(', ')}) " +
39
+ "VALUES(#{model.send(:attributes_with_quotes).values.join(', ')})"
40
+ bananas
41
+ end
42
+
43
+ def sort_out_bananas(bananas)
44
+ bananas.join(";\n")
45
+ end
46
+
47
+ end
48
+
49
+ module FixtureMonkey
50
+
51
+ extend self
52
+
53
+ def harvest(model, bacon)
54
+ { "#{model.class.to_s.tableize}_#{model.id}" => model.attributes }.to_yaml(:separator => "")
55
+ end
56
+
57
+ def sort_out_bananas(bananas)
58
+ bananas.join("\n")
59
+ end
60
+
61
+ end
62
+
63
+ class TreeClimber
64
+
65
+ attr_reader :model, :baton
66
+
67
+ def initialize(model, baton={})
68
+ @model = model
69
+ @baton = baton
70
+ initialize_baton
71
+ end
72
+
73
+ def initialize_baton
74
+ baton[:ignore_associations_for] ||= []
75
+ baton[:ignore_models] ||= []
76
+ baton[:ignore_tables] ||= []
77
+ baton[:dumped_ids] ||= Hash.new { |hsh,key| hsh[key] = Array.new }
78
+ baton[:current_level] ||= baton[:level].to_i
79
+ baton[:debug] ||= false
80
+ baton[:add_deletes] ||= false
81
+ end
82
+
83
+ def climb_with(monkey)
84
+ return if baton[:ignore_models].include?(model.class)
85
+ table_name = model.class.respond_to?(:table_name) ? model.class.table_name : nil
86
+ return if baton[:ignore_tables].include?(table_name) || table_name.nil?
87
+
88
+ if baton[:dumped_ids][model.class].include?(model.id)
89
+ return
90
+ else
91
+ baton[:dumped_ids][model.class] << model.id
92
+ end
93
+
94
+ bananas = []
95
+ bananas << monkey.harvest(model, baton)
96
+ STDERR << "Getting banana #{model.class}:#{model.id}\n" if baton[:debug]
97
+
98
+ if baton[:level]
99
+ baton[:current_level] -= 1
100
+ end
101
+
102
+ if !baton[:ignore_associations_for].include?(model.class) and baton[:current_level] >= 0
103
+ model.class.reflect_on_all_associations.each do |assoc|
104
+ assoc_value = model.send(assoc.name)
105
+ if assoc_value
106
+ unless assoc_value.is_a? Array
107
+ leafs = [ assoc_value ]
108
+ else
109
+ leafs = assoc_value
110
+ end
111
+ leafs.each do |leaf|
112
+ bananas << TreeClimber.new(leaf, baton).climb_with(monkey)
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ monkey.sort_out_bananas(bananas.flatten.compact)
119
+ end
120
+
121
+ end
@@ -0,0 +1,5 @@
1
+ require 'models_to_sql/array'
2
+ require 'models_to_sql/active_record'
3
+ require 'models_to_sql/climber'
4
+
5
+ ActiveRecord::Base.send(:include, ModelsToSql::ActiveRecord)
@@ -0,0 +1,11 @@
1
+ module ModelsToSql
2
+ module ActiveRecord
3
+
4
+ def to_sql_insert(options = {})
5
+ self.class.cache do
6
+ ModelsToSql::Climber.climb(self, options.delete(:out) || STDOUT, options)
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ class Array
2
+
3
+ def to_sql_insert(*anything)
4
+ map do |leaf|
5
+ leaf.to_sql_insert(*anything)
6
+ end
7
+ end
8
+
9
+ end
@@ -0,0 +1,62 @@
1
+ module ModelsToSql
2
+ class Climber
3
+
4
+ def self.climb(model, output, baton = {})
5
+ baton[:ignore_models] ||= []
6
+ baton[:ignore_tables] ||= []
7
+ baton[:dumped_ids] ||= Hash.new { |hsh,key| hsh[key] = Array.new }
8
+ baton[:debug] ||= false
9
+ baton[:level] ||= 0
10
+
11
+ return if !model.class.respond_to?(:table_name)
12
+ return if baton[:ignore_models].include?(model.class)
13
+ return if baton[:ignore_tables].include?(model.class.table_name)
14
+ return if baton[:dumped_ids][model.class].include?(model.id)
15
+
16
+ baton[:dumped_ids][model.class] << model.id
17
+
18
+ output << sql(model)
19
+ STDERR << "LEVEL: #{baton[:level]} Copying #{model.class}:#{model.id}\n" if baton[:debug]
20
+
21
+ model.class.reflect_on_all_associations.each do |assoc|
22
+ assoc_value = model.send(assoc.name)
23
+ if assoc_value
24
+ unless assoc_value.is_a? Array
25
+ leafs = [ assoc_value ]
26
+ else
27
+ leafs = assoc_value
28
+ end
29
+
30
+ leafs.each do |leaf|
31
+ baton[:level] += 1
32
+ climb(leaf, output, baton)
33
+ baton[:level] -= 1
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def self.sql(model)
40
+
41
+ include ActiveModel::AttributeMethods
42
+ c = model.connection
43
+
44
+ quoted_columns = []
45
+ quoted_values = []
46
+
47
+ if Rails.version.to_i >= 4
48
+ attributes_with_values = model.send(:arel_attributes_with_values_for_create, model.attribute_names)
49
+ else
50
+ attributes_with_values = model.send(:arel_attributes_values, true, true)
51
+ end
52
+
53
+ attributes_with_values.each_pair do |key,value|
54
+ quoted_columns << c.quote_column_name(key.name)
55
+ quoted_values << c.quote(value)
56
+ end
57
+
58
+ "INSERT INTO #{model.class.quoted_table_name} (#{quoted_columns.join(', ')}) VALUES(#{quoted_values.join(', ')});\n"
59
+ end
60
+
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: models-to-sql-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - PagerDuty
8
+ - Fernando Paladini
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '1.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '1.2'
28
+ description: Dump ActiveRecord models graphs back into SQL or Rails fixtures format.
29
+ Easily convert a model or an array of models to SQL insertion.
30
+ email: fnpaladini@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files:
34
+ - README.md
35
+ files:
36
+ - README.md
37
+ - Rakefile
38
+ - VERSION
39
+ - lib/dumper.rb
40
+ - lib/models_to_sql.rb
41
+ - lib/models_to_sql/active_record.rb
42
+ - lib/models_to_sql/array.rb
43
+ - lib/models_to_sql/climber.rb
44
+ homepage: https://github.com/paladini/models-to-sql-rails
45
+ licenses:
46
+ - MIT
47
+ metadata: {}
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubyforge_project:
64
+ rubygems_version: 2.1.11
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Dump ActiveRecord models graphs back into SQL or Rails fixtures format. Rails
68
+ 4 supported.
69
+ test_files: []