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


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

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)


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

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



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

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.



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

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.



146
147
148
149
150
151
# File 'lib/webhookdb/postgres.rb', line 146

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)



138
139
140
141
142
# File 'lib/webhookdb/postgres.rb', line 138

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-



159
160
161
162
# File 'lib/webhookdb/postgres.rb', line 159

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.



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

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.



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

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



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

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

.run_all_migrations(target: nil) ⇒ Object



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

def self.run_all_migrations(target: nil)
  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
end

Instance Method Details

#registered_modelsObject

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



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

singleton_attr_reader :registered_models