Skip to content

Instantly share code, notes, and snippets.

@schneems
Created December 7, 2011 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schneems/1444018 to your computer and use it in GitHub Desktop.
Save schneems/1444018 to your computer and use it in GitHub Desktop.
Convert Yaml_DB fields
data = File.read(File.join(Rails.root, 'db', 'data.yml'))
documents = YAML.load_documents(data)
#=====================================================================
class YamlDb::Document < Hash
class Record < Hash
def initialize(column_names, array)
super nil
column_names.each_with_index do |name, index|
self[name] = array[index]
end
end
end
# YamlDb::Document::Record.new(["id", "price"], [1, 2]) # => {"id" => 1, "price" => 2}
def initialize(hash)
super nil; hash.each { |k, v| self[k] = v}
end
def table_name
self.keys.first
end
def columns=(value)
values.first["columns"] = value
end
def columns
values.first["columns"]
end
def add_record(record)
record_array = columns.map do |column|
record[column]
end
values.first["records"] << record_array
end
def raw_records
values.first["records"]
end
def records
raw_records.map do |record|
Record.new(self.columns, record)
end
end
def to_yaml
result = {table_name => {'columns' => columns , 'records' => raw_records}}.to_yaml
end
end
orig_doc = YamlDb::Document.new(documents.last)
class YamlDb::Process
@tables_to_keep = []
@select_from_table = {}
def self.keep_table(table_name)
@tables_to_keep << table_name unless @tables_to_keep.include? table_name
end
def self.select_from(table_name, &block)
keep_table(table_name)
@select_from_table[table_name] = block
end
def self.all(table_name)
keep_table(table_name)
end
def self.convert(documents)
raise "nothing to be processed" if @select_from_table.nil?
documents.collect! do |document|
document = YamlDb::Document.new(document)
if @select_from_table[document.table_name]
document = transform(document, &@select_from_table[document.table_name])
end
document
end
documents.delete_if {|document| !@tables_to_keep.include? document.table_name}
documents
end
def self.transform(document_from, document_to = nil, &blk )
document_to ||= YamlDb::Document.new(document_from.table_name => {"columns" => [], "records" => []})
document_from.records.each do |from_record|
record = blk.call(from_record)
document_to.columns = record.keys if document_to.columns.empty?
document_to.add_record(record)
end
document_to
end
def self.export(documents, path = File.join(Rails.root, 'db', 'export_data.yml'))
documents = convert(documents)
YamlDb::Export.run(documents, path)
end
end
module YamlDb::Export
def self.run(documents, path)
File.open(path, 'w') {} # creates/clears document
documents.each do |document|
File.open(path, 'a') {|f| f.write(document.to_yaml) } # append
File.open(path, 'a') {|f| f.write("\n")}
end
end
end
# ============================================================
YamlDb::Process.select_from("payments") do |payment|
{
:id => payment['id']
:amount => payment['amount']
:transaction_id => payment['transaction_id']
:user_id => payment['user_id']
:course_id => payment['course_id']
:created_at => payment['created_at']
:updated_at => payment['updated_at']
}
end
YamlDb::Process.all("roles")
YamlDb::Process.select_from("courses") do |record|
{:id => record['id'],
:title => record['topic'],
:description => record['body'],
:price => record['price'],
:max_seats => record['maxStudent'],
:date => record['start_time'], # change data type to dateTime
:place_name => record['location'],
:created_at => record['created_at'],
:updated_at => record['updated_at'],
:min_seats => record['minStudent'],
:time_range => "#{DateTime.parse(record['start_time']).strftime('%I:%m')} - #{DateTime.parse(record['end_time']).strftime('%I:%m')}",
:status => record['classStatus'],
:address => record['locationAddy'],
:public => record['publicLocation']}
end
YamlDb::Process.select_from("users") do |user|
{ :id => user['id'],
:email => user['email'],
:name => "#{user['firstname']} #{user['last_name']}",
:created_at => user['created_at'],
:updated_at => user['updated_at'],
:location => user['city'],
:admin => user['admin'],
:bio => user['bio'],
:legacy_password_hash => user['password_hash'],
:legacy_password_salt => user['password_salt']}
end
YamlDb::Process.select_from("payments") do |payment|
{
:id => payment['id'],
:amount => payment['amount'],
:transaction_id => payment['transaction_id'],
:user_id => payment['user_id'],
:course_id => payment['course_id'],
:created_at => payment['created_at'],
:updated_at => payment['updated_at']
}
end
# ==========================================================
YamlDb::Process.export(documents)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment