Module: AssociateJsonb::ConnectionAdapters::ReferenceDefinition

Defined in:
lib/associate_jsonb/connection_adapters/schema_definitions/reference_definition.rb

Constant Summary collapse

ForeignKeyDefinition =
ActiveRecord::ConnectionAdapters::ForeignKeyDefinition

Instance Method Summary collapse

Instance Method Details

#add_to(table) ⇒ Object



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/associate_jsonb/connection_adapters/schema_definitions/reference_definition.rb', line 27

def add_to(table)
  return super unless store

  should_add_col = false
  if table.respond_to? :column_exists?
    should_add_col = !table.column_exists?(store)
  elsif table.respond_to? :columns
    should_add_col = table.columns.none? {|col| col.name.to_sym == store}
  end

  if should_add_col
    opts = { null: false, default: {} }
    table.column(store, :jsonb, **opts)
  end

  if foreign_key && column_names.length == 1
    fk = ForeignKeyDefinition.new(table.name, foreign_table_name, foreign_key_options)
    columns.each do |col_name, type, options|
      options ||= {}
      value = <<-SQL.squish
        jsonb_foreign_key(
          '#{fk.to_table}'::text,
          '#{fk.primary_key}'::text,
          #{store}::jsonb,
          '#{col_name}'::text,
          '#{type}'::text,
          #{nullable}
        )
      SQL
      table.constraint(
        name: "#{table.name}_#{col_name}_foreign_key",
        value: value,
        not_valid: true,
        deferrable: true
      )
    end
  elsif !nullable
    columns.each do |col_name, *|
      value = <<-SQL.squish
        #{store}->>'#{col_name}' IS NOT NULL
        AND
        #{store}->>'#{col_name}' <> ''
      SQL
      table.constraint(
        name: "#{table.name}_#{col_name}_not_null",
        value: value,
        not_valid: false,
        deferrable: true
      )
    end
  end

  return unless index

  columns.each do |col_name, type, *|
    type = :text if type == :string
    table.index(
      "CAST (\"#{store}\"->>'#{col_name}' AS #{type || :bigint})",
      using: :btree,
      name: "index_#{table.name}_on_#{store}_#{col_name}"
    )

    table.index(
      "(\"#{store}\"->>'#{col_name}')",
      using: :btree,
      name: "index_#{table.name}_on_#{store}_#{col_name}_text"
    )
  end
end

#column_nameObject

rubocop:enable Metrics/ParameterLists



23
24
25
# File 'lib/associate_jsonb/connection_adapters/schema_definitions/reference_definition.rb', line 23

def column_name
  store_key || super
end

#initialize(name, store: false, store_key: false, **options) ⇒ Object

rubocop:disable Metrics/ParameterLists



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/associate_jsonb/connection_adapters/schema_definitions/reference_definition.rb', line 9

def initialize(
  name,
  store: false,
  store_key: false,
  **options
)
  @store = store && store.to_sym
  @store_key = store_key && store_key.to_s unless options[:polymorphic]
  @nullable = options[:null] != false

  super(name, **options)
end