Class: Hummingbird::Plan
- Inherits:
-
Object
- Object
- Hummingbird::Plan
- 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
-
#migration_dir ⇒ String
readonly
The base directory for all of the migration files referenced in the ‘.plan` file.
-
#planned_files ⇒ Array<String>
readonly
This list has not been verified against the files on disk, or against the database in any way.
Instance Method Summary collapse
-
#files_missing_from_migration_dir ⇒ Array<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.
-
#files_missing_from_plan ⇒ Array<String>
All #migration_files that are not in #planned_files.
-
#get_migration_contents(migration_file) ⇒ String
Return the contents of the specified migration file.
-
#initialize(planfile, migration_dir) ⇒ Plan
constructor
A new instance of Plan.
-
#migration_files ⇒ Array<String>
All files found under #migration_dir.
-
#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`.
-
#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.
Constructor Details
#initialize(planfile, migration_dir) ⇒ Plan
Returns a new instance of Plan.
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_dir ⇒ String (readonly)
Returns 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_files ⇒ Array<String> (readonly)
This list has not been verified against the files on disk, or against the database in any way.
18 19 20 |
# File 'lib/hummingbird/plan.rb', line 18 def planned_files @planned_files end |
Instance Method Details
#files_missing_from_migration_dir ⇒ Array<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.
46 47 48 |
# File 'lib/hummingbird/plan.rb', line 46 def files_missing_from_migration_dir planned_files - migration_files end |
#files_missing_from_plan ⇒ Array<String>
Returns All #migration_files that are not in #planned_files.
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.
129 130 131 |
# File 'lib/hummingbird/plan.rb', line 129 def get_migration_contents(migration_file) File.read(File.(migration_file, @migration_dir)) end |
#migration_files ⇒ Array<String>
Returns All files found under #migration_dir.
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.
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.
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 |