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/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)


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

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)


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

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



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

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.



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

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.



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

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)



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

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-



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

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.



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

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.



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

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



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

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

.run_all_migrations(target: nil) ⇒ Object



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

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.



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

singleton_attr_reader :registered_models