class MigrationBundler::Databases::SqliteDatabase
Attributes
db[R]
path[R]
Public Class Methods
create(url)
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 18 def create(url) new(url) do |database| database.db.execute(create_schema_migrations_sql) end end
create_schema_migrations_sql()
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 10 def create_schema_migrations_sql MigrationBundler::Util.strip_leading_whitespace <<-SQL CREATE TABLE schema_migrations( version INTEGER UNIQUE NOT NULL ); SQL end
exception_class()
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 28 def exception_class SQLite3::Exception end
migration_ext()
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 24 def migration_ext ".sql" end
new(url) { |self| ... }
click to toggle source
Calls superclass method
MigrationBundler::Databases::AbstractDatabase::new
# File lib/migration_bundler/databases/sqlite_database.rb, line 33 def initialize(url) super(url) raise ArgumentError, "Must initialize with a URI" unless url.kind_of?(URI) raise ArgumentError, "Must initialize with a sqlite URI" unless url.scheme.nil? || url.scheme == 'sqlite' path = url.path || url.opaque raise ArgumentError, "Must initialize with a sqlite URI that has a path component" unless path @path = path @db = SQLite3::Database.new(path) yield self if block_given? end
Public Instance Methods
all_versions()
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 56 def all_versions db.execute('SELECT version FROM schema_migrations ORDER BY version ASC').map { |row| row[0] } end
current_version()
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 52 def current_version db.get_first_value('SELECT MAX(version) FROM schema_migrations') end
drop()
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 70 def drop File.truncate(path, 0) if File.size?(path) end
dump_rows(table_name)
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 78 def dump_rows(table_name) statement = db.prepare("SELECT * FROM #{table_name}") result_set = statement.execute Array.new.tap do |statements| result_set.each do |row| values = row.map { |v| v.is_a?(String) ? SQLite3::Database.quote(v) : v } statements << "INSERT INTO #{table_name} (#{result_set.columns.join(', ')}) VALUES (#{values.join(', ')});" end end end
dump_to_schema(type, schema_path) { |name| ... }
click to toggle source
Outside of abstract interface…
# File lib/migration_bundler/databases/sqlite_database.rb, line 92 def dump_to_schema(type, schema_path) sql = MigrationBundler::Util.strip_leading_whitespace <<-SQL SELECT name, sql FROM sqlite_master WHERE sql NOT NULL AND type = '#{type}' ORDER BY name ASC SQL File.open(schema_path, 'a') do |f| db.execute(sql) do |row| name, sql = row next if name =~ /^sqlite/ f << "#{sql};\n\n" yield name if block_given? end f.puts end end
execute_migration(sql)
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 64 def execute_migration(sql) db.transaction do |db| db.execute_batch(sql) end end
insert_version(version)
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 60 def insert_version(version) db.execute("INSERT INTO schema_migrations(version) VALUES (?)", version) end
migrations_table?()
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 44 def migrations_table? has_table?('schema_migrations') end
origin_version()
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 48 def origin_version db.get_first_value('SELECT MIN(version) FROM schema_migrations') end
to_s()
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 74 def to_s path end
Private Instance Methods
has_table?(table)
click to toggle source
# File lib/migration_bundler/databases/sqlite_database.rb, line 111 def has_table?(table) db.get_first_value("SELECT name FROM sqlite_master WHERE type='table' AND name=?", table) end