boto-rb-data 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/lib/boto/database/connection_error.rb +8 -0
- data/lib/boto/database/connections.rb +31 -0
- data/lib/boto/database/entity_mapper.rb +31 -0
- data/lib/boto/database/migrator.rb +68 -0
- data/lib/boto/database/missing_database_error.rb +12 -0
- data/lib/boto/database/version.rb +7 -0
- data/lib/boto/database.rb +18 -0
- data/lib/boto/plugins/database.rb +106 -0
- data/lib/boto/repository/foreign_key_violation.rb +8 -0
- data/lib/boto/repository/missing_declaration_error.rb +8 -0
- data/lib/boto/repository/not_found.rb +12 -0
- data/lib/boto/repository/reading.rb +83 -0
- data/lib/boto/repository/schema_mismatch.rb +8 -0
- data/lib/boto/repository/unique_violation.rb +8 -0
- data/lib/boto/repository/writing.rb +64 -0
- data/lib/boto/repository.rb +71 -0
- metadata +99 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: c89767b0b1588d40d2bbecd3c4c6517fae158ce121ee5950a0800784f265b53a
|
|
4
|
+
data.tar.gz: 25780fe24767c5c4f05ebbeac5c8dd4f69eff66f3328ae2dca4d979693bca148
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 89813b26e0cdfc0a1361d9b77e3d1afb0bd021222c6cd238f12731ada62df5f1f42d89166ba139be3bb6d5236d7bc5cefc51a9fa71b4ba26bb69a58ee1105044
|
|
7
|
+
data.tar.gz: efe06f21c86ab0119009d0338af4abfdec7ca1c6b3f2e83264ec97809e287ea6924314dcffb6673979aef16dc7a6aaefbf14039b1d807b365d055b47d50485bf
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Evo Tech Builder
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Database
|
|
5
|
+
# Databases of the running projects, keyed by namespace. The :database plugin fills it at start and
|
|
6
|
+
# empties it at stop, so it only changes at boot and shutdown; the lock covers the hand-off between threads.
|
|
7
|
+
class Connections
|
|
8
|
+
@registry = {}
|
|
9
|
+
@lock = Mutex.new
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
def register(namespace, database)
|
|
13
|
+
@lock.synchronize { @registry[namespace] = database }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def unregister(namespace)
|
|
17
|
+
@lock.synchronize { @registry.delete(namespace) }
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def registered?(namespace)
|
|
21
|
+
@lock.synchronize { @registry.key?(namespace) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def for(namespace)
|
|
25
|
+
database = @lock.synchronize { @registry[namespace] }
|
|
26
|
+
database || raise(MissingDatabaseError, namespace)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Database
|
|
5
|
+
# Turns a row (a Hash keyed by column Symbols) into an entity. Columns the entity does not declare are ignored.
|
|
6
|
+
class EntityMapper
|
|
7
|
+
attr_reader :entity, :members
|
|
8
|
+
|
|
9
|
+
def initialize(entity)
|
|
10
|
+
@entity = entity
|
|
11
|
+
@members = entity.members.freeze
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def build(row)
|
|
15
|
+
entity.new(**row.slice(*members))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Accepts a String or a Symbol, so filters and order fields can come from a request.
|
|
19
|
+
def member(name)
|
|
20
|
+
key = name.is_a?(String) ? name.to_sym : name
|
|
21
|
+
return key if members.include?(key)
|
|
22
|
+
|
|
23
|
+
raise ArgumentError, %(unknown field "#{name}" for #{entity})
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def missing_columns(columns)
|
|
27
|
+
members - columns
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sequel"
|
|
4
|
+
|
|
5
|
+
module Boto
|
|
6
|
+
module Database
|
|
7
|
+
# Applies the project's timestamped Sequel migrations (db/migrations) to its database.
|
|
8
|
+
class Migrator
|
|
9
|
+
Sequel.extension :migration
|
|
10
|
+
|
|
11
|
+
SCHEMA_TABLE = :schema_migrations
|
|
12
|
+
MIGRATION_GLOB = "*.rb"
|
|
13
|
+
UP = :up
|
|
14
|
+
DOWN = :down
|
|
15
|
+
STEPS_MESSAGE = "steps must be a positive Integer"
|
|
16
|
+
|
|
17
|
+
def initialize(db, dir)
|
|
18
|
+
@db = db
|
|
19
|
+
@dir = dir
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Returns the file names applied by this call, in apply order.
|
|
23
|
+
def migrate
|
|
24
|
+
return [] if file_names.empty?
|
|
25
|
+
|
|
26
|
+
before = applied_names
|
|
27
|
+
Sequel::TimestampMigrator.run(@db, @dir)
|
|
28
|
+
applied_names - before
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Returns the file names undone by this call, newest first.
|
|
32
|
+
def rollback(steps: 1)
|
|
33
|
+
validate_steps(steps)
|
|
34
|
+
applied = applied_names
|
|
35
|
+
undone = applied.last(steps)
|
|
36
|
+
return [] if undone.empty?
|
|
37
|
+
|
|
38
|
+
# The Sequel target is the version of the last migration that stays applied (0 undoes everything).
|
|
39
|
+
Sequel::TimestampMigrator.run(@db, @dir, target: (applied - undone).last.to_i)
|
|
40
|
+
undone.reverse
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def status
|
|
44
|
+
applied = applied_names
|
|
45
|
+
file_names.map { [_1, applied.include?(_1) ? UP : DOWN] }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
private
|
|
49
|
+
|
|
50
|
+
def file_names
|
|
51
|
+
Dir.glob(MIGRATION_GLOB, base: @dir).sort
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Sorted by name, which is sorted by timestamp.
|
|
55
|
+
def applied_names
|
|
56
|
+
return [] unless @db.table_exists?(SCHEMA_TABLE)
|
|
57
|
+
|
|
58
|
+
@db[SCHEMA_TABLE].order(:filename).select_map(:filename)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def validate_steps(steps)
|
|
62
|
+
return if steps.is_a?(Integer) && steps.positive?
|
|
63
|
+
|
|
64
|
+
raise ArgumentError, STEPS_MESSAGE
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
module Database
|
|
5
|
+
# Raised when a project asks for its database but its config/app.rb does not enable :database.
|
|
6
|
+
class MissingDatabaseError < StandardError
|
|
7
|
+
def initialize(namespace)
|
|
8
|
+
super("#{namespace} has no database: add app.use :database, url: ... to its config/app.rb")
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "boto"
|
|
4
|
+
require "sequel"
|
|
5
|
+
require "zeitwerk"
|
|
6
|
+
|
|
7
|
+
# Times are stored and read in UTC, so a created_at written by one server reads back the same on any other.
|
|
8
|
+
Sequel.default_timezone = :utc
|
|
9
|
+
|
|
10
|
+
loader = Zeitwerk::Loader.for_gem_extension(Boto)
|
|
11
|
+
loader.setup
|
|
12
|
+
|
|
13
|
+
module Boto
|
|
14
|
+
# Repositories, the :database plugin and migrations, on Sequel. Named Database, not Data, because
|
|
15
|
+
# Data.define is used inside Boto by the core and by third-party plugins.
|
|
16
|
+
module Database
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "sequel"
|
|
4
|
+
require "uri"
|
|
5
|
+
|
|
6
|
+
module Boto
|
|
7
|
+
module Plugins
|
|
8
|
+
# Connects the project to its database with Sequel. The connection is registered under the project
|
|
9
|
+
# namespace at start, and it answers the /health/ready probe with a test query.
|
|
10
|
+
class Database < Boto::Plugin
|
|
11
|
+
DEFAULT_POOL = 5
|
|
12
|
+
MASK = "***"
|
|
13
|
+
UNPARSEABLE_URL = "[unparseable url]"
|
|
14
|
+
FIBER_EXTENSION = :fiber_concurrency
|
|
15
|
+
HEALTH_CHECK = :database
|
|
16
|
+
URL_MESSAGE = "database: url must be a non-empty String or a callable that returns one"
|
|
17
|
+
POOL_MESSAGE = "database: pool must be a positive Integer"
|
|
18
|
+
|
|
19
|
+
# No #call: the plugin only has a lifecycle (connect, disconnect, health), so the core leaves it out of
|
|
20
|
+
# every request stack.
|
|
21
|
+
|
|
22
|
+
def attach(app)
|
|
23
|
+
@namespace = Object.const_get(app.title)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def start
|
|
27
|
+
Sequel.extension FIBER_EXTENSION
|
|
28
|
+
database = open_connection
|
|
29
|
+
check_repositories(database)
|
|
30
|
+
@database = database
|
|
31
|
+
Boto::Database::Connections.register(@namespace, @database)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Safe to call twice: the second call finds nothing left to close.
|
|
35
|
+
def stop
|
|
36
|
+
return unless @database
|
|
37
|
+
|
|
38
|
+
Boto::Database::Connections.unregister(@namespace)
|
|
39
|
+
@database.disconnect
|
|
40
|
+
@database = nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def health_checks
|
|
44
|
+
{ HEALTH_CHECK => -> { @database.test_connection } }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def inspect
|
|
48
|
+
"#<#{self.class.name} url=#{masked_url} pool=#{@pool}>"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# A new Sequel::Database on every call, not registered: the caller owns it and must disconnect it.
|
|
52
|
+
def open_connection
|
|
53
|
+
Sequel.connect(@url, max_connections: @pool, test: true, logger: @logger)
|
|
54
|
+
rescue StandardError => error
|
|
55
|
+
# cause: nil keeps the driver error, which may print the URL, out of the boot report.
|
|
56
|
+
raise Boto::Database::ConnectionError, "database: could not connect (#{error.class})", cause: nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
def configure(url:, pool: DEFAULT_POOL, logger: nil)
|
|
62
|
+
@url = resolve_url(url)
|
|
63
|
+
@pool = validate_pool(pool)
|
|
64
|
+
@logger = logger
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# A callable is resolved here, at boot, so a missing secret (KeyError) stops the boot.
|
|
68
|
+
def resolve_url(url)
|
|
69
|
+
value = url.respond_to?(:call) ? url.call : url
|
|
70
|
+
return value if value.is_a?(String) && !value.strip.empty?
|
|
71
|
+
|
|
72
|
+
raise ArgumentError, URL_MESSAGE
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def validate_pool(pool)
|
|
76
|
+
return pool if pool.is_a?(Integer) && pool.positive?
|
|
77
|
+
|
|
78
|
+
raise ArgumentError, POOL_MESSAGE
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Runs before the connection is registered, so a failed check leaves no connection behind.
|
|
82
|
+
def check_repositories(database)
|
|
83
|
+
repositories.each { _1.check!(database) }
|
|
84
|
+
rescue StandardError
|
|
85
|
+
database.disconnect
|
|
86
|
+
raise
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Only the classes already loaded: the core eager loads the project code before it starts the plugins.
|
|
90
|
+
def repositories
|
|
91
|
+
prefix = "#{@namespace.name}::"
|
|
92
|
+
Boto::Repository.subclasses.select do |repository|
|
|
93
|
+
repository.name.to_s.start_with?(prefix) && repository.declared?
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def masked_url
|
|
98
|
+
uri = URI.parse(@url)
|
|
99
|
+
uri.password = MASK if uri.password
|
|
100
|
+
uri.to_s
|
|
101
|
+
rescue URI::InvalidURIError
|
|
102
|
+
UNPARSEABLE_URL
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class Repository
|
|
5
|
+
# Read methods, extended into every repository. Filter keys and order fields are checked against the entity
|
|
6
|
+
# before they reach Sequel, so a value from a request can never pick a column the entity does not declare.
|
|
7
|
+
module Reading
|
|
8
|
+
DEFAULT_ORDER = :id
|
|
9
|
+
|
|
10
|
+
def find(id)
|
|
11
|
+
first_row(where_dataset(id: id))
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def find!(id)
|
|
15
|
+
find(id) || raise(NotFound.new(mapper.entity, id))
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def all(order: nil, limit: nil, offset: nil)
|
|
19
|
+
select_rows(dataset, order: order, limit: limit, offset: offset)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# order, limit and offset are reserved: a field with one of these names is filtered with find_by_<field>
|
|
23
|
+
# or query, because the keyword configures the query instead.
|
|
24
|
+
def where(order: nil, limit: nil, offset: nil, **filters)
|
|
25
|
+
select_rows(where_dataset(filters), order: order, limit: limit, offset: offset)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Goes through select_rows, not #where, so a field named order still filters.
|
|
29
|
+
def find_by(member, value)
|
|
30
|
+
select_rows(where_dataset({ member => value }), limit: 1).first
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def exists?(**filters)
|
|
34
|
+
!where_dataset(filters).empty?
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def count(**filters)
|
|
38
|
+
where_dataset(filters).count
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
# Escape hatch for queries the basics do not cover: the block gets the table dataset and returns one.
|
|
44
|
+
def query
|
|
45
|
+
map_rows(yield(dataset))
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def where_dataset(filters)
|
|
49
|
+
return dataset if filters.empty?
|
|
50
|
+
|
|
51
|
+
dataset.where(filters.to_h { |key, value| [mapper.member(key), value] })
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def select_rows(source, order: nil, limit: nil, offset: nil)
|
|
55
|
+
rows = source.order(*order_fields(order))
|
|
56
|
+
rows = rows.limit(non_negative(limit, :limit)) if limit
|
|
57
|
+
rows = rows.offset(non_negative(offset, :offset)) if offset
|
|
58
|
+
map_rows(rows)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def order_fields(order)
|
|
62
|
+
fields = Array(order)
|
|
63
|
+
fields = [DEFAULT_ORDER] if fields.empty?
|
|
64
|
+
fields.map { mapper.member(_1) }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def non_negative(value, option)
|
|
68
|
+
return value if value.is_a?(Integer) && value >= 0
|
|
69
|
+
|
|
70
|
+
raise ArgumentError, "#{option} must be a non-negative Integer"
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def first_row(source)
|
|
74
|
+
row = source.first
|
|
75
|
+
row && mapper.build(row)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def map_rows(source)
|
|
79
|
+
source.map { mapper.build(_1) }
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
class Repository
|
|
5
|
+
# Write methods, extended into every repository. Fields are checked against the entity, and id and the
|
|
6
|
+
# timestamps are managed here, so a caller can never set them.
|
|
7
|
+
module Writing
|
|
8
|
+
MANAGED_FIELDS = %i[id created_at updated_at].freeze
|
|
9
|
+
|
|
10
|
+
def create(**attrs)
|
|
11
|
+
row = writable_values(attrs).merge(timestamps_for(:created_at, :updated_at))
|
|
12
|
+
id = translate_violations { dataset.insert(row) }
|
|
13
|
+
find!(id)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Returns the entity after the write, so a missing id shows up as nil with no extra query.
|
|
17
|
+
def update(id, **attrs)
|
|
18
|
+
values = writable_values(attrs)
|
|
19
|
+
return find(id) if values.empty?
|
|
20
|
+
|
|
21
|
+
translate_violations { where_dataset(id: id).update(values.merge(timestamps_for(:updated_at))) }
|
|
22
|
+
find(id)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def delete(id)
|
|
26
|
+
translate_violations { where_dataset(id: id).delete }.positive?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Repository calls inside the block share its connection, which Sequel keeps per fiber or thread.
|
|
30
|
+
# Options go to Sequel: rollback: :always keeps a test's writes out of the database.
|
|
31
|
+
def transaction(**options, &)
|
|
32
|
+
database.transaction(**options, &)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def writable_values(attrs)
|
|
38
|
+
attrs.to_h { |key, value| [writable_member(key), value] }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def writable_member(key)
|
|
42
|
+
member = mapper.member(key)
|
|
43
|
+
return member unless MANAGED_FIELDS.include?(member)
|
|
44
|
+
|
|
45
|
+
raise ArgumentError, %(field "#{member}" is managed by the repository)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Only the timestamps the entity declares, so the repository never writes a column the entity lacks.
|
|
49
|
+
def timestamps_for(*fields)
|
|
50
|
+
now = clock.call
|
|
51
|
+
fields.select { mapper.members.include?(_1) }.to_h { [_1, now] }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# The driver message can carry the offending value, so the error names only the entity.
|
|
55
|
+
def translate_violations
|
|
56
|
+
yield
|
|
57
|
+
rescue Sequel::UniqueConstraintViolation
|
|
58
|
+
raise UniqueViolation, "#{mapper.entity}: unique constraint violated"
|
|
59
|
+
rescue Sequel::ForeignKeyConstraintViolation
|
|
60
|
+
raise ForeignKeyViolation, "#{mapper.entity}: foreign key constraint violated"
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Boto
|
|
4
|
+
# Read access to one table, with rows mapped to an entity (a Data). Repositories are classes, each one a
|
|
5
|
+
# direct subclass of Boto::Repository, because the boot check finds them through Class#subclasses.
|
|
6
|
+
class Repository
|
|
7
|
+
extend Reading
|
|
8
|
+
extend Writing
|
|
9
|
+
|
|
10
|
+
DEFAULT_CLOCK = -> { Time.now }
|
|
11
|
+
|
|
12
|
+
class << self
|
|
13
|
+
# Replaced in tests, so timestamps can be checked without waiting on the real time.
|
|
14
|
+
attr_writer :clock
|
|
15
|
+
|
|
16
|
+
def clock = @clock || DEFAULT_CLOCK
|
|
17
|
+
|
|
18
|
+
# Defines find_by_<member> for every member here, at boot, so no request reflects on the entity.
|
|
19
|
+
def entity(entity_class)
|
|
20
|
+
@mapper = Boto::Database::EntityMapper.new(entity_class)
|
|
21
|
+
entity_class.members.each { |member| define_finder(member) }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def table(name)
|
|
25
|
+
@table = name
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def declared?
|
|
29
|
+
!@mapper.nil? && !@table.nil?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Raises on the first entity member that is not a column of the table.
|
|
33
|
+
def check!(db)
|
|
34
|
+
raise SchemaMismatch, "#{name}: table #{table_name} does not exist" unless db.table_exists?(table_name)
|
|
35
|
+
|
|
36
|
+
column = mapper.missing_columns(db.schema(table_name).map(&:first)).first
|
|
37
|
+
return unless column
|
|
38
|
+
|
|
39
|
+
raise SchemaMismatch, %(#{name}: column "#{column}" of #{mapper.entity} is missing in table #{table_name})
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def define_finder(member)
|
|
45
|
+
define_singleton_method(:"find_by_#{member}") { |value| find_by(member, value) }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def mapper
|
|
49
|
+
@mapper || raise(MissingDeclarationError, "#{name} has no entity; declare it with `entity YourData`")
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def table_name
|
|
53
|
+
@table || raise(MissingDeclarationError, "#{name} has no table; declare it with `table :name`")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def dataset
|
|
57
|
+
database[table_name]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Asked on every call, never memorized: a reload replaces the connection.
|
|
61
|
+
def database
|
|
62
|
+
Boto::Database::Connections.for(namespace)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# The project owning the class is the first segment of its name (Catalogo::ProductRepository -> Catalogo).
|
|
66
|
+
def namespace
|
|
67
|
+
@namespace ||= Object.const_get(name.split("::").first)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: boto-rb-data
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.16.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Evo Tech Builder
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: boto-rb
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - '='
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: 0.16.0
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - '='
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: 0.16.0
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: sequel
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '5.0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '5.0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: sqlite3
|
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - "~>"
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '2.0'
|
|
47
|
+
type: :development
|
|
48
|
+
prerelease: false
|
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
50
|
+
requirements:
|
|
51
|
+
- - "~>"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: '2.0'
|
|
54
|
+
executables: []
|
|
55
|
+
extensions: []
|
|
56
|
+
extra_rdoc_files: []
|
|
57
|
+
files:
|
|
58
|
+
- LICENSE
|
|
59
|
+
- lib/boto/database.rb
|
|
60
|
+
- lib/boto/database/connection_error.rb
|
|
61
|
+
- lib/boto/database/connections.rb
|
|
62
|
+
- lib/boto/database/entity_mapper.rb
|
|
63
|
+
- lib/boto/database/migrator.rb
|
|
64
|
+
- lib/boto/database/missing_database_error.rb
|
|
65
|
+
- lib/boto/database/version.rb
|
|
66
|
+
- lib/boto/plugins/database.rb
|
|
67
|
+
- lib/boto/repository.rb
|
|
68
|
+
- lib/boto/repository/foreign_key_violation.rb
|
|
69
|
+
- lib/boto/repository/missing_declaration_error.rb
|
|
70
|
+
- lib/boto/repository/not_found.rb
|
|
71
|
+
- lib/boto/repository/reading.rb
|
|
72
|
+
- lib/boto/repository/schema_mismatch.rb
|
|
73
|
+
- lib/boto/repository/unique_violation.rb
|
|
74
|
+
- lib/boto/repository/writing.rb
|
|
75
|
+
homepage: https://github.com/EvoTechBuilder/boto-rb
|
|
76
|
+
licenses:
|
|
77
|
+
- MIT
|
|
78
|
+
metadata:
|
|
79
|
+
rubygems_mfa_required: 'true'
|
|
80
|
+
source_code_uri: https://github.com/EvoTechBuilder/boto-rb
|
|
81
|
+
changelog_uri: https://github.com/EvoTechBuilder/boto-rb/blob/main/CHANGELOG.md
|
|
82
|
+
rdoc_options: []
|
|
83
|
+
require_paths:
|
|
84
|
+
- lib
|
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '3.4'
|
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
91
|
+
requirements:
|
|
92
|
+
- - ">="
|
|
93
|
+
- !ruby/object:Gem::Version
|
|
94
|
+
version: '0'
|
|
95
|
+
requirements: []
|
|
96
|
+
rubygems_version: 4.0.20
|
|
97
|
+
specification_version: 4
|
|
98
|
+
summary: Repositories, the :database plugin and migrations for Boto, on Sequel.
|
|
99
|
+
test_files: []
|