Class: ContentfulMigrations::Migrator

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/contentful_migrations/migrator.rb

Defined Under Namespace

Classes: InvalidMigrationPath

Constant Summary collapse

DEFAULT_MIGRATION_PATH =
'db/contentful_migrations'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

#camelize, #constantize

Constructor Details

#initialize(migrations_path:, access_token:, space_id:, migration_content_type_name:, logger:, env_id: nil) ⇒ Migrator

Returns a new instance of Migrator.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/contentful_migrations/migrator.rb', line 28

def initialize(migrations_path:,
               access_token:,
               space_id:,
               migration_content_type_name:,
               logger:,
               env_id: nil)
  @migrations_path = migrations_path
  @access_token = access_token
  @logger = logger
  @space_id = space_id
  @migration_content_type_name = migration_content_type_name
  @client = Contentful::Management::Client.new(access_token)
  @env_id = env_id || ENV['CONTENTFUL_ENV'] || 'master'
  @space = @client.environments(space_id).find(@env_id)
  validate_options
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



25
26
27
# File 'lib/contentful_migrations/migrator.rb', line 25

def access_token
  @access_token
end

#clientObject (readonly)

Returns the value of attribute client.



25
26
27
# File 'lib/contentful_migrations/migrator.rb', line 25

def client
  @client
end

#env_idObject (readonly)

Returns the value of attribute env_id.



25
26
27
# File 'lib/contentful_migrations/migrator.rb', line 25

def env_id
  @env_id
end

#loggerObject (readonly)

Returns the value of attribute logger.



25
26
27
# File 'lib/contentful_migrations/migrator.rb', line 25

def logger
  @logger
end

#migration_content_type_nameObject (readonly)

Returns the value of attribute migration_content_type_name.



25
26
27
# File 'lib/contentful_migrations/migrator.rb', line 25

def migration_content_type_name
  @migration_content_type_name
end

#migrations_pathObject (readonly)

Returns the value of attribute migrations_path.



25
26
27
# File 'lib/contentful_migrations/migrator.rb', line 25

def migrations_path
  @migrations_path
end

#spaceObject (readonly)

Returns the value of attribute space.



25
26
27
# File 'lib/contentful_migrations/migrator.rb', line 25

def space
  @space
end

#space_idObject (readonly)

Returns the value of attribute space_id.



25
26
27
# File 'lib/contentful_migrations/migrator.rb', line 25

def space_id
  @space_id
end

Class Method Details

.migrate(args = {}) ⇒ Object



13
14
15
# File 'lib/contentful_migrations/migrator.rb', line 13

def self.migrate(args = {})
  new(parse_options(args)).migrate
end

.pending(args = {}) ⇒ Object



21
22
23
# File 'lib/contentful_migrations/migrator.rb', line 21

def self.pending(args = {})
  new(parse_options(args)).pending
end

.rollback(args = {}) ⇒ Object



17
18
19
# File 'lib/contentful_migrations/migrator.rb', line 17

def self.rollback(args = {})
  new(parse_options(args)).rollback
end

Instance Method Details

#migrateObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/contentful_migrations/migrator.rb', line 45

def migrate
  runnable = migrations(migrations_path).reject { |m| ran?(m) }
  if runnable.empty?
    logger.info('No migrations to run, everything up to date!')
  end

  runnable.each do |migration|
    logger.info("running migration #{migration.version} #{migration.name} ")
    migration.migrate(:up, client, space)
    migration.record_migration(migration_content_type)
  end
  self
end

#pendingObject



67
68
69
70
71
72
73
# File 'lib/contentful_migrations/migrator.rb', line 67

def pending
  runnable = migrations(migrations_path).reject { |m| ran?(m) }

  runnable.each do |migration|
    logger.info("Pending #{migration.version} #{migration.name} ")
  end
end

#rollbackObject



59
60
61
62
63
64
65
# File 'lib/contentful_migrations/migrator.rb', line 59

def rollback
  already_migrated = migrations(migrations_path).select { |m| ran?(m) }
  migration = already_migrated.pop
  logger.info("Rolling back migration #{migration.version} #{migration.name} ")
  migration.migrate(:down, client, space)
  migration.erase_migration(migration_content_type)
end