Module: Webhookdb::Postgres

Extended by:
MethodUtilities
Includes:
Appydays::Loggable
Defined in:
lib/webhookdb/postgres.rb

Defined Under Namespace

Modules: Maintenance, ModelUtilities, Validations Classes: InTransaction, Model, TestingPixie

Constant Summary collapse

SUPERCLASSES =

Require paths for model superclasses.

[
  "webhookdb/postgres/model",
].freeze
MODELS =

Require paths for all Sequel models used by the app.

[
  "webhookdb/backfill_job",
  "webhookdb/backfill_job/service_integration_lock",
  "webhookdb/customer",
  "webhookdb/customer/reset_code",
  "webhookdb/database_document",
  "webhookdb/idempotency",
  "webhookdb/logged_webhook",
  "webhookdb/message/body",
  "webhookdb/message/delivery",
  "webhookdb/oauth/session",
  "webhookdb/organization",
  "webhookdb/organization/database_migration",
  "webhookdb/organization_membership",
  "webhookdb/role",
  "webhookdb/saved_query",
  "webhookdb/saved_view",
  "webhookdb/service_integration",
  "webhookdb/subscription",
  "webhookdb/sync_target",
  "webhookdb/system_log_event",
  "webhookdb/webhook_subscription",
  "webhookdb/webhook_subscription/delivery",
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodUtilities

attr_predicate, attr_predicate_accessor, singleton_attr_accessor, singleton_attr_reader, singleton_attr_writer, singleton_method_alias, singleton_predicate_accessor, singleton_predicate_reader

Class Method Details

.check_transaction(db, error_msg) ⇒ Object

Raises:



20
21
22
23
24
# File 'lib/webhookdb/postgres.rb', line 20

def self.check_transaction(db, error_msg)
  return true if self.unsafe_skip_transaction_check
  return true unless db.in_transaction?
  raise InTransaction, error_msg
end

.defer_after_commit(db, &block) ⇒ Object

Call block immediately if not deferring events; otherwise call it after db commit.

Raises:

  • (LocalJumpError)


171
172
173
174
175
# File 'lib/webhookdb/postgres.rb', line 171

def self.defer_after_commit(db, &block)
  raise LocalJumpError unless block
  return yield if self.do_not_defer_events?
  return db.after_commit(&block)
end

.defer_after_rollback(db, &block) ⇒ Object

Raises:

  • (LocalJumpError)


177
178
179
180
181
# File 'lib/webhookdb/postgres.rb', line 177

def self.defer_after_rollback(db, &block)
  raise LocalJumpError unless block
  return yield if self.do_not_defer_events?
  return db.after_rollback(&block)
end

.each_model_classObject



120
121
122
123
124
# File 'lib/webhookdb/postgres.rb', line 120

def self.each_model_class(&)
  self.each_model_superclass do |sc|
    sc.descendants.each(&)
  end
end

.each_model_superclassObject

Call the block for each registered model superclass.



116
117
118
# File 'lib/webhookdb/postgres.rb', line 116

def self.each_model_superclass(&)
  self.model_superclasses.each(&)
end

.load_modelsObject

After configuration, require in the model superclass files, to make sure their .db gets set and they’re in model_superclasses.



152
153
154
155
156
157
# File 'lib/webhookdb/postgres.rb', line 152

def self.load_models
  self.load_superclasses
  Appydays::Loggable[self].silence(:fatal) do
    self.require_models
  end
end

.load_superclassesObject

After configuration, load superclasses. You may need these without loading models, like if you need access to their DBs without loading them (if their tables do not yet exist)



144
145
146
147
148
# File 'lib/webhookdb/postgres.rb', line 144

def self.load_superclasses
  SUPERCLASSES.each do |sc|
    require(sc)
  end
end

.now_sql(&block) ⇒ Object

Return ‘Time.now’ as an expression suitable for Sequel/SQL. In some cases (like range @> expressions) you need to cast to a timestamptz explicitly, the implicit cast isn’t enough. And because ‘Time.now’ is an external dependency, we should always use Sequel.delay, to avoid any internal caching it will do, like in association blocks: github.com/jeremyevans/sequel/blob/master/doc/association_basics.rdoc#block-



165
166
167
168
# File 'lib/webhookdb/postgres.rb', line 165

def self.now_sql(&block)
  block ||= -> { Time.now }
  return Sequel.delay { Sequel.cast(block.call, :timestamptz) }
end

.register_model(path) ⇒ Object

Add a path to require once the database connection is set.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/webhookdb/postgres.rb', line 95

def self.register_model(path)
  self.logger.debug "Registered model for requiring: %s" % [path]

  # If the connection's set, require the path immediately.
  if self.model_superclasses.any?(&:db)
    Appydays::Loggable[self].silence(:fatal) do
      require(path)
    end
  end

  self.registered_models << path
end

.register_model_superclass(superclass) ⇒ Object

Register the given superclass as a base class for a set of models, for operations which should happen on all the current database connections.



84
85
86
87
# File 'lib/webhookdb/postgres.rb', line 84

def self.register_model_superclass(superclass)
  self.logger.debug "Registered model superclass: %p" % [superclass]
  self.model_superclasses << superclass
end

.require_modelsObject

Require the model classes once the database connection has been established



109
110
111
112
113
# File 'lib/webhookdb/postgres.rb', line 109

def self.require_models
  self.registered_models.each do |path|
    require path
  end
end

.run_all_migrations(target: nil) ⇒ Object



126
127
128
129
130
131
132
133
134
# File 'lib/webhookdb/postgres.rb', line 126

def self.run_all_migrations(target: nil)
  # :nocov:
  Sequel.extension :migration
  Webhookdb::Postgres.each_model_superclass do |cls|
    cls.install_all_extensions
    Sequel::Migrator.run(cls.db, Pathname(__FILE__).dirname.parent.parent + "db/migrations", target:)
  end
  # :nocov:
end

Instance Method Details

#registered_modelsObject

The list of models that will be required once the database connection has been established.



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

singleton_attr_reader :registered_models