Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save douglasnomizo/c9bb9904b331470f4cb0 to your computer and use it in GitHub Desktop.
Save douglasnomizo/c9bb9904b331470f4cb0 to your computer and use it in GitHub Desktop.
require 'nokogiri'
class String
def underscore
self.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
def foreign_key_migration_name(foreign_key)
"add_#{foreign_key.attribute('referencedTableName')}_reference_to_#{foreign_key.attribute('baseTableName')}"
end
def create_table_migration_name(create_table)
"create_table_#{create_table.attribute('tableName')}"
end
def create_index_migration_name(create_index)
"add_#{create_index.children[1].attribute('name')}_index_for_#{create_index.attribute('tableName')}"
end
def add_unique_constraint_name(unique_constraint)
"add_unique_constraint_#{unique_constraint.attribute('columnNames')}_to_#{unique_constraint.attribute('tableName')}"
end
def primary_key_name(primary_key)
column_names = primary_key.attribute('columnNames').to_s.delete(' ').gsub(',', '_')
"add_primary_key_#{column_names}_to_#{primary_key.attribute('tableName')}"
end
doc = Nokogiri::XML(File.open("liquibase_change_log.xml"))
header = File.open('header.xml', 'r').read
changeSets = doc.children[0].search('changeSet')
changeSets.each_with_index do |item, index|
action = item.children[1].name
element = item.children[1]
if (action == 'createTable')
name = create_table_migration_name(element)
elsif (action == 'createIndex')
name = create_index_migration_name(element)
elsif (action == 'addForeignKeyConstraint')
name = foreign_key_migration_name(element)
elsif (action == 'addUniqueConstraint')
name = add_unique_constraint_name(element)
elsif (action == 'addPrimaryKey')
name = primary_key_name(element)
end
filename = "migrations/#{(index+1).to_s.rjust(4, '0')}_#{name}.xml"
File.open(filename, 'w') { |file| file.write("#{header}\n#{item.to_xml}") }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment