Skip to content

Instantly share code, notes, and snippets.

@reedlaw
Created November 30, 2020 18:52
Show Gist options
  • Save reedlaw/25e768d0cb853fc40bbdebfaeabaf360 to your computer and use it in GitHub Desktop.
Save reedlaw/25e768d0cb853fc40bbdebfaeabaf360 to your computer and use it in GitHub Desktop.
Downgrade PostgreSQL 10 syntax for backwards compatibility
#!/usr/bin/env ruby
# typed: ignore
# Makes PostgreSQL > 9 syntax compatible with < 10
sql = File.open('./db/structure.sql', 'r+')
inside_create_sequence = false
out = ''
sql.each_line do |line|
if inside_create_sequence && line.strip == 'AS integer'
next
elsif line.strip == 'SET xmloption = content;'
next
elsif line.strip == 'SET default_table_access_method = heap;'
out += "SET default_with_oids = false;\n"
elsif line.include?('EXECUTE FUNCTION')
out += line.gsub('EXECUTE FUNCTION', 'EXECUTE PROCEDURE')
else
out += line
end
inside_create_sequence = line.start_with?('CREATE SEQUENCE')
end
File.truncate('./db/structure.sql', 0)
sql.seek(0, IO::SEEK_SET)
sql.write(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment