Class: SqlMigrations::SqlScript

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

Direct Known Subclasses

Fixture, Migration, Seed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts) ⇒ SqlScript

Returns a new instance of SqlScript.



6
7
8
9
10
11
12
13
14
15
# File 'lib/sql_migrations/sql_script.rb', line 6

def initialize(path, opts)
  @date     = opts[:date]
  @time     = opts[:time]
  @name     = opts[:name]
  @path     = opts[:path]
  @db_name  = opts[:db_name]
  @content  = IO.read(path)
  @type     = self.class.name.downcase.split('::').last
  @datetime = (@date + @time).to_i
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



4
5
6
# File 'lib/sql_migrations/sql_script.rb', line 4

def date
  @date
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/sql_migrations/sql_script.rb', line 4

def name
  @name
end

#timeObject (readonly)

Returns the value of attribute time.



4
5
6
# File 'lib/sql_migrations/sql_script.rb', line 4

def time
  @time
end

Class Method Details

.find(db_name, type) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/sql_migrations/sql_script.rb', line 34

def self.find(db_name, type)
  scripts = []
  Find.find(Dir.pwd) do |path|
    if !db_name.is_a? Array then db_name = [ db_name ] end
    file_date, file_time, file_name, file_db = self.file_options(db_name, type, path)
    next unless file_name
    scripts << self.new(path, date: file_date, time: file_time, name: file_name, db_name: file_db)
  end
  scripts.sort_by { |file| (file.date + file.time).to_i }
end

Instance Method Details

#execute(db) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/sql_migrations/sql_script.rb', line 17

def execute(db)
  @database = db
  return unless is_new?
  begin
    @database.db.transaction do
      @benchmark = Benchmark.measure do
        @database.db.run @content
      end
    end
  rescue
    puts "[-] Error while executing #{@type} #{@name} !"
    raise
  else
    on_success
  end
end