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/error_handler",
  "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)


169
170
171
172
173
# File 'lib/webhookdb/postgres.rb', line 169

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)


175
176
177
178
179
# File 'lib/webhookdb/postgres.rb', line 175

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



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

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.



114
115
116
# File 'lib/webhookdb/postgres.rb', line 114

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.



150
151
152
153
154
155
# File 'lib/webhookdb/postgres.rb', line 150

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)



142
143
144
145
146
# File 'lib/webhookdb/postgres.rb', line 142

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-



163
164
165
166
# File 'lib/webhookdb/postgres.rb', line 163

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
# File 'lib/webhookdb/postgres.rb', line 95

def self.register_model(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.



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

def self.register_model_superclass(superclass)
  self.model_superclasses << superclass
end

.require_modelsObject

Require the model classes once the database connection has been established



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

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

.run_all_migrations(target: nil) ⇒ Object



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

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