Sha256: e43a30606c96c8d84b48b7a68a6f24a8de2b680f8b1385af263be34fffd92cc7

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

require 'pathname'
require 'rom/types'
require 'rom/initializer'

module ROM
  module SQL
    module Migration
      class Migrator
        extend Initializer

        DEFAULT_PATH = 'db/migrate'.freeze
        VERSION_FORMAT = '%Y%m%d%H%M%S'.freeze

        param :connection

        option :path, type: ROM::Types.Definition(Pathname), reader: true, default: proc { DEFAULT_PATH }

        def run(options = {})
          Sequel::Migrator.run(connection, path.to_s, options)
        end

        def pending?
          !Sequel::Migrator.is_current?(connection, path.to_s)
        end

        def migration(&block)
          Sequel.migration(&block)
        end

        def create_file(name, version = generate_version)
          filename = "#{version}_#{name}.rb"
          dirname = Pathname(path)
          fullpath = dirname.join(filename)

          FileUtils.mkdir_p(dirname)
          File.write(fullpath, migration_file_content)

          fullpath
        end

        def generate_version
          Time.now.utc.strftime(VERSION_FORMAT)
        end

        def migration_file_content
          File.read(Pathname(__FILE__).dirname.join('template.rb').realpath)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rom-sql-1.0.0.beta3 lib/rom/sql/migration/migrator.rb
rom-sql-1.0.0.beta2 lib/rom/sql/migration/migrator.rb
rom-sql-1.0.0.beta1 lib/rom/sql/migration/migrator.rb