application_seeds 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ v0.6.0, 2014-03-06
2
+ ------------------
3
+
4
+ * Added method that will allow the user to defer the enforcement of
5
+ referential integrity checks while a block of code is being
6
+ executed.
7
+
8
+
1
9
  v0.5.0, 2014-02-28
2
10
  ------------------
3
11
 
data/README.md CHANGED
@@ -363,6 +363,16 @@ ApplicationSeeds.config_value(:num_departments)
363
363
  => 3
364
364
  ```
365
365
 
366
+ And use them in your seed files:
367
+
368
+ ```ruby
369
+ <% ApplicationSeeds.config_value(:num_companies).times do |x| %>
370
+ company_<%= x %>:
371
+ name: Company_<%= x %>
372
+ <% end %>
373
+ ```
374
+
375
+
366
376
  ### Merging config value files
367
377
 
368
378
  If you are using nested datasets, then all of the appropriate
@@ -554,6 +564,19 @@ are unable to insert new data into the databse after your dataset has
554
564
  been imported, then this should correct them.
555
565
 
556
566
 
567
+ ### Defer referential integrity checks
568
+
569
+ ```ruby
570
+ ApplicationSeeds.defer_referential_integrity_checks do
571
+ # Process some seed data
572
+ end
573
+ ```
574
+
575
+ This method will defer the enforcement of foreign key contraints while
576
+ the block of code is being executed. This is useful when creating
577
+ chunks of seed data that have are dependent on each other's existance.
578
+
579
+
557
580
  ### Fetch data from the `_config.yml` files
558
581
 
559
582
  ```ruby
@@ -158,6 +158,15 @@ module ApplicationSeeds
158
158
  end
159
159
  end
160
160
 
161
+ #
162
+ # Defer the enforcement of foreign key constraints while the block is being executed.
163
+ #
164
+ def defer_referential_integrity_checks
165
+ Database.without_foreign_keys do
166
+ yield
167
+ end
168
+ end
169
+
161
170
  private
162
171
 
163
172
  def dataset_path(dataset)
@@ -1,7 +1,7 @@
1
1
  module ApplicationSeeds
2
2
  class Database
3
-
4
3
  class << self
4
+
5
5
  def connection
6
6
  return @connection unless @connection.nil?
7
7
 
@@ -21,8 +21,43 @@ module ApplicationSeeds
21
21
  connection.exec('DROP TABLE IF EXISTS application_seeds;')
22
22
  connection.exec('CREATE TABLE application_seeds (dataset varchar(255));')
23
23
  end
24
- end
25
24
 
25
+ def without_foreign_keys
26
+ drop_foreign_keys_sql = generate_drop_foreign_keys_sql
27
+ create_foreign_keys_sql = generate_create_foreign_keys_sql
28
+
29
+ connection.exec(drop_foreign_keys_sql)
30
+ yield
31
+ connection.exec(create_foreign_keys_sql)
32
+ end
33
+
34
+ private
35
+
36
+ def generate_drop_foreign_keys_sql
37
+ result = connection.exec <<-SQL
38
+ SELECT 'ALTER TABLE '||nspname||'.'||relname||' DROP CONSTRAINT '||conname||';'
39
+ FROM pg_constraint
40
+ INNER JOIN pg_class ON conrelid=pg_class.oid
41
+ INNER JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace
42
+ WHERE contype='f'
43
+ ORDER BY CASE WHEN contype='f' THEN 0 ELSE 1 END,contype,nspname,relname,conname
44
+ SQL
45
+ result.values.join
46
+ end
47
+
48
+ def generate_create_foreign_keys_sql
49
+ result = connection.exec <<-SQL
50
+ SELECT 'ALTER TABLE '||nspname||'.'||relname||' ADD CONSTRAINT '||conname||' '|| pg_get_constraintdef(pg_constraint.oid)||';'
51
+ FROM pg_constraint
52
+ INNER JOIN pg_class ON conrelid=pg_class.oid
53
+ INNER JOIN pg_namespace ON pg_namespace.oid=pg_class.relnamespace
54
+ WHERE contype='f'
55
+ ORDER BY CASE WHEN contype='f' THEN 0 ELSE 1 END DESC,contype DESC,nspname DESC,relname DESC,conname DESC;
56
+ SQL
57
+ result.values.join
58
+ end
59
+
60
+ end
26
61
  end
27
62
  end
28
63
 
@@ -1,3 +1,3 @@
1
1
  module ApplicationSeeds
2
- VERSION = "0.5.0"
2
+ VERSION = "0.6.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: application_seeds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
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: 2014-02-28 00:00:00.000000000 Z
12
+ date: 2014-03-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -123,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  segments:
125
125
  - 0
126
- hash: 632668571274566106
126
+ hash: -1007490013987030043
127
127
  required_rubygems_version: !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements:
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  segments:
134
134
  - 0
135
- hash: 632668571274566106
135
+ hash: -1007490013987030043
136
136
  requirements: []
137
137
  rubyforge_project:
138
138
  rubygems_version: 1.8.23