realm-rom 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4726882d01a80b82479b77ce4e5dc356147f2ce85bd6dc896330238ff12ddd27
4
+ data.tar.gz: b8842e600b7883a4b3b20b96beceb20841b00bf624e7ebfa91db3f0b75c52ab4
5
+ SHA512:
6
+ metadata.gz: d6c4791cdb46382952d50369174255fff517fb823c6ddc8222bf05751e84324a63f5385a964b84aa4a41aea879b619fc15999e3a1661174aafb659af47756400
7
+ data.tar.gz: 712acbcc76bb72823a998b2fe69ca69273fec5c2ff6f2f61893b092e4297f053098983dfdd39374edbdf4315222b13d38d6b8b0baf2a77416e844b8bd2387f72
data/lib/realm-rom.rb ADDED
@@ -0,0 +1,8 @@
1
+ # rubocop:disable Naming/FileName
2
+ # frozen_string_literal: true
3
+
4
+ Dir[File.join(File.dirname(__FILE__), 'realm', '**', '*.rb')].sort.each do |f|
5
+ require f
6
+ end
7
+
8
+ # rubocop:enable Naming/FileName
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rom'
4
+ require 'realm/health_status'
5
+ require 'active_support/core_ext/string'
6
+
7
+ module Realm
8
+ module ROM
9
+ class Gateway
10
+ def initialize(url:, root_module:, class_path:, migration_path:, **)
11
+ @url = url
12
+ @root_module = root_module
13
+ @class_path = class_path
14
+ @migration_path = migration_path
15
+ end
16
+
17
+ def health
18
+ issues = []
19
+ issues << 'Cannot connect to db' unless default_gateway.connection.test_connection
20
+ issues << 'Pending migrations' if default_gateway.migrator.pending?
21
+ HealthStatus.from_issues(issues)
22
+ end
23
+
24
+ def method_missing(...)
25
+ client.send(...)
26
+ end
27
+
28
+ def respond_to_missing?(...)
29
+ client.respond_to?(...)
30
+ end
31
+
32
+ private
33
+
34
+ def client
35
+ @client ||= ::ROM.container(config)
36
+ end
37
+
38
+ def config
39
+ ::ROM::Configuration.new(:sql, @url, **config_options).tap do |config|
40
+ config.auto_registration(@class_path, namespace: @root_module.to_s)
41
+ end
42
+ end
43
+
44
+ def config_options
45
+ { search_path: @root_module.to_s.underscore, migrator: { path: @migration_path } }
46
+ end
47
+
48
+ def default_gateway
49
+ client.gateways[:default]
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'realm/plugin'
4
+ require_relative 'gateway'
5
+
6
+ module Realm
7
+ module ROM
8
+ class Plugin < Realm::Plugin
9
+ def self.setup(config, container)
10
+ return unless config.persistence_gateway[:type] == :rom
11
+
12
+ gateway = Gateway.new(config.persistence_gateway)
13
+ container.register('persistence.gateway', gateway)
14
+ container.register(:rom, gateway) # for backward compatibility as we access it a lot in tests
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake'
4
+ require 'active_support/core_ext/string'
5
+
6
+ module Realm
7
+ module ROM
8
+ module RakeTasks
9
+ # rubocop:disable Metrics/AbcSize, Metrics/BlockLength, Metrics/MethodLength
10
+ def self.setup(engine_name, engine_root: Rails.root.join('engines', engine_name.to_s),
11
+ db_url: ENV['DATABASE_URL'])
12
+ return unless db_url
13
+
14
+ options = { search_path: engine_name.to_s, migrator: { path: "#{engine_root}/db/migrate" } }
15
+ config = ::ROM::Configuration.new(:sql, db_url, options)
16
+ gateway = config.gateways[:default]
17
+
18
+ Rake.application.in_namespace(:db) do
19
+ Rake::Task.define_task(:init_schema) do
20
+ gateway.run "CREATE SCHEMA IF NOT EXISTS \"#{engine_name}\""
21
+ puts "<= #{engine_name}:db:init_schema executed"
22
+ end
23
+
24
+ Rake::Task.define_task(:drop_schema) do
25
+ gateway.run "DROP SCHEMA \"#{engine_name}\" CASCADE"
26
+ puts "<= #{engine_name}:db:drop_schema executed"
27
+ end
28
+
29
+ Rake.application.last_description = 'Perform migration reset (full erase and migration up)'
30
+ Rake::Task.define_task(:reset) do
31
+ gateway.run_migrations(target: 0)
32
+ gateway.run_migrations
33
+ puts "<= #{engine_name}:db:reset executed"
34
+ end
35
+
36
+ Rake.application.last_description = 'Migrate the database (options [version_number])]'
37
+ Rake::Task.define_task(:migrate, %i[version]) do |_, args|
38
+ version = args[:version]
39
+
40
+ if version.nil?
41
+ gateway.run_migrations
42
+ puts "<= #{engine_name}:db:migrate executed"
43
+ else
44
+ gateway.run_migrations(target: version.to_i)
45
+ puts "<= #{engine_name}:db:migrate version=[#{version}] executed"
46
+ end
47
+ end
48
+
49
+ Rake.application.last_description = 'Perform migration down (removes all tables)'
50
+ Rake::Task.define_task(:clean) do
51
+ gateway.run_migrations(target: 0)
52
+ puts "<= #{engine_name}:db:clean executed"
53
+ end
54
+
55
+ Rake.application.last_description = 'Create a migration (parameters: NAME, VERSION)'
56
+ Rake::Task.define_task(:create_migration, %i[name version]) do |_, args|
57
+ name, version = args.values_at(:name, :version)
58
+
59
+ if name.nil?
60
+ puts "No NAME specified. Example usage:
61
+ `rake #{engine_name}:db:create_migration[create_users]`"
62
+ exit
63
+ end
64
+
65
+ path = gateway.migrator.create_file(*[name, version].compact)
66
+ puts "<= migration file created #{path}"
67
+ end
68
+ end
69
+ end
70
+ # rubocop:enable Metrics/AbcSize, Metrics/BlockLength, Metrics/MethodLength
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rom-sql'
4
+ require 'realm/persistence'
5
+
6
+ module Realm
7
+ module ROM
8
+ class ReadOnlyRelationWrapper
9
+ FORBIDDEN_METHODS = (::ROM::SQL::Relation::Writing.instance_methods(false) + [:command]).freeze
10
+
11
+ def initialize(relation)
12
+ @relation = relation
13
+ end
14
+
15
+ def method_missing(symbol, *args)
16
+ raise Persistence::RelationIsReadOnly, @relation if FORBIDDEN_METHODS.include?(symbol)
17
+
18
+ @relation.send(symbol, *args)
19
+ end
20
+
21
+ def respond_to_missing?(symbol)
22
+ !FORBIDDEN_METHODS.include?(symbol) && @relation.respond_to?(symbol)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'realm/persistence'
4
+ require_relative 'read_only_relation_wrapper'
5
+
6
+ module Realm
7
+ module ROM
8
+ class ReadOnlyRepositoryWrapper
9
+ def initialize(repo)
10
+ @repo = repo.clone
11
+ @repo.define_singleton_method(:root) { ReadOnlyRelationWrapper.new(super()) }
12
+ end
13
+
14
+ def method_missing(name, *args, &block)
15
+ @repo.send(name, *args, &block)
16
+ rescue Persistence::RelationIsReadOnly
17
+ raise Persistence::RepositoryIsReadOnly, @repo
18
+ end
19
+
20
+ def respond_to_missing?(*args)
21
+ @repo.respond_to?(*args)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/string'
4
+ require 'rom-repository'
5
+ require 'rom-sql'
6
+ require 'realm/error'
7
+ require_relative 'read_only_repository_wrapper'
8
+
9
+ module Realm
10
+ module ROM
11
+ class Repository < ::ROM::Repository::Root
12
+ # Prevents leaking of persistence details into business logic
13
+ class Isolated
14
+ def initialize(repo)
15
+ @repo = repo
16
+ end
17
+
18
+ def method_missing(*args, &block)
19
+ result = @repo.send(*args, &block)
20
+ result.is_a?(::ROM::Relation) ? result.to_a : result
21
+ rescue ::ROM::SQL::UniqueConstraintError
22
+ raise Realm::UniqueConstraintError
23
+ end
24
+
25
+ def respond_to_missing?(*args)
26
+ @repo.respond_to?(*args)
27
+ end
28
+ end
29
+
30
+ def self.new(*)
31
+ Isolated.new(super)
32
+ end
33
+
34
+ def self.repo_name(value = :not_provided)
35
+ @repo_name = value.to_sym unless value == :not_provided
36
+ @repo_name = name.demodulize.underscore unless defined?(@repo_name)
37
+ @repo_name
38
+ end
39
+
40
+ def readonly
41
+ @readonly ||= ROM::ReadOnlyRepositoryWrapper.new(self)
42
+ end
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: realm-rom
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.7.0
5
+ platform: ruby
6
+ authors:
7
+ - developers@reevoo.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: realm-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rom
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rom-sql
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pg
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - lib/realm-rom.rb
132
+ - lib/realm/rom/gateway.rb
133
+ - lib/realm/rom/plugin.rb
134
+ - lib/realm/rom/rake_tasks.rb
135
+ - lib/realm/rom/read_only_relation_wrapper.rb
136
+ - lib/realm/rom/read_only_repository_wrapper.rb
137
+ - lib/realm/rom/repository.rb
138
+ homepage:
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: 2.7.0
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubygems_version: 3.1.6
158
+ signing_key:
159
+ specification_version: 4
160
+ summary: ROM SQL persistence plugin for Realm
161
+ test_files: []