yaml_db_with_schema_tables 0.3.2 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56086fd52fca521d5a61ff835253a623ae9ee680
4
- data.tar.gz: 577d01ac73bc69863cf41cf40051bf9d40d378be
3
+ metadata.gz: 076c6682db8052bfab667c3d27ddd35e8a6daa8f
4
+ data.tar.gz: 53255b3a65cec12c7e71657e9773f73ed56dd1db
5
5
  SHA512:
6
- metadata.gz: 80a7996018fb8202a8aa083e329482a202f6fb8e418db6ff7db957ee6bba9204cb7c79f5fb98ed2d4afcd3fa1c95743fc401648ff50702fc7c1657577ce01992
7
- data.tar.gz: 7124cc5ade5ac68f92ee8e54bdc1e9005db4c0f81a9dd8087bdfedca36b77dceb9666db19f0694f2e7e98ec99996c3f8e23c53ddb13e1da41987d64b5e9bd4e3
6
+ metadata.gz: 7eef2c5835ba0ef9aadc57c96e520630adb2d50ca31cc762ff5d575f75c51e263aa8a23a5123e4009156e02e6166542bd64773b9bdcbaadc32af4e6544a721b6
7
+ data.tar.gz: 57417ce8ffe02b7641ec3dd38b4b5e7346a10d40068fc02d4d94ffa263073a1efd0c6776f7e99e2e1499b4a150e4fc443c35355f876afd00aa67716295dd4051
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
data/lib/csv_db.rb CHANGED
@@ -16,7 +16,7 @@ module CsvDb
16
16
  end
17
17
 
18
18
  class Load < SerializationHelper::Load
19
- def self.load_documents(io, truncate = true)
19
+ def self.load_documents(io)
20
20
  tables = {}
21
21
  curr_table = nil
22
22
  io.each do |line|
@@ -34,7 +34,7 @@ module CsvDb
34
34
  end
35
35
 
36
36
  tables.each_pair do |table_name, contents|
37
- load_table(table_name, contents, truncate)
37
+ load_table(table_name, contents)
38
38
  end
39
39
  end
40
40
  end
@@ -72,7 +72,7 @@ module CsvDb
72
72
  end
73
73
  end
74
74
  end
75
-
75
+
76
76
  end
77
77
 
78
78
 
data/lib/json_db.rb CHANGED
@@ -62,11 +62,11 @@ module JsonDb
62
62
  end
63
63
 
64
64
  class Load < SerializationHelper::Load
65
- def self.load_documents(io, truncate = true)
65
+ def self.load_documents(io)
66
66
  JSON.load(io).tap do |json|
67
67
  json.keys.each do |table_name|
68
68
  next if json[table_name].nil?
69
- load_table(table_name, json[table_name], truncate)
69
+ load_table(table_name, json[table_name])
70
70
  end
71
71
  end
72
72
  end
@@ -62,28 +62,39 @@ module SerializationHelper
62
62
  ActiveRecord::Base.logger = @@old_logger
63
63
  end
64
64
  end
65
-
65
+
66
66
  class Load
67
67
  def self.load(io, truncate = true)
68
68
  ActiveRecord::Base.connection.transaction do
69
- load_documents(io, truncate)
69
+ defer_fk_constraints do
70
+ truncate_all if truncate
71
+ load_documents(io)
72
+ end
70
73
  end
71
74
  end
72
75
 
73
- def self.truncate_table(table)
74
- begin
75
- ActiveRecord::Base.connection.execute("TRUNCATE #{SerializationHelper::Utils.quote_table(table)}")
76
- rescue Exception
77
- ActiveRecord::Base.connection.execute("DELETE FROM #{SerializationHelper::Utils.quote_table(table)}")
76
+ def self.truncate_all
77
+ quoted_tables = tables.map do |table|
78
+ SerializationHelper::Utils.quote_table(table)
79
+ end
80
+ case database_type
81
+ when :postgresql
82
+ ActiveRecord::Base.connection.execute("TRUNCATE #{quoted_tables.join(',')} CASCADE")
83
+ when :mysql
84
+ quoted_tables.each do |quoted_table|
85
+ ActiveRecord::Base.connection.execute("TRUNCATE #{quoted_table}")
86
+ end
78
87
  end
79
88
  end
80
89
 
