Class: Hummingbird::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/hummingbird/plan.rb

Overview

This is responsible for parsing the ‘.plan` file, and for verifying it against the migrations stored on disk.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(planfile, migration_dir) ⇒ Plan

Returns a new instance of Plan.

Parameters:

  • planfile (String)

    The path to the ‘.plan` file.

  • migration_dir (String)

    The path to the base directory for all of the migration files referenced in the ‘.plan` file.



24
25
26
27
# File 'lib/hummingbird/plan.rb', line 24

def initialize(planfile, migration_dir)
  @planned_files = parse_plan(planfile)
  @migration_dir = migration_dir
end

Instance Attribute Details

#migration_dirString (readonly)

Returns The base directory for all of the migration files referenced in the ‘.plan` file.

Returns:

  • (String)

    The base directory for all of the migration files referenced in the ‘.plan` file.



11
12
13
# File 'lib/hummingbird/plan.rb', line 11

def migration_dir
  @migration_dir
end

#planned_filesArray<String> (readonly)

This list has not been verified against the files on disk, or against the database in any way.

Returns:

  • (Array<String>)

    The list of all files referenced in the ‘.plan` file.



18
19
20
# File 'lib/hummingbird/plan.rb', line 18

def planned_files
  @planned_files
end

Instance Method Details

#files_missing_from_migration_dirArray<String>

If this list is not empty, there is probably an error as there are files that have been planned to run that do not exist on disk.

Returns:



46
47
48
# File 'lib/hummingbird/plan.rb', line 46

def files_missing_from_migration_dir
  planned_files - migration_files
end

#files_missing_from_planArray<String>

Returns All #migration_files that are not in #planned_files.

Returns:



36
37
38
# File 'lib/hummingbird/plan.rb', line 36

def files_missing_from_plan
  migration_files - planned_files
end

#get_migration_contents(migration_file) ⇒ String

Return the contents of the specified migration file.

Parameters:

  • migration_file (String)

    The path to the desired migration file, relative to #migration_dir.

Returns:

  • (String)

    The contents of the specified migration file.



129
130
131
# File 'lib/hummingbird/plan.rb', line 129

def get_migration_contents(migration_file)
  File.read(File.expand_path(migration_file, @migration_dir))
end

#migration_filesArray<String>

Returns All files found under #migration_dir.

Returns:



30
31
32
# File 'lib/hummingbird/plan.rb', line 30

def migration_files
  @migration_files ||= get_migration_files
end

#migrations_to_be_run(already_run_migrations) ⇒ Array<Hash{Symbol => String}>

The names, and SQL for migrations that have yet to be run according to the list of already run migrations in ‘already_run_migrations`.

This delegates to #to_be_run_migration_file_names and attaches the associated SQL to each migration.

Parameters:

Returns:

  • (Array<Hash{Symbol => String}>)

    This is the list of migrations to be run, with their associated SQL as ‘[{ :migration_name => String, :sql => String }, …]`.

Raises:

  • (Hummingbird::PlanError)

    If any of the already_run_migrations are not in the list of planned files.

  • (Hummingbird::PlanError)

    If any of the if any of the files in already_run_migrations appear out of order relative to the planned files.

See Also:



74
75
76
77
78
79
80
81
# File 'lib/hummingbird/plan.rb', line 74

def migrations_to_be_run(already_run_migrations)
  to_be_run_migration_file_names(already_run_migrations).map do |f|
    {
      migration_name: f,
      sql: get_migration_contents(f)
    }
  end
end

#to_be_run_migration_file_names(already_run_migrations) ⇒ Array<String>

It compares ‘already_run_migrations` against the list of planned migrations, and return the list of migrations that have yet to be run.

Parameters:

Returns:

  • (Array<String>)

    The list of migration names that have not yet been run.

Raises:

  • (Hummingbird::PlanError)

    If any of the already_run_migrations are not in the list of planned files.

  • (Hummingbird::PlanError)

    If any of the if any of the files in already_run_migrations appear out of order relative to the planned files.

See Also:



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hummingbird/plan.rb', line 102

def to_be_run_migration_file_names(already_run_migrations)
  return planned_files if already_run_migrations.empty?

  unless (run_migrations_missing_from_plan = already_run_migrations.map {|a| a[:migration_name]} - planned_files).empty?
    raise Hummingbird::PlanError.new("Plan is missing the following already run migrations: #{run_migrations_missing_from_plan.join(', ')}",planned_files,already_run_migrations)
  end

  files = planned_files.dup
  already_run_migrations.each do |f|
    if f[:migration_name] == files.first
      files.shift
    else
      first_out_of_sync_run_on = DateTime.strptime(f[:run_on].to_s, '%s')

      raise Hummingbird::PlanError.new("Plan has '#{files.first}' before '#{f[:migration_name]}' which was run on #{first_out_of_sync_run_on}",planned_files,already_run_migrations)
    end
  end

  files
end