Class: SerializationHelper::Load
- Inherits:
-
Object
- Object
- SerializationHelper::Load
show all
- Defined in:
- lib/serialization_helper.rb
Class Method Summary
collapse
Class Method Details
.load(io, truncate = true) ⇒ Object
67
68
69
70
71
|
# File 'lib/serialization_helper.rb', line 67
def self.load(io, truncate = true)
ActiveRecord::Base.connection.transaction do
load_documents(io, truncate)
end
end
|
.load_records(table, column_names, records, records_per_page = 1000) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/serialization_helper.rb', line 91
def self.load_records(table, column_names, records, records_per_page=1000)
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)
0.step(records.count-1, records_per_page) do |offset|
all_quoted_values = records[offset, records_per_page].map do |record|
'(' + record.zip(columns).map{|c| ActiveRecord::Base.connection.quote(c.first, c.last)}.join(',') + ')'
end.join(', ')
ActiveRecord::Base.connection.execute("INSERT INTO #{quoted_table_name} (#{quoted_column_names}) VALUES #{all_quoted_values}")
end
end
|
.load_table(table, data, truncate = true) ⇒ Object
81
82
83
84
85
86
87
88
89
|
# File 'lib/serialization_helper.rb', line 81
def self.load_table(table, data, truncate = true)
return if table == 'ar_internal_metadata'
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
107
108
109
110
111
|
# File 'lib/serialization_helper.rb', line 107
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
73
74
75
76
77
78
79
|
# File 'lib/serialization_helper.rb', line 73
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
|