Sha256: c5f48a5b4303757624a895ec35c8a93535db51c12a4f935cd28b5a77805e7f5f
Contents?: true
Size: 1.74 KB
Versions: 1
Compression:
Stored size: 1.74 KB
Contents
module PgMigrate class SqlReader def initialize end # read in a migration file, # converting lines of text into SQL statements that can be executed with our database connection def load_migration(migration_path) statements = [] current_statement = "" migration_lines = IO.readlines(migration_path) migration_lines.each_with_index do |line, index| line_stripped = line.strip if line_stripped.empty? || line_stripped.start_with?('--') # it's a comment; ignore elsif line_stripped.start_with?("\\") # it's a psql command; ignore else current_statement += " " + line_stripped; if line_stripped.end_with?(";") if current_statement =~ /^\s*CREATE\s+(OR\s+REPLACE\s+)?FUNCTION/i || current_statement =~ /^\s*DO\s+/i # if we are in a function, a ';' isn't enough to end. We need to see if the last word was one of # pltcl, plperl, plpgsql, plpythonu, sql # you can extend languages in postgresql; detecting these isn't supported yet. # we also detect anonymous functions (DO) and their ending sequence. if current_statement =~ /(plpgsql|plperl|plpythonu|pltcl|sql)\s*;$/i || current_statement =~ /END\s*\$\$\s+(LANGUAGE\s+(plpgsql|plperl|plpythonu|pltcl|sql))?\s*;$/i statements.push(current_statement[0...-1]) # strip off last ; current_statement = "" end else statements.push(current_statement[0...-1]) # strip off last ; current_statement = "" end end end end return statements end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pg_migrate-0.1.11 | lib/pg_migrate/sql_reader.rb |