Class: ROM::SQL::Migration::Migrator

Inherits:
Object
  • Object
show all
Includes:
Options
Defined in:
lib/rom/sql/migration/migrator.rb

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, options = {}) ⇒ Migrator

Returns a new instance of Migrator.



14
15
16
17
# File 'lib/rom/sql/migration/migrator.rb', line 14

def initialize(connection, options = {})
  super
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



12
13
14
# File 'lib/rom/sql/migration/migrator.rb', line 12

def connection
  @connection
end

Instance Method Details

#create_file(name, version = generate_version) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/rom/sql/migration/migrator.rb', line 31

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

#generate_versionObject



42
43
44
# File 'lib/rom/sql/migration/migrator.rb', line 42

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

#migration(&block) ⇒ Object



27
28
29
# File 'lib/rom/sql/migration/migrator.rb', line 27

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

#migration_file_contentObject



46
47
48
# File 'lib/rom/sql/migration/migrator.rb', line 46

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

#pending?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/rom/sql/migration/migrator.rb', line 23

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

#run(options = {}) ⇒ Object



19
20
21
# File 'lib/rom/sql/migration/migrator.rb', line 19

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