class Radical::Database
Attributes
connection_string[W]
migrations_path[RW]
Public Class Methods
applied_migrations()
click to toggle source
# File lib/radical/database.rb, line 74 def applied_migrations migrations.select { |f| applied_versions.include?(version(f)) } end
applied_versions()
click to toggle source
# File lib/radical/database.rb, line 67 def applied_versions sql = 'select * from radical_migrations order by version' rows = db.execute sql rows.map { |r| r['version'] } end
connection()
click to toggle source
# File lib/radical/database.rb, line 17 def connection conn = SQLite3::Database.new(connection_string) conn.results_as_hash = true conn.type_translation = true @connection ||= conn end
connection_string()
click to toggle source
# File lib/radical/database.rb, line 13 def connection_string @connection_string || ENV['DATABASE_URL'] end
db()
click to toggle source
# File lib/radical/database.rb, line 29 def db connection end
migrate!()
click to toggle source
# File lib/radical/database.rb, line 43 def migrate! db.execute 'create table if not exists radical_migrations ( version integer primary key )' pending_migrations.each do |file| puts "Executing migration #{file}" v = version(file) migration(file).migrate!(db: db, version: v) end end
migration(file)
click to toggle source
# File lib/radical/database.rb, line 33 def migration(file) context = Module.new context.class_eval(File.read(file), file) const = context.constants.find do |constant| context.const_get(constant).ancestors.include?(Radical::Migration) end context.const_get(const) end
migrations()
click to toggle source
# File lib/radical/database.rb, line 82 def migrations Dir[File.join(migrations_path || '.', 'migrations', '*.rb')].sort end
pending_migrations()
click to toggle source
# File lib/radical/database.rb, line 78 def pending_migrations migrations.reject { |f| applied_versions.include?(version(f)) } end
prepend_migrations_path(path)
click to toggle source
# File lib/radical/database.rb, line 25 def prepend_migrations_path(path) self.migrations_path = path end
rollback!()
click to toggle source
# File lib/radical/database.rb, line 55 def rollback! db.execute 'create table if not exists radical_migrations ( version integer primary key )' file = applied_migrations.last puts "Rolling back migration #{file}" v = version(file) migration(file).rollback!(db: db, version: v) end
version(filename)
click to toggle source
# File lib/radical/database.rb, line 86 def version(filename) filename.split(File::SEPARATOR).last.split('_').first.to_i end