Class: Migr8::DBMS::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/migr8.rb

Direct Known Subclasses

MySQL, PostgreSQL, SQLite3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command = nil) ⇒ Base

Returns a new instance of Base.



671
672
673
674
675
# File 'lib/migr8.rb', line 671

def initialize(command=nil)
  @command = command
  @history_table = Repository::HISTORY_TABLE
  @sqltmpfile = 'migr8/tmp.sql'
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



667
668
669
# File 'lib/migr8.rb', line 667

def command
  @command
end

#history_tableObject

Returns the value of attribute history_table.



668
669
670
# File 'lib/migr8.rb', line 668

def history_table
  @history_table
end

#sqltmpfileObject

:nodoc:



669
670
671
# File 'lib/migr8.rb', line 669

def sqltmpfile
  @sqltmpfile
end

Class Method Details

.detect_by_command(command) ⇒ Object



843
844
845
846
# File 'lib/migr8.rb', line 843

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



839
840
841
# File 'lib/migr8.rb', line 839

def self.inherited(klass)
  @subclasses << klass
end

Instance Method Details

#apply_migrations(migs) ⇒ Object



802
803
804
# File 'lib/migr8.rb', line 802

def apply_migrations(migs)
  _do_migrations(migs) {|mig| _applying_sql(mig) }
end

#create_history_tableObject



707
708
709
710
711
712
# File 'lib/migr8.rb', line 707

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



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
# File 'lib/migr8.rb', line 677

def execute_sql(sql, cmdopt=nil)
  output, error = Open3.popen3("#{@command} #{cmdopt}") do |sin, sout, serr|
    sin.write(sql)
    sin.close()   # important!
    [sout.read(), serr.read()]
  end
  #if output && ! output.empty?
  #  $stdout << output
  #end
  if error && ! error.empty?
    $stderr << error
    raise SQLExecutionError.new
  end
  return output
end

#fetch_column_value_of(version, column) ⇒ Object



757
758
759
760
761
# File 'lib/migr8.rb', line 757

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_migrationsObject



733
734
735
736
737
# File 'lib/migr8.rb', line 733

def get_migrations()
  cmdopt = ""
  separator = "|"
  return _get_girations(cmdopt, separator)
end

#history_table_exist?Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


729
730
731
# File 'lib/migr8.rb', line 729

def history_table_exist?
  raise NotImplementedError.new("#{self.class.name}#history_table_exist?: not implemented yet.")
end

#new_skeletonObject



831
832
833
# File 'lib/migr8.rb', line 831

def new_skeleton()
  return self.class.const_get(:Skeleton).new
end

#q(str) ⇒ Object



827
828
829
# File 'lib/migr8.rb', line 827

def q(str)
  return str.gsub(/\'/, "''")
end

#run_sql(sql, opts = {}) ⇒ Object



693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'lib/migr8.rb', line 693

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



806
807
808
809
810
811
812
813
# File 'lib/migr8.rb', line 806

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