File: data_schema.rb

package info (click to toggle)
ruby-data-migrate 6.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 568 kB
  • sloc: ruby: 1,844; makefile: 6
file content (63 lines) | stat: -rw-r--r-- 1,622 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true

module DataMigrate
  ##
  # Provides the definition method for data_schema.rb
  class Data < ActiveRecord::Schema
    # This method is based on the following two methods
    #   ActiveRecord::Schema#define
    #   ActiveRecord::ConnectionAdapters::SchemaStatements
    #     #assume_migrated_upto_version
    def define(info)
      DataMigrate::DataMigrator.assure_data_schema_table

      return if info[:version].blank?

      version = info[:version].to_i

      unless migrated.include?(version)
        execute "INSERT INTO #{sm_table} (version) VALUES ('#{version}')"
      end

      insert(version)
    end

    private

    def migrated
      @migrated ||= select_values("SELECT version FROM #{sm_table}").map(&:to_i)
    end

    def versions
      @versions ||= begin
        versions = []
        Dir.foreach(DataMigrate::DataMigrator.full_migrations_path) do |file|
          match_data = DataMigrate::DataMigrator.match(file)
          versions << match_data[1].to_i if match_data
        end
        versions
      end
    end

    def insert(version)
      inserted = Set.new
      (versions - migrated).each do |v|
        if inserted.include?(v)
          raise "Duplicate data migration #{v}. Please renumber your data " \
            "migrations to resolve the conflict."
        elsif v < version
          execute "INSERT INTO #{sm_table} (version) VALUES ('#{v}')"
          inserted << v
        end
      end
    end

    def sm_table
      quote_table_name(table_name)
    end

    def table_name
      DataMigrate::DataSchemaMigration.table_name
    end
  end
end