Class: YeetDba::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/yeet_dba/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table_name:, tables:) ⇒ Table

Returns a new instance of Table.



5
6
7
8
# File 'lib/yeet_dba/table.rb', line 5

def initialize(table_name:, tables:)
  @table_name = table_name
  @tables = tables
end

Instance Attribute Details

#table_nameObject

Returns the value of attribute table_name.



3
4
5
# File 'lib/yeet_dba/table.rb', line 3

def table_name
  @table_name
end

#tablesObject

Returns the value of attribute tables.



3
4
5
# File 'lib/yeet_dba/table.rb', line 3

def tables
  @tables
end

Instance Method Details

#invalid_columnsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yeet_dba/table.rb', line 10

def invalid_columns
  missing_keys_array = []
  columns.each do |db_column|
    column = Column.new(db_column: db_column, table_name: table_name, tables: tables)
    next unless column.is_association?
    next if column.polymorphic_association?
    next if column.foreign_key_exists?
    next if column.association_table_name.blank?
    verify_data = VerifyData.new(column: column)
    next unless verify_data.orphaned_rows?

    invalid_column = InvalidColumn.new(table_name: table_name,
                                       column: column,
                                       verify_data: verify_data)
    missing_keys_array.push(invalid_column)

  end
  missing_keys_array
end

#missing_keysObject



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
# File 'lib/yeet_dba/table.rb', line 30

def missing_keys
  missing_keys_array = []
  columns.each do |db_column|
    column = Column.new(db_column: db_column, table_name: table_name, tables: tables)
    next unless column.is_association?

    unless column.model
      puts "WARNING - cannot find a model for #{table_name} . #{db_column.name} | #{column&.association_table_name}"
    end

    unless column.association
      puts "WARNING - cannot find an association for #{table_name} . #{db_column.name} | #{column&.association_table_name}"
    end

    next if column.polymorphic_association?
    next if column.foreign_key_exists?
    next if column.association_table_name.blank?

    if VerifyData.new(column: column).orphaned_rows?
      puts "YeetDba - orphaned rows. Skipping #{table_name} . #{db_column.name} | #{column&.association_table_name}"
      next
    end

    foreign_key = ForeignKey.new(table_a: table_name,
                                 table_b: column&.association_table_name,
                                 column: db_column.name)
    missing_keys_array.push(foreign_key)
  end
  missing_keys_array
end