Class: ContentfulMigrations::Migrator
- Inherits:
-
Object
- Object
- ContentfulMigrations::Migrator
- 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
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#env_id ⇒ Object
readonly
Returns the value of attribute env_id.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#migration_content_type_name ⇒ Object
readonly
Returns the value of attribute migration_content_type_name.
-
#migrations_path ⇒ Object
readonly
Returns the value of attribute migrations_path.
-
#page_size ⇒ Object
readonly
Returns the value of attribute page_size.
-
#space ⇒ Object
readonly
Returns the value of attribute space.
-
#space_id ⇒ Object
readonly
Returns the value of attribute space_id.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(migrations_path:, access_token:, space_id:, migration_content_type_name:, logger:, env_id: nil) ⇒ Migrator
constructor
A new instance of Migrator.
- #migrate ⇒ Object
- #pending ⇒ Object
- #rollback ⇒ Object
Methods included from Utils
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 44 |
# 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) @page_size = 1000 end |
Instance Attribute Details
#access_token ⇒ Object (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 |
#client ⇒ Object (readonly)
Returns the value of attribute client.
25 26 27 |
# File 'lib/contentful_migrations/migrator.rb', line 25 def client @client end |
#env_id ⇒ Object (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 |
#logger ⇒ Object (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_name ⇒ Object (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_path ⇒ Object (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 |
#page_size ⇒ Object (readonly)
Returns the value of attribute page_size.
25 26 27 |
# File 'lib/contentful_migrations/migrator.rb', line 25 def page_size @page_size end |
#space ⇒ Object (readonly)
Returns the value of attribute space.
25 26 27 |
# File 'lib/contentful_migrations/migrator.rb', line 25 def space @space end |
#space_id ⇒ Object (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((args)).migrate end |
.pending(args = {}) ⇒ Object
21 22 23 |
# File 'lib/contentful_migrations/migrator.rb', line 21 def self.pending(args = {}) new((args)).pending end |
.rollback(args = {}) ⇒ Object
17 18 19 |
# File 'lib/contentful_migrations/migrator.rb', line 17 def self.rollback(args = {}) new((args)).rollback end |
Instance Method Details
#migrate ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/contentful_migrations/migrator.rb', line 46 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 |
#pending ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/contentful_migrations/migrator.rb', line 68 def pending runnable = migrations(migrations_path).reject { |m| ran?(m) } runnable.each do |migration| logger.info("Pending #{migration.version} #{migration.name} ") end end |
#rollback ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/contentful_migrations/migrator.rb', line 60 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 |