Class: BazaMigrations::MigrationsExecutor
- Inherits:
-
Object
- Object
- BazaMigrations::MigrationsExecutor
- Defined in:
- lib/baza_migrations/migrations_executor.rb
Instance Method Summary collapse
- #add_dir(path) ⇒ Object
- #add_file(path) ⇒ Object
- #add_migration(args) ⇒ Object
- #ensure_schema_migrations_table ⇒ Object
- #execute_migrations(direction = :up) ⇒ Object
-
#initialize(args = {}) ⇒ MigrationsExecutor
constructor
A new instance of MigrationsExecutor.
- #migration_already_executed?(migration_data) ⇒ Boolean
- #ordered_migrations ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ MigrationsExecutor
Returns a new instance of MigrationsExecutor.
2 3 4 5 |
# File 'lib/baza_migrations/migrations_executor.rb', line 2 def initialize(args = {}) @db = args.fetch(:db) @migrations = [] end |
Instance Method Details
#add_dir(path) ⇒ Object
7 8 9 10 11 12 13 14 15 |
# File 'lib/baza_migrations/migrations_executor.rb', line 7 def add_dir(path) path = File.realpath(path) Dir.foreach(path) do |file| next unless file.end_with?(".rb") full_path = "#{path}/#{file}" add_file(full_path) end end |
#add_file(path) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/baza_migrations/migrations_executor.rb', line 17 def add_file(path) match = File.basename(path).match(/\A(\d+)_(.+)\.rb\Z/) raise "Could not match class name from: #{path}" unless match require path class_name = StringCases.snake_to_camel(File.basename(match[2], File.extname(path))) add_migration( class_name: class_name, const: Object.const_get(class_name), time: Time.parse(match[1]) ) end |
#add_migration(args) ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/baza_migrations/migrations_executor.rb', line 31 def add_migration(args) @migrations << { class_name: args.fetch(:class_name), const: args.fetch(:const), time: args.fetch(:time) } end |
#ensure_schema_migrations_table ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/baza_migrations/migrations_executor.rb', line 65 def ensure_schema_migrations_table return if schema_migrations_table_exist? @db.tables.create( :baza_schema_migrations, columns: [ {name: :version, type: :varchar} ], indexes: [ {name: :index_version, columns: [:version], unique: true} ] ) end |
#execute_migrations(direction = :up) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/baza_migrations/migrations_executor.rb', line 45 def execute_migrations(direction = :up) ensure_schema_migrations_table ordered_migrations.each do |migration_data| next if migration_already_executed?(migration_data) migration_data.fetch(:const).new(db: @db).migrate(direction) @db.insert(:baza_schema_migrations, version: migration_data.fetch(:time).strftime("%Y%m%d%H%M%S")) end end |
#migration_already_executed?(migration_data) ⇒ Boolean
57 58 59 60 61 62 63 |
# File 'lib/baza_migrations/migrations_executor.rb', line 57 def migration_already_executed?(migration_data) if @db.single(:baza_schema_migrations, version: migration_data.fetch(:time).strftime("%Y%m%d%H%M%S")) return true else return false end end |
#ordered_migrations ⇒ Object
39 40 41 42 43 |
# File 'lib/baza_migrations/migrations_executor.rb', line 39 def ordered_migrations @migrations.sort do |migration1, migration2| migration1.fetch(:time) <=> migration2.fetch(:time) end end |