Class: Migr8::DBMS::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/migr8.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(command = nil) ⇒ Base
Returns a new instance of Base.
669
670
671
672
673
|
# File 'lib/migr8.rb', line 669
def initialize(command=nil)
@command = command
@history_table = Repository::HISTORY_TABLE
@sqltmpfile = 'migr8/tmp.sql'
end
|
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
665
666
667
|
# File 'lib/migr8.rb', line 665
def command
@command
end
|
#history_table ⇒ Object
Returns the value of attribute history_table.
666
667
668
|
# File 'lib/migr8.rb', line 666
def history_table
@history_table
end
|
#sqltmpfile ⇒ Object
667
668
669
|
# File 'lib/migr8.rb', line 667
def sqltmpfile
@sqltmpfile
end
|
Class Method Details
.detect_by_command(command) ⇒ Object
841
842
843
844
|
# File 'lib/migr8.rb', line 841
def self.detect_by_command(command)
klazz = @subclasses.find {|klass| command =~ klass.const_get(:PATTERN) }
return klazz ? klazz.new(command) : nil
end
|
.inherited(klass) ⇒ Object
837
838
839
|
# File 'lib/migr8.rb', line 837
def self.inherited(klass)
@subclasses << klass
end
|
Instance Method Details
#apply_migrations(migs) ⇒ Object
800
801
802
|
# File 'lib/migr8.rb', line 800
def apply_migrations(migs)
_do_migrations(migs) {|mig| _applying_sql(mig) }
end
|
#create_history_table ⇒ Object
705
706
707
708
709
710
|
# File 'lib/migr8.rb', line 705
def create_history_table()
return false if history_table_exist?
sql = _history_table_statement()
run_sql(sql, :verbose=>true)
return true
end
|
#execute_sql(sql, cmdopt = nil) ⇒ Object
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
|
# File 'lib/migr8.rb', line 675
def execute_sql(sql, cmdopt=nil)
output, error = Open3.popen3("#{@command} #{cmdopt}") do |sin, sout, serr|
sin.write(sql)
sin.close()
[sout.read(), serr.read()]
end
if error && ! error.empty?
$stderr << error
raise SQLExecutionError.new
end
return output
end
|
#fetch_column_value_of(version, column) ⇒ Object
755
756
757
758
759
|
# File 'lib/migr8.rb', line 755
def fetch_column_value_of(version, column)
sql = "SELECT #{column} FROM #{history_table()} WHERE version = '#{version}';"
down_script = _execute_sql_and_get_column_as_text(sql)
return down_script
end
|
#get_migrations ⇒ Object
731
732
733
734
735
|
# File 'lib/migr8.rb', line 731
def get_migrations()
cmdopt = ""
separator = "|"
return _get_girations(cmdopt, separator)
end
|
#history_table_exist? ⇒ Boolean
727
728
729
|
# File 'lib/migr8.rb', line 727
def history_table_exist?
raise NotImplementedError.new("#{self.class.name}#history_table_exist?: not implemented yet.")
end
|
#new_skeleton ⇒ Object
829
830
831
|
# File 'lib/migr8.rb', line 829
def new_skeleton()
return self.class.const_get(:Skeleton).new
end
|
#q(str) ⇒ Object
825
826
827
|
# File 'lib/migr8.rb', line 825
def q(str)
return str.gsub(/\'/, "''")
end
|
#run_sql(sql, opts = {}) ⇒ Object
691
692
693
694
695
696
697
698
699
700
701
702
703
|
# File 'lib/migr8.rb', line 691
def run_sql(sql, opts={})
verbose = opts[:verbose]
tmpfile = sqltmpfile()
puts "$ cat <<_END_ > #{tmpfile}" if verbose
puts sql if verbose
puts "_END_" if verbose
File.open(tmpfile, 'w') {|f| f.write(sql) }
puts "$ #{@command} < #{tmpfile}" if verbose
ok = system("#{@command} < #{tmpfile}")
ok or
raise SQLExecutionError.new("Failed to run sql ('#{tmpfile}').")
File.unlink(tmpfile) unless Migr8::DEBUG
end
|
#unapply_migrations(migs, down_script_in_db = false) ⇒ Object
804
805
806
807
808
809
810
811
|
# File 'lib/migr8.rb', line 804
def unapply_migrations(migs, down_script_in_db=false)
if down_script_in_db
migs.each do |mig|
mig.down_script = fetch_column_value_of(mig.version, 'down_script')
end
end
_do_migrations(migs) {|mig| _unapplying_sql(mig) }
end
|