Class: PgMigrate::SqlReader
- Inherits:
-
Object
- Object
- PgMigrate::SqlReader
- Defined in:
- lib/pg_migrate/sql_reader.rb
Instance Method Summary collapse
-
#initialize ⇒ SqlReader
constructor
A new instance of SqlReader.
-
#load_migration(migration_path) ⇒ Object
read in a migration file, converting lines of text into SQL statements that can be executed with our database connection.
Constructor Details
#initialize ⇒ SqlReader
Returns a new instance of SqlReader.
5 6 7 |
# File 'lib/pg_migrate/sql_reader.rb', line 5 def initialize end |
Instance Method Details
#load_migration(migration_path) ⇒ Object
read in a migration file, converting lines of text into SQL statements that can be executed with our database connection
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pg_migrate/sql_reader.rb', line 12 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 |