Module: Webhookdb::Postgres::ModelUtilities::ClassMethods

Defined in:
lib/webhookdb/postgres/model_utilities.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#appnameObject

The application name, set on database connections.



36
37
38
# File 'lib/webhookdb/postgres/model_utilities.rb', line 36

def appname
  @appname
end

Instance Method Details

#all_loaded_schemasObject

Return the Array of the schemas used by all descendents of the receiving model class.



90
91
92
# File 'lib/webhookdb/postgres/model_utilities.rb', line 90

def all_loaded_schemas
  return self.descendents.map(&:schema_name).uniq.compact
end

#by_name(classname) ⇒ Object

Fetch a model class by its classname. This can be the fully-qualified name, or just the bit after ‘Webhookdb::’.



82
83
84
85
86
# File 'lib/webhookdb/postgres/model_utilities.rb', line 82

def by_name(classname)
  return self.descendents.find do |cl|
    cl.name&.end_with?(classname)
  end
end

#connect(uri) ⇒ Object

Connect to the given URI using the standard extensions and other options.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/webhookdb/postgres/model_utilities.rb', line 39

def connect(uri)
  newdb = Sequel.connect(
    uri,
    logger: self.logger,
    extensions: [
      :is_distinct_from,
      :pagination,
      :pg_json,
      :pg_inet,
      :pg_array,
      :pg_streaming,
      :pg_range,
      :pg_interval,
      :pg_triggers,
      :pretty_table,
    ],
    **Webhookdb::Dbutil.configured_connection_options,
  )
  self.db = newdb
  self.descendents.each do |subclass|
    subclass.db = newdb
  end
end

#create_schema(name, &block) ⇒ Object

Create a new schema named name (if it doesn’t already exist).



95
96
97
98
# File 'lib/webhookdb/postgres/model_utilities.rb', line 95

def create_schema(name, &block)
  self.db.create_schema(name, if_not_exists: true)
  self.instance_eval(&block) if block
end

#create_schema!(name) ⇒ Object

Create the schema named name, dropping any previous schema by the same name.



101
102
103
104
# File 'lib/webhookdb/postgres/model_utilities.rb', line 101

def create_schema!(name, &)
  self.drop_schema!(name)
  self.create_schema(name, &)
end

#drop_schema(name) ⇒ Object

Drop the empty schema named name (if it exists).



107
108
109
# File 'lib/webhookdb/postgres/model_utilities.rb', line 107

def drop_schema(name)
  self.db.drop_schema(name, if_exists: true)
end

#drop_schema!(name) ⇒ Object

Drop the schema named name and all of its tables.



112
113
114
# File 'lib/webhookdb/postgres/model_utilities.rb', line 112

def drop_schema!(name)
  self.db.drop_schema(name, if_exists: true, cascade: true)
end

#extension_schemaObject



135
136
137
# File 'lib/webhookdb/postgres/model_utilities.rb', line 135

def extension_schema
  return "public"
end

#install_all_extensionsObject



139
140
141
142
143
144
145
146
147
148
149
# File 'lib/webhookdb/postgres/model_utilities.rb', line 139

def install_all_extensions
  extensions = [
    "citext",
    "pgcrypto",
    "btree_gist",
    "pg_trgm",
  ]
  extensions.each do |ext|
    self.db.execute("CREATE EXTENSION IF NOT EXISTS #{ext} WITH SCHEMA #{self.extension_schema}")
  end
end

#now_sqlObject



131
132
133
# File 'lib/webhookdb/postgres/model_utilities.rb', line 131

def now_sql
  return Webhookdb::Postgres.now_sql
end

#schema_exists?(name = self.schema_name) ⇒ Boolean

Returns true if a schema named name exists.

Returns:

  • (Boolean)


117
118
119
120
121
122
123
# File 'lib/webhookdb/postgres/model_utilities.rb', line 117

def schema_exists?(name=self.schema_name)
  ds = self.db[Sequel[:pg_catalog][:pg_namespace]].
    filter(nspname: name.to_s).
    select(:nspname)

  return ds.first ? true : false
end

#schema_nameObject

Return the name of the schema the receiving class is in.



126
127
128
129
# File 'lib/webhookdb/postgres/model_utilities.rb', line 126

def schema_name
  schemaname, = self.db.send(:schema_and_table, self.table_name)
  return schemaname
end

#tsort_each_child(model_class) ⇒ Object

TSort API – yield each of the given model_class‘s dependent model classes.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/webhookdb/postgres/model_utilities.rb', line 158

def tsort_each_child(model_class)
  # Include (non-anonymous) parents other than Model
  non_anon_parents = model_class.ancestors[1..].
    select { |cl| cl < self }.
    select(&:name)
  # rubocop:disable Style/ExplicitBlockArgument
  non_anon_parents.each do |parentclass|
    yield(parentclass)
  end
  # rubocop:enable Style/ExplicitBlockArgument

  # Include associated classes for which this model class's table has a
  # foreign key
  model_class.association_reflections.each do |name, config|
    next if config[:polymorphic]

    associated_class = Object.const_get(config[:class_name])

    if config[:type] == :many_to_one
      self.logger.debug "  %p#%s is dependent on %p" %
        [model_class, name, associated_class]
      yield(associated_class)
    else
      self.logger.debug "  %p#%s is *not* dependent on %p" %
        [model_class, name, associated_class]
    end
  end
end

#tsort_each_nodeObject

TSort API – yield each model class.



152
153
154
# File 'lib/webhookdb/postgres/model_utilities.rb', line 152

def tsort_each_node(&)
  self.descendents.select(&:name).each(&)
end

#update_connection_appnameObject

Set the connection’s application name if there is one.



71
72
73
74
75
76
77
78
# File 'lib/webhookdb/postgres/model_utilities.rb', line 71

def update_connection_appname
  return unless self.db
  self.logger.debug "Setting application name to %p" % [self.appname]
  self.db.synchronize do |conn|
    escaped = conn.escape_string(self.appname)
    conn.exec("SET application_name TO '%s'" % [escaped])
  end
end