class Proxy::ContainerGateway::Database

Attributes

connection[R]

Public Class Methods

new(connection_string, prior_sqlite_db_path = nil) click to toggle source
# File lib/smart_proxy_container_gateway/database.rb, line 7
def initialize(connection_string, prior_sqlite_db_path = nil)
  @connection = Sequel.connect(connection_string)
  if connection_string.start_with?('sqlite://')
    @connection.run("PRAGMA foreign_keys = ON;")
    @connection.run("PRAGMA journal_mode = wal;")
  elsif prior_sqlite_db_path && File.exist?(prior_sqlite_db_path) &&
        (!@connection.table_exists?(:repositories) || @connection[:repositories].count.zero?)
    migrate_to_postgres(Sequel.sqlite(prior_sqlite_db_path), @connection)
    File.delete(prior_sqlite_db_path)
  end
  migrate
end

Private Instance Methods

migrate() click to toggle source
# File lib/smart_proxy_container_gateway/database.rb, line 22
def migrate
  Sequel.extension :migration, :core_extensions
  migration_path = File.join(__dir__, 'sequel_migrations')
  begin
    Sequel::Migrator.check_current(@connection, migration_path)
  rescue Sequel::Migrator::NotCurrentError
    Sequel::Migrator.run(@connection, migration_path)
  end
end
migrate_to_postgres(sqlite_db, postgres_db) click to toggle source
# File lib/smart_proxy_container_gateway/database.rb, line 32
def migrate_to_postgres(sqlite_db, postgres_db)
  migrate
  sqlite_db.transaction do
    sqlite_db.tables.each do |table|
      next if table == :schema_info

      sqlite_db[table].each do |row|
        postgres_db[table.to_sym].insert(row)
      end
    end
  end
end