Sha256: 48ceb027e4381e122c2db1cddbc99987f5f578408d823dc5a0ad7e5b1c5acff3
Contents?: true
Size: 1.38 KB
Versions: 5
Compression:
Stored size: 1.38 KB
Contents
require 'fileutils' require 'simple_migrations/version' require 'simple_migrations/rake_tasks' module SimpleMigrations class << self def sql_executor(&block) @sql_executor = block end def ensure_directories FileUtils.mkdir_p('db/migrate') end 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 def stub_migration(name) now = Time.now stamp = now.year.to_s + now.month.to_s + now.day.to_s + now.hour.to_s + now.min.to_s + now.sec.to_s FileUtils.touch("db/migrate/#{stamp}_#{name}.sql") end 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("insert into schema_migrations(id, ran_at) values(#{id}, '#{Time.now.iso8601}')") exec('commit') puts 'Done.' end end end private def record_for(id) exec("select * from schema_migrations where id = #{id}").first end def exec(sql) @sql_executor.call(sql) end end end
Version data entries
5 entries across 5 versions & 1 rubygems