Class: CouchbaseOrm::IndexMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase-orm/index_migrator.rb

Class Method Summary collapse

Instance Method Summary collapse

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(**options)
  new(**options).adopt
end

.cleanup(**options) ⇒ Object



10
11
12
# File 'lib/couchbase-orm/index_migrator.rb', line 10

def cleanup(**options)
  new(**options).cleanup
end

.migrate(**options) ⇒ Object



6
7
8
# File 'lib/couchbase-orm/index_migrator.rb', line 6

def migrate(**options)
  new(**options).migrate
end

.rollback(**options) ⇒ Object



14
15
16
# File 'lib/couchbase-orm/index_migrator.rb', line 14

def rollback(**options)
  new(**options).rollback
end

.status(**options) ⇒ Object



18
19
20
# File 'lib/couchbase-orm/index_migrator.rb', line 18

def status(**options)
  new(**options).status
end

Instance Method Details

#adoptObject



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

#cleanupObject



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

#migrateObject



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

#rollbackObject

Raises:

  • (ArgumentError)


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

#statusObject



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