Sha256: 539eb14c11ff0e4daebe6cbe3417d661fd55c8534933f9b9f1c7da6d97fa67f6

Contents?: true

Size: 1.38 KB

Versions: 17

Compression:

Stored size: 1.38 KB

Contents

module DataMigrate
  # Helper class to getting access to db schema
  # to allow data/schema combiation tasks
  class SchemaMigration
    def self.pending_schema_migrations
      all_migrations = DataMigrate::MigrationContext.new(migrations_paths).migrations
      sort_migrations(
        ActiveRecord::Migrator.new(:up, all_migrations, ActiveRecord::Base.connection.schema_migration).
        pending_migrations.
        map {|m| { version: m.version, kind: :schema }}
      )
    end

    def self.run(direction, migration_paths, version)
      ActiveRecord::MigrationContext.new(migration_paths, ActiveRecord::Base.connection.schema_migration).run(direction, version)
    end

    def self.sort_migrations(set1, set2 = nil)
      migrations = set1 + (set2 || [])
      migrations.sort {|a, b|  sort_string(a) <=> sort_string(b)}
    end

    def self.migrations_paths
      spec_name = DataMigrate.config.spec_name
      if spec_name && Rails.version > '6.1'
        ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, name: spec_name).migrations_paths
      elsif spec_name
        ActiveRecord::Base.configurations.configs_for(env_name: Rails.env, spec_name: spec_name).migrations_paths
      else
        Rails.application.config.paths["db/migrate"].to_a
      end
    end

    def self.sort_string(migration)
      "#{migration[:version]}_#{migration[:kind] == :data ? 1 : 0}"
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
data_migrate-9.0.0 lib/data_migrate/schema_migration_six.rb
data_migrate-8.5.0 lib/data_migrate/schema_migration_six.rb
data_migrate-8.4.2 lib/data_migrate/schema_migration_six.rb
data_migrate-8.4.1 lib/data_migrate/schema_migration_six.rb
data_migrate-8.4.0 lib/data_migrate/schema_migration_six.rb
data_migrate-8.3.0 lib/data_migrate/schema_migration_six.rb
data_migrate-8.2.0 lib/data_migrate/schema_migration_six.rb
data_migrate-8.1.1 lib/data_migrate/schema_migration_six.rb
data_migrate-8.1.0 lib/data_migrate/schema_migration_six.rb
data_migrate-8.0.0 lib/data_migrate/schema_migration_six.rb
data_migrate-8.0.0.rc2 lib/data_migrate/schema_migration_six.rb
data_migrate-8.0.0.rc1 lib/data_migrate/schema_migration_six.rb
data_migrate-7.0.2 lib/data_migrate/schema_migration_six.rb
data_migrate-7.0.1 lib/data_migrate/schema_migration_six.rb
data_migrate-7.0.0 lib/data_migrate/schema_migration_six.rb
data_migrate-6.8.0 lib/data_migrate/schema_migration_six.rb
data_migrate-6.7.0 lib/data_migrate/schema_migration_six.rb