81
- def self.load_table(table, data, truncate = true)
90
+ def self.tables
91
+ ActiveRecord::Base.connection.tables
92
+ end
93
+
94
+
95
+ def self.load_table(table, data)
82
96
  return if table == 'ar_internal_metadata'
83
97
  column_names = data['columns']
84
- if truncate
85
- truncate_table(table)
86
- end
87
98
  load_records(table, column_names, data['records'])
88
99
  reset_pk_sequence!(table)
89
100
  end
@@ -95,7 +106,7 @@ module SerializationHelper
95
106
  columns = column_names.map{|cn| ActiveRecord::Base.connection.columns(table).detect{|c| c.name == cn}}
96
107
  quoted_column_names = column_names.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',')
97
108
  quoted_table_name = SerializationHelper::Utils.quote_table(table)
98
-
109
+
99
110
  0.step(records.count-1, records_per_page) do |offset|
100
111
  all_quoted_values = records[offset, records_per_page].map do |record|
101
112
  '(' + record.zip(columns).map{|c| ActiveRecord::Base.connection.quote(c.first, c.last)}.join(',') + ')'
@@ -108,9 +119,39 @@ module SerializationHelper
108
119
  if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
109
120
  ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
110
121
  end
111
- end
122
+ end
112
123
 
113
-
124
+ def self.defer_fk_constraints(&block)
125
+ case database_type
126
+ when :postgresql
127
+ # make all fk constraints deferrable
128
+ fk_constraints = []
129
+ tables.each do |table|
130
+ fk_constraints_on_table = ActiveRecord::Base.connection.foreign_keys(table)
131
+ fk_constraints_on_table.each do |fk_constraint|
132
+ quoted_table_name = SerializationHelper::Utils.quote_table(table)
133
+ ActiveRecord::Base.connection.execute("ALTER TABLE #{quoted_table_name} ALTER CONSTRAINT #{fk_constraint.name} DEFERRABLE INITIALLY IMMEDIATE")
134
+ end
135
+ fk_constraints += fk_constraints_on_table
136
+ end
137
+ # defer all fk constraints
138
+ ActiveRecord::Base.connection.execute("SET CONSTRAINTS #{fk_constraints.collect(&:name).join(',')} DEFERRED")
139
+ yield block
140
+ when :mysql
141
+ ActiveRecord::Base.connection.execute("SET foreign_key_checks = 0")
142
+ yield block
143
+ ActiveRecord::Base.connection.execute("SET foreign_key_checks = 1")
144
+ end
145
+ end
146
+
147
+ def self.database_type
148
+ case ActiveRecord::Base.connection.class.name
149
+ when 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
150
+ :postgresql
151
+ when 'ActiveRecord::ConnectionAdapters::Mysql2Adapter', 'ActiveRecord::ConnectionAdapters::MysqlAdapter'
152
+ :mysql
153
+ end
154
+ end
114
155
  end
115
156
 
116
157
  module Utils
@@ -47,6 +47,14 @@ namespace :db do
47
47
  desc "Load gzipped contents from stdin"
48
48
  task :load_gzipped_stdin => :environment do
49
49
  gz = Zlib::GzipReader.new($stdin.dup)
50
+ # workaround for ruby < 2.3.0
51
+ unless gz.respond_to? :external_encoding
52
+ class << gz
53
+ def external_encoding
54
+ Encoding::UTF_8
55
+ end
56
+ end
57
+ end
50
58
  SerializationHelper::Base.new(helper).load_from_io gz
51
59
  gz.close
52
60
  end
data/lib/yaml_db.rb CHANGED
@@ -56,11 +56,11 @@ module YamlDb
56
56
  end
57
57
 
58
58
  class Load < SerializationHelper::Load
59
- def self.load_documents(io, truncate = true)
59
+ def self.load_documents(io)
60
60
  YAML.load_documents(io) do |ydoc|
61
61
  ydoc.keys.each do |table_name|
62
62
  next if ydoc[table_name].nil?
63
- load_table(table_name, ydoc[table_name], truncate)
63
+ load_table(table_name, ydoc[table_name])
64
64
  end
65
65
  end
66
66
  end
data/yaml_db.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "yaml_db_with_schema_tables"
8
- s.version = "0.3.2"
8
+ s.version = "0.3.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Adam Wiggins", "Orion Henry", "Martin Honermeyer"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_db_with_schema_tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wiggins