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


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

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)


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

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



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

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.



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

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.



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

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)



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

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-



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

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.



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

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.



81
82
83
84
# File 'lib/webhookdb/postgres.rb', line 81

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



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

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

.run_all_migrations(target: nil) ⇒ Object



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

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.



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

singleton_attr_reader :registered_models