Skip to content

Instantly share code, notes, and snippets.

@MatMoore
Created April 1, 2019 16:26
Show Gist options
  • Save MatMoore/a73c40b5ff1ea24f855ab2e946f98f02 to your computer and use it in GitHub Desktop.
Save MatMoore/a73c40b5ff1ea24f855ab2e946f98f02 to your computer and use it in GitHub Desktop.
Scripts for getting schema exports into liquibase format
#!/usr/bin/env ruby
current_statement_lines = []
primary_keys = []
other_constraints = []
current_statement = :unknown
lines = File.readlines(ARGV.first)
lines.each do |line|
current_statement_lines << line.gsub("\r", '')
if current_statement == :unknown && line =~ /PRIMARY KEY/
current_statement = :primary_key
elsif current_statement == :primary_key && line =~ /;/
primary_keys << current_statement_lines
current_statement_lines = []
current_statement = :unknown
elsif line =~ /;/
other_constraints << current_statement_lines
current_statement_lines = []
current_statement = :unknown
end
end
changeset = 1
primary_keys.each do |pk|
if pk.join =~ /create_constraints/
puts pk.join().gsub(/create_constraints\.\d+/, "create_constraints.#{changeset}")
changeset += 1
else
puts pk.join
end
end
other_constraints.each do |c|
if c.join =~ /create_constraints/
puts c.join().gsub(/create_constraints\.\d+/, "create_constraints.#{changeset}")
changeset += 1
else
puts c.join
end
end
#!/usr/bin/env ruby
ARGV.each do | file|
file_id = file.split('/').last.gsub('.', '_')
contents = File.read(file)
out = "--liquibase formatted sql\r\n\r\n--changeset UserName:#{file_id}.1 splitStatements:false runOnChange:true\r\n#{contents}"
File.open(file, 'w') {|f| f.write(out) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment