module Prick module SubCommand # def self.migrate(database, username, file) TODO def self.migrate(database, username, file: nil) PgConn.new(database, username) { |conn| conn.schema.exist_relation? "prick", "versions" or raise Prick::Fail, "Can't read version from database" from_version = PrickVersion.new(conn.value "select version from prick.versions") or raise Prick::Fail, "Illegal version in table prick.versions" if file conn.transaction { conn.exec File.read(file) } else to_version = Prick.state.version from_version != to_version or raise Prick::Fail, "Already up to date" from_version < to_version or raise Prick::Fail, "Can't migrate backwards" migration_dir = "#{MIGRATION_DIR}/#{from_version}" File.directory? migration_dir or raise Prick::Fail, "Can't migrate from #{from_version} to #{to_version}" puts "Migrating from #{from_version} to #{to_version}" builder = Prick::Build::Builder.new(conn, migration_dir) conn.transaction { builder.execute conn conn.exec File.read(SCHEMA_VERSION_PATH) } end } end end end