mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-15 19:31:26 +00:00
switch from yaml to json serialization
This commit is contained in:
parent
77882908c4
commit
ec32d7f979
2 changed files with 42 additions and 1 deletions
41
db/migrate/20131223032112_switch_to_json_serialization.rb
Normal file
41
db/migrate/20131223032112_switch_to_json_serialization.rb
Normal file
|
@ -0,0 +1,41 @@
|
|||
class SwitchToJsonSerialization < ActiveRecord::Migration
|
||||
FIELDS = {
|
||||
:agents => [:options, :memory],
|
||||
:events => [:payload]
|
||||
}
|
||||
|
||||
def up
|
||||
puts "This migration will update Agent and Event storage from YAML to JSON. It should work, but please make a backup"
|
||||
puts "before proceeding."
|
||||
print "Continue? (y/n) "
|
||||
STDOUT.flush
|
||||
exit unless STDIN.gets =~ /^y/i
|
||||
|
||||
translate YAML, JSON
|
||||
end
|
||||
|
||||
def down
|
||||
translate JSON, YAML
|
||||
end
|
||||
|
||||
def translate(from, to)
|
||||
FIELDS.each do |table, fields|
|
||||
quoted_table_name = ActiveRecord::Base.connection.quote_table_name(table)
|
||||
fields = fields.map { |f| ActiveRecord::Base.connection.quote_column_name(f) }
|
||||
|
||||
rows = ActiveRecord::Base.connection.select_rows("SELECT id, #{fields.join(", ")} FROM #{quoted_table_name}")
|
||||
rows.each do |row|
|
||||
id, *field_data = row
|
||||
|
||||
yaml_fields = field_data.map { |f| from.load(f) }.map { |f| to.dump(f) }
|
||||
|
||||
update_sql = "UPDATE #{quoted_table_name} SET #{fields.map {|f| "#{f}=?"}.join(", ")} WHERE id = ?"
|
||||
|
||||
sanitized_update_sql = ActiveRecord::Base.send :sanitize_sql_array, [update_sql, *yaml_fields, id]
|
||||
|
||||
ActiveRecord::Base.connection.execute sanitized_update_sql
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
|
@ -8,7 +8,7 @@ module SerializeAndNormalize
|
|||
normalize_name = "normalize_#{column_name}".to_sym
|
||||
validate_name = "validate_#{column_name}".to_sym
|
||||
|
||||
serialize column_name
|
||||
serialize column_name, JSON
|
||||
after_initialize setup_name
|
||||
before_validation normalize_name
|
||||
before_save normalize_name
|
||||
|
|
Loading…
Add table
Reference in a new issue