Class: WebpageArchivist::Migrations

Inherits:
Object
  • Object
show all
Defined in:
lib/webpage-archivist/migrations.rb

Overview

Define the sequel migrations and run them at startup, code adapted from sinatra-sequel gem

Constant Summary collapse

@@migrations =
[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.migration(name, &block) ⇒ Object



27
28
29
# File 'lib/webpage-archivist/migrations.rb', line 27

def self.migration(name, &block)
  @@migrations << {:name => name, :block => block}
end

Instance Method Details

#create_migrations_tableObject



82
83
84
85
86
87
88
# File 'lib/webpage-archivist/migrations.rb', line 82

def create_migrations_table
  DATABASE.create_table? :migrations do
    primary_key :id
    String :name, :null => false, :index => true
    timestamp :ran_at, :null => false
  end
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/webpage-archivist/migrations.rb', line 13

def run
  create_migrations_table
  @@migrations.each do |migration|
    if DATABASE[:migrations].filter(:name => migration[:name]).count == 0
      p "Running migration: #{migration[:name]}"
      DATABASE.transaction do
        migration[:block].yield
        DATABASE[:migrations] << {:name => migration[:name], :ran_at => Time.now}
      end
    end
  end
  @@migrations.clear
end