forked from ubicloud/ubicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.rb
More file actions
106 lines (89 loc) · 3.51 KB
/
model.rb
File metadata and controls
106 lines (89 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true
require_relative "db"
require "sequel/model"
if ENV["RACK_ENV"] == "development"
Sequel::Model.cache_associations = false
end
Sequel::Model.plugin :auto_validations, skip_invalid: true
Sequel::Model.plugin :require_valid_schema
Sequel::Model.plugin :singular_table_names
Sequel::Model.plugin :subclasses unless ENV["RACK_ENV"] == "development"
Sequel::Model.plugin :column_encryption do |enc|
key = Config.clover_column_encryption_key
if Config.kms_decrypt_clover_column_encryption_key_with_arn
require "aws-sdk-kms"
kms_client = Aws::KMS::Client.new
response = kms_client.decrypt(ciphertext_blob: key, key_id: Config.kms_decrypt_clover_column_encryption_key_with_arn)
key = response.plaintext
end
enc.key 0, key
end
Sequel::Model.plugin :many_through_many
Sequel::Model.plugin :insert_conflict
Sequel::Model.plugin :inspect_pk
Sequel::Model.plugin :static_cache_cache, "cache/static_cache.cache"
Sequel::Model.plugin :pg_auto_constraint_validations, cache_file: "cache/pg_auto_constraint_validations.cache"
Sequel::Model.plugin :pg_auto_validate_enums, message: proc { |valid_values| "is not one of the supported values (#{valid_values.sort.join(", ")})" }
Sequel::Model.plugin :pg_eager_any_typed_array
Sequel::Model.plugin :association_lazy_eager_option
Sequel::Model.plugin :forbid_lazy_load if Config.unfrozen_test?
Sequel::Model.plugin :detect_unnecessary_association_options, action: :raise if Config.unfrozen_test? && ENV["FORCE_AUTOLOAD"] == "1"
if ENV["UNUSED_ASSOCIATIONS"]
Sequel::Model.plugin :unused_associations,
file: "unused-associations.json",
coverage_file: "unused-associations-coverage.json"
end
if (level = Config.database_logger_level) || Config.test?
require "logger"
LOGGER = Logger.new($stdout, level: level || "fatal")
DB.loggers << LOGGER
end
if ENV["CHECK_LOGGED_SQL"]
require "logger"
File.unlink("sql.log") if File.file?("sql.log")
f = File.open("sql.log", "ab")
# Remove optimization that does not use parameterization
def (Sequel::Model).reset_fast_pk_lookup_sql = nil
# Hack to make specs pass that mock Time.now and depend
# on certain number of Time.now calls
time = Time.now
def time.now
self
end
Logger.const_set(:Time, time)
sql_logger = Logger.new(f, level: :INFO)
sql_logger.formatter = proc do |sev, _, _, msg|
"#{sev} -- : #{msg}\0"
end
DB.loggers << sql_logger
end
module SequelExtensions
def delete(force: false, &)
# Do not error if this is a plain dataset that does not respond to destroy
return super(&) unless respond_to?(:destroy)
caller_lines = caller
rodauth_in_callstack = !caller_lines.grep(/rodauth/).empty?
destroy_in_callstack = !caller_lines.grep(/sequel\/model\/base.*_destroy_delete/).empty?
# This can happen when fast instance deletes are disabled (when CHECK_LOGGED_SQL
# environment variable is set)
callee_in_callstack = !caller_lines.grep(/#{Regexp.escape(__FILE__)}.*delete/).empty?
unless rodauth_in_callstack || destroy_in_callstack || callee_in_callstack || force
raise "Calling delete is discouraged as it skips hooks such as before_destroy, which " \
"we use to archive records. Use destroy instead. If you know what you are doing " \
"and still want to use delete, you can pass force: true to trigger delete."
end
if is_a?(Sequel::Dataset)
super(&)
else
super()
end
end
end
module Sequel
class Dataset
prepend SequelExtensions
end
class Model
prepend SequelExtensions
end
end