Class: RedshiftSimpleMigrator::Migrator

Inherits:
Object
  • Object
show all
Defined in:
lib/redshift_simple_migrator/migrator.rb

Constant Summary collapse

MIGRATION_FILE_PATTERN =
/^(?<version>\d+)_(?<migration_name>.*)\.rb$/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMigrator

Returns a new instance of Migrator.



11
12
13
14
15
16
17
18
19
20
# File 'lib/redshift_simple_migrator/migrator.rb', line 11

def initialize
  config = RedshiftSimpleMigrator.config
  @connection = Connection.new
  @migrations_path = config.migrations_path
  @schema_migrations_table_name =
    @connection.quote_ident(config.schema_migrations_table_name)

  @current_version_is_loaded = nil
  @current_migrations = nil
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/redshift_simple_migrator/migrator.rb', line 5

def connection
  @connection
end

#migrations_pathObject (readonly)

Returns the value of attribute migrations_path.



5
6
7
# File 'lib/redshift_simple_migrator/migrator.rb', line 5

def migrations_path
  @migrations_path
end

#schema_migrations_table_nameObject (readonly)

Returns the value of attribute schema_migrations_table_name.



5
6
7
# File 'lib/redshift_simple_migrator/migrator.rb', line 5

def schema_migrations_table_name
  @schema_migrations_table_name
end

Instance Method Details

#current_migrationsObject



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/redshift_simple_migrator/migrator.rb', line 22

def current_migrations
  return @current_migrations if @current_migrations

  migrations = Dir.entries(migrations_path).map do |name|
    if match = MIGRATION_FILE_PATTERN.match(name)
      require File.expand_path(File.join(migrations_path, name))
      migration_class = match[:migration_name].camelcase.constantize
      migration_class.new(connection, match[:version].to_i)
    end
  end
  @current_migrations = migrations.compact
end

#current_versionObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/redshift_simple_migrator/migrator.rb', line 52

def current_version
  return @current_version if @current_version_is_loaded

  connection.async_exec(get_version_query) do |result|
    versions = result.map do |row|
      row["version"].to_i
    end
    @current_version = versions.max
    @current_version_is_loaded = true
    @current_version
  end
rescue PG::UndefinedTable
  connection.exec(create_schema_migrations_table_query)
  retry
end

#run(target_version = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/redshift_simple_migrator/migrator.rb', line 68

def run(target_version = nil)
  direction, migrations = run_migrations(target_version)
  connection.with_transaction do
    migrations.each do |m|
      m.send(direction)
      if direction == :up
        insert_version(m.version)
      else
        remove_version(m.version)
      end
    end
  end
end

#run_migrations(target_version = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/redshift_simple_migrator/migrator.rb', line 35

def run_migrations(target_version = nil)
  direction = detect_direction(target_version)
  if direction == :up
    migrations = current_migrations.select do |m|
      include_target = target_version ? target_version.to_i >= m.version : true
      include_target && m.version > current_version.to_i
    end
    return direction, migrations.sort_by(&:version)
  else
    migrations = current_migrations.select do |m|
      target_version.to_i < m.version &&
        m.version <= current_version.to_i
    end
    return direction, migrations.sort_by {|m| -(m.version) }
  end
end