Class: CouchbaseOrm::IndexMigrator
- Inherits:
-
Object
- Object
- CouchbaseOrm::IndexMigrator
- Defined in:
- lib/couchbase-orm/index_migrator.rb
Class Method Summary collapse
- .adopt(**options) ⇒ Object
- .cleanup(**options) ⇒ Object
- .migrate(**options) ⇒ Object
- .rollback(**options) ⇒ Object
- .status(**options) ⇒ Object
Instance Method Summary collapse
- #adopt ⇒ Object
- #cleanup ⇒ Object
-
#initialize(context: IndexMigrationContext.new, schema_migration: IndexSchemaMigration.new, out: nil) ⇒ IndexMigrator
constructor
A new instance of IndexMigrator.
- #migrate ⇒ Object
- #rollback ⇒ Object
- #status ⇒ Object
Constructor Details
#initialize(context: IndexMigrationContext.new, schema_migration: IndexSchemaMigration.new, out: nil) ⇒ IndexMigrator
Returns a new instance of IndexMigrator.
27 28 29 30 31 |
# File 'lib/couchbase-orm/index_migrator.rb', line 27 def initialize(context: IndexMigrationContext.new, schema_migration: IndexSchemaMigration.new, out: nil) @context = context @schema_migration = schema_migration @out = out || $stdout end |
Class Method Details
.adopt(**options) ⇒ Object
22 23 24 |
# File 'lib/couchbase-orm/index_migrator.rb', line 22 def adopt(**) new(**).adopt end |
.cleanup(**options) ⇒ Object
10 11 12 |
# File 'lib/couchbase-orm/index_migrator.rb', line 10 def cleanup(**) new(**).cleanup end |
.migrate(**options) ⇒ Object
6 7 8 |
# File 'lib/couchbase-orm/index_migrator.rb', line 6 def migrate(**) new(**).migrate end |
.rollback(**options) ⇒ Object
14 15 16 |
# File 'lib/couchbase-orm/index_migrator.rb', line 14 def rollback(**) new(**).rollback end |
.status(**options) ⇒ Object
18 19 20 |
# File 'lib/couchbase-orm/index_migrator.rb', line 18 def status(**) new(**).status end |
Instance Method Details
#adopt ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/couchbase-orm/index_migrator.rb', line 65 def adopt migration_def = @context.migrations.max_by(&:version) return nil unless migration_def @schema_migration.add_version(migration_def.version) migration_def.version end |
#cleanup ⇒ Object
73 74 75 76 77 78 |
# File 'lib/couchbase-orm/index_migrator.rb', line 73 def cleanup names = IndexMigration::IndexIntrospector.new.indexes.map { |row| row[:name] }.sort migration = IndexMigration.new names.each { |name| migration.remove_index(name) } names end |
#migrate ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/couchbase-orm/index_migrator.rb', line 33 def migrate @context.pending_migrations(@schema_migration.versions).each do |migration_def| migration = migration_def.klass.new migration.migrate(:up) @schema_migration.add_version(migration_def.version) end end |
#rollback ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/couchbase-orm/index_migrator.rb', line 41 def rollback version = @schema_migration.versions.max return nil unless version migration_def = @context.find(version) raise ArgumentError.new("Migration file not found for version #{version}") unless migration_def migration = migration_def.klass.new migration.migrate(:down) @schema_migration.remove_version(version) version end |
#status ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/couchbase-orm/index_migrator.rb', line 54 def status executed_versions = @schema_migration.versions lines = @context.migrations.map do |migration_def| state = executed_versions.include?(migration_def.version) ? 'up' : 'down' "#{state.ljust(6)} #{migration_def.version} #{migration_def.name}" end @out.puts(lines.join("\n")) unless lines.empty? lines end |