Class: SerializationHelper::Load
- Inherits:
-
Object
- Object
- SerializationHelper::Load
- Defined in:
- lib/fidius-common/yamldb/serialization_helper.rb
Direct Known Subclasses
Class Method Summary collapse
- .load(io, truncate = true) ⇒ Object
- .load_records(table, column_names, records) ⇒ Object
- .load_table(table, data, truncate = true) ⇒ Object
- .reset_pk_sequence!(table_name) ⇒ Object
- .truncate_table(table) ⇒ Object
Class Method Details
.load(io, truncate = true) ⇒ Object
115 116 117 118 119 |
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 115 def self.load(io, truncate = true) ActiveRecord::Base.connection.transaction do load_documents(io, truncate) end end |
.load_records(table, column_names, records) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 138 def self.load_records(table, column_names, records) if column_names.nil? return end columns = column_names.map{|cn| ActiveRecord::Base.connection.columns(table).detect{|c| c.name == cn}} quoted_column_names = column_names.map { |column| ActiveRecord::Base.connection.quote_column_name(column) }.join(',') quoted_table_name = SerializationHelper::Utils.quote_table(table) records.each do |record| quoted_values = record.zip(columns).map{|c| ActiveRecord::Base.connection.quote(c.first, c.last)}.join(',') ActiveRecord::Base.connection.execute("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES (#{quoted_values})") end end |
.load_table(table, data, truncate = true) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 129 def self.load_table(table, data, truncate = true) column_names = data['columns'] if truncate truncate_table(table) end load_records(table, column_names, data['records']) reset_pk_sequence!(table) end |
.reset_pk_sequence!(table_name) ⇒ Object
151 152 153 154 155 |
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 151 def self.reset_pk_sequence!(table_name) if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!) ActiveRecord::Base.connection.reset_pk_sequence!(table_name) end end |
.truncate_table(table) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/fidius-common/yamldb/serialization_helper.rb', line 121 def self.truncate_table(table) begin ActiveRecord::Base.connection.execute("TRUNCATE #{SerializationHelper::Utils.quote_table(table)}") rescue Exception ActiveRecord::Base.connection.execute("DELETE FROM #{SerializationHelper::Utils.quote_table(table)}") end end |