Sha256: d93f1c65e9a9775af2ca4d1c86f6f7e7423c323d74193d8998a57e37a2d2a9f8

Contents?: true

Size: 1.27 KB

Versions: 8

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
        alert('No one 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

8 entries across 8 versions & 1 rubygems

Version Path
cyclone_lariat-1.0.0.rc8 lib/cyclone_lariat/services/migrate.rb
cyclone_lariat-1.0.0.rc7 lib/cyclone_lariat/services/migrate.rb
cyclone_lariat-1.0.0.rc6 lib/cyclone_lariat/services/migrate.rb
cyclone_lariat-1.0.0.rc5 lib/cyclone_lariat/services/migrate.rb
cyclone_lariat-1.0.0.rc4 lib/cyclone_lariat/services/migrate.rb
cyclone_lariat-1.0.0.rc3 lib/cyclone_lariat/services/migrate.rb
cyclone_lariat-1.0.0.rc2 lib/cyclone_lariat/services/migrate.rb
cyclone_lariat-1.0.0.rc1 lib/cyclone_lariat/services/migrate.rb