Created
April 1, 2019 16:26
-
-
Save MatMoore/a73c40b5ff1ea24f855ab2e946f98f02 to your computer and use it in GitHub Desktop.
Scripts for getting schema exports into liquibase format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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