Sha256: a95ef128ea1de357e60261efd84cdf0a527970efe5a4242eefe69c56e335ea9f

Contents?: true

Size: 1.27 KB

Versions: 2

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module CycloneLariat
  module Services
    class Migrate
      attr_reader :repo, :dir

      def initialize(repo:, dir:)
        @repo = repo
        @dir = dir
      end

      def call
        return 'No migration exists' if !Dir.exist?(dir) || Dir.empty?(dir)

        output = []

        migration_paths.each do |path|
          filename = File.basename(path, '.rb')
          version, title = filename.split('_', 2)

          next if existed_migrations.include? version.to_i

          class_name = title.split('_').collect(&:capitalize).join
          output << "Up - #{version} #{class_name} #{path}"
          require_relative Pathname.new(Dir.pwd) + Pathname.new(path)
          Object.const_get(class_name).new.up
          repo.add(version)
        end

        output
      end

      private

      # Sorted by timestamp
      def migration_paths
        # lariat/migrate/1668161620_many_to_one.rb
        Dir.glob("#{dir}/*.rb").sort_by do |file_path|
          # 1668161620_many_to_one.rb
          file_name = file_path.split('/')[-1]
          # 1668161620
          file_name.split('_').first.to_i
        end
      end

      def existed_migrations
        @existed_migrations ||= repo.all.map { |row| row[:version] }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cyclone_lariat-1.0.0 lib/cyclone_lariat/services/migrate.rb
cyclone_lariat-1.0.0.rc9 lib/cyclone_lariat/services/migrate.rb