class ContentfulMigrations::Migrator
Constants
- DEFAULT_MIGRATION_PATH
- MIGRATION_FILENAME_REGEX
Attributes
access_token[R]
client[R]
env_id[R]
logger[R]
migration_content_type_name[R]
migrations_path[R]
page_size[R]
space[R]
space_id[R]
Public Class Methods
migrate(args = {})
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 13 def self.migrate(args = {}) new(parse_options(args)).migrate end
new(migrations_path:, access_token:, space_id:, migration_content_type_name:, logger:, env_id: nil)
click to toggle source
# 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 validate_options end
pending(args = {})
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 21 def self.pending(args = {}) new(parse_options(args)).pending end
rollback(args = {})
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 17 def self.rollback(args = {}) new(parse_options(args)).rollback end
Private Class Methods
parse_options(args)
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 78 def self.parse_options(args) { migrations_path: ENV.fetch('MIGRATION_PATH', DEFAULT_MIGRATION_PATH), access_token: ENV['CONTENTFUL_MANAGEMENT_ACCESS_TOKEN'], space_id: ENV['CONTENTFUL_SPACE_ID'], migration_content_type_name: MigrationContentType::DEFAULT_MIGRATION_CONTENT_TYPE, logger: Logger.new(STDOUT) }.merge(args) end
Public Instance Methods
migrate()
click to toggle source
# 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()
click to toggle source
# 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()
click to toggle source
# 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
Private Instance Methods
fetch_page(args)
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 115 def fetch_page(args) migration_content_type.entries.all(args).map { |m| m.version.to_i } end
load_migrated()
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 100 def load_migrated entries = [] args = { limit: @page_size, skip: entries.count } page = fetch_page(args) entries.concat(page) if page.size == @page_size load_migrated else entries end end
migrated()
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 96 def migrated @migrated ||= load_migrated end
migration_content_type()
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 133 def migration_content_type @migration_content_type ||= MigrationContentType.new( space: space, client: client, logger: logger ).resolve end
migration_files(paths)
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 129 def migration_files(paths) Dir[*paths.flat_map { |path| "#{path}/**/[0-9]*_*.rb" }] end
migrations(paths)
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 119 def migrations(paths) paths = Array(paths) migrations = migration_files(paths).map do |file| version, name, scope = parse_migration_filename(file) ContentfulMigrations::MigrationProxy.new(camelize(name), version.to_i, file, scope) end migrations.sort_by(&:version) end
parse_migration_filename(filename)
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 141 def parse_migration_filename(filename) File.basename(filename).scan(MIGRATION_FILENAME_REGEX).first end
ran?(migration)
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 92 def ran?(migration) migrated.include?(migration.version.to_i) end
validate_options()
click to toggle source
# File lib/contentful_migrations/migrator.rb, line 88 def validate_options raise InvalidMigrationPath, migrations_path unless File.directory?(migrations_path) end