Class: Pcrd::Schema::ObjectReader

Inherits:
Object
  • Object
show all
Defined in:
lib/pcrd/schema/object_reader.rb

Overview

Reads the “secondary” schema objects that the load DDL deliberately omits (Schema::DDL creates only the table + primary key). Used by the target-readiness manifest to report and regenerate what must exist on the target before cutover.

Works against either cluster — point it at the source to discover objects, at the target to see what already exists.

Defined Under Namespace

Classes: Constraint, Grant, IdentityColumn, Index

Instance Method Summary collapse

Constructor Details

#initialize(pool) ⇒ ObjectReader

Returns a new instance of ObjectReader.



18
19
20
# File 'lib/pcrd/schema/object_reader.rb', line 18

def initialize(pool)
  @pool = pool
end

Instance Method Details

#column_comments(table_name, schema_name: "public") ⇒ Object

Hash<column_name, comment> for columns that have a COMMENT.



85
86
87
88
# File 'lib/pcrd/schema/object_reader.rb', line 85

def column_comments(table_name, schema_name: "public")
  @pool.exec(COLUMN_COMMENTS_SQL, [table_name, schema_name])
       .each_with_object({}) { |r, h| h[r["attname"]] = r["comment"] }
end

#constraints(table_name, schema_name: "public") ⇒ Object

Foreign-key, unique, and check constraints (not the primary key).



36
37
38
39
40
41
42
43
44
45
# File 'lib/pcrd/schema/object_reader.rb', line 36

def constraints(table_name, schema_name: "public")
  @pool.exec(CONSTRAINTS_SQL, [table_name, schema_name]).map do |r|
    Constraint.new(
      name:       r["conname"],
      kind:       r["contype"],
      definition: r["definition"],
      columns:    split_list(r["columns"])
    )
  end
end

#grants(table_name, schema_name: "public") ⇒ Object

Explicit table privileges (the owner’s implicit grant is excluded), one Grant per grantee with its sorted privilege list.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pcrd/schema/object_reader.rb', line 65

def grants(table_name, schema_name: "public")
  owner_name = owner(table_name, schema_name: schema_name)
  by_grantee = Hash.new { |h, k| h[k] = [] }

  @pool.exec(GRANTS_SQL, [table_name, schema_name]).each do |r|
    next if r["grantee"] == owner_name # owner already has everything

    by_grantee[r["grantee"]] << r["privilege_type"]
  end

  by_grantee.map { |grantee, privs| Grant.new(grantee: grantee, privileges: privs.sort) }
end

#identity_columns(table_name, schema_name: "public") ⇒ Object

Identity (GENERATED … AS IDENTITY) and serial (nextval default) columns.



48
49
50
51
52
53
54
55
# File 'lib/pcrd/schema/object_reader.rb', line 48

def identity_columns(table_name, schema_name: "public")
  @pool.exec(IDENTITY_SQL, [table_name, schema_name]).map do |r|
    IdentityColumn.new(
      column: r["attname"],
      kind:   r["attidentity"].to_s.empty? ? "serial" : "identity"
    )
  end
end

#indexes(table_name, schema_name: "public") ⇒ Object

Non-PK indexes that are not backing a unique/PK constraint (those are reported under #constraints instead, to avoid double-counting).



24
25
26
27
28
29
30
31
32
33
# File 'lib/pcrd/schema/object_reader.rb', line 24

def indexes(table_name, schema_name: "public")
  @pool.exec(INDEXES_SQL, [table_name, schema_name]).map do |r|
    Index.new(
      name:       r["index_name"],
      definition: r["definition"],
      unique:     r["indisunique"] == "t",
      columns:    split_list(r["columns"])
    )
  end
end

#owner(table_name, schema_name: "public") ⇒ Object

The table’s owner role name.



58
59
60
61
# File 'lib/pcrd/schema/object_reader.rb', line 58

def owner(table_name, schema_name: "public")
  row = @pool.exec(OWNER_SQL, [table_name, schema_name])
  row.ntuples.zero? ? nil : row[0]["owner"]
end

#table_comment(table_name, schema_name: "public") ⇒ Object

The table’s COMMENT, or nil.



79
80
81
82
# File 'lib/pcrd/schema/object_reader.rb', line 79

def table_comment(table_name, schema_name: "public")
  row = @pool.exec(TABLE_COMMENT_SQL, [table_name, schema_name])
  row.ntuples.zero? ? nil : row[0]["comment"]
end