Class: SerializationHelper::Load
- Inherits:
-
Object
- Object
- SerializationHelper::Load
show all
- Defined in:
- lib/serialization_helper.rb
Class Method Summary
collapse
Class Method Details
.database_type ⇒ Object
145
146
147
148
149
150
151
152
|
# File 'lib/serialization_helper.rb', line 145
def self.database_type
case ActiveRecord::Base.connection.class.name
when 'ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
:postgresql
when 'ActiveRecord::ConnectionAdapters::Mysql2Adapter', 'ActiveRecord::ConnectionAdapters::MysqlAdapter'
:mysql
end
end
|
.defer_fk_constraints(&block) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/serialization_helper.rb', line 124
def self.defer_fk_constraints(&block)
case database_type
when :postgresql
tables.each do |table|
fk_constraints_on_table = ActiveRecord::Base.connection.foreign_keys(table)
fk_constraints_on_table.each do |fk_constraint|
quoted_table_name = SerializationHelper::Utils.quote_table(table)
ActiveRecord::Base.connection.execute("ALTER TABLE #{quoted_table_name} ALTER CONSTRAINT #{fk_constraint.name} DEFERRABLE INITIALLY IMMEDIATE")
end
end
ActiveRecord::Base.connection.execute("SET CONSTRAINTS ALL DEFERRED")
yield block
when :mysql
ActiveRecord::Base.connection.execute("SET foreign_key_checks = 0")
yield block
ActiveRecord::Base.connection.execute("SET foreign_key_checks = 1")
end
end
|
.load(io, truncate = true) ⇒ Object
67
68
69
70
71
72
73
74
|
# File 'lib/serialization_helper.rb', line 67
def self.load(io, truncate = true)
ActiveRecord::Base.connection.transaction do
defer_fk_constraints do
truncate_all if truncate
load_documents(io)
end
end
end
|
.load_records(table, column_names, records, records_per_page = 1000) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/serialization_helper.rb', line 102
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) ⇒ Object
95
96
97
98
99
100
|
# File 'lib/serialization_helper.rb', line 95
def self.load_table(table, data)
return if table == 'ar_internal_metadata'
column_names = data['columns']
load_records(table, column_names, data['records'])
reset_pk_sequence!(table)
end
|
.reset_pk_sequence!(table_name) ⇒ Object
118
119
120
121
122
|
# File 'lib/serialization_helper.rb', line 118
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
|
.tables ⇒ Object
90
91
92
|
# File 'lib/serialization_helper.rb', line 90
def self.tables
ActiveRecord::Base.connection.tables
end
|
.truncate_all ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/serialization_helper.rb', line 76
def self.truncate_all
quoted_tables = tables.map do |table|
SerializationHelper::Utils.quote_table(table)
end
case database_type
when :postgresql
ActiveRecord::Base.connection.execute("TRUNCATE #{quoted_tables.join(',')} CASCADE")
when :mysql
quoted_tables.each do |quoted_table|
ActiveRecord::Base.connection.execute("TRUNCATE #{quoted_table}")
end
end
end
|