module SimpleMigrations

Constants

VERSION

Public Class Methods

ensure_directories() click to toggle source
# File lib/simple_migrations.rb, line 11
def ensure_directories
  FileUtils.mkdir_p('db/migrate')
end
ensure_schema_migrations() click to toggle source
# File lib/simple_migrations.rb, line 15
def ensure_schema_migrations
  exec('SET client_min_messages TO WARNING')
  exec('create table if not exists schema_migrations(id bigint, ran_at timestamp)')
end
migrate() click to toggle source
# File lib/simple_migrations.rb, line 26
def migrate
  Dir['db/migrate/*'].each do |filename|
    basename = File.basename(filename)
    id = Integer(basename.split('_').first)
    if run_record = record_for(id)
      puts "#{basename} ran at #{run_record['ran_at']}"
    else
      puts "Running #{basename}..."
      exec('begin')
      sql = File.read(filename)
      puts sql
      exec(sql)
      exec("insert into schema_migrations(id, ran_at) values(#{id}, '#{Time.now.to_s}')")
      exec('commit')
      puts 'Done.'
    end
  end
end
sql_executor(&block) click to toggle source
# File lib/simple_migrations.rb, line 7
def sql_executor(&block)
  @sql_executor = block
end
stub_migration(name) click to toggle source
# File lib/simple_migrations.rb, line 20
def stub_migration(name)
  now = Time.now
  stamp = now.year.to_s + now.month.to_s.rjust(2, '0') + now.day.to_s.rjust(2, '0') + now.hour.to_s.rjust(2, '0') + now.min.to_s.rjust(2, '0') + now.sec.to_s.rjust(2, '0')
  FileUtils.touch("db/migrate/#{stamp}_#{name}.sql")
end

Private Class Methods

exec(sql) click to toggle source
# File lib/simple_migrations.rb, line 51
def exec(sql)
  @sql_executor.call(sql)
end
record_for(id) click to toggle source
# File lib/simple_migrations.rb, line 47
def record_for(id)
  exec("select * from schema_migrations where id = #{id}").first
end