hanami-model 1.3.2 → 1.3.3
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 +4 -4
- data/CHANGELOG.md +5 -0
- data/LICENSE.md +1 -1
- data/README.md +6 -3
- data/hanami-model.gemspec +25 -21
- data/lib/hanami-model.rb +3 -1
- data/lib/hanami/entity.rb +5 -3
- data/lib/hanami/entity/schema.rb +9 -7
- data/lib/hanami/model.rb +14 -12
- data/lib/hanami/model/association.rb +7 -7
- data/lib/hanami/model/associations/belongs_to.rb +3 -1
- data/lib/hanami/model/associations/dsl.rb +2 -2
- data/lib/hanami/model/associations/has_many.rb +10 -8
- data/lib/hanami/model/associations/has_one.rb +9 -7
- data/lib/hanami/model/associations/many_to_many.rb +9 -11
- data/lib/hanami/model/configuration.rb +15 -13
- data/lib/hanami/model/configurator.rb +5 -3
- data/lib/hanami/model/entity_name.rb +3 -1
- data/lib/hanami/model/error.rb +9 -7
- data/lib/hanami/model/mapped_relation.rb +4 -2
- data/lib/hanami/model/mapping.rb +3 -1
- data/lib/hanami/model/migration.rb +2 -0
- data/lib/hanami/model/migrator.rb +7 -5
- data/lib/hanami/model/migrator/adapter.rb +13 -11
- data/lib/hanami/model/migrator/connection.rb +10 -8
- data/lib/hanami/model/migrator/logger.rb +3 -1
- data/lib/hanami/model/migrator/mysql_adapter.rb +23 -13
- data/lib/hanami/model/migrator/postgres_adapter.rb +30 -28
- data/lib/hanami/model/migrator/sqlite_adapter.rb +7 -9
- data/lib/hanami/model/plugins.rb +5 -3
- data/lib/hanami/model/plugins/mapping.rb +2 -0
- data/lib/hanami/model/plugins/schema.rb +2 -0
- data/lib/hanami/model/plugins/timestamps.rb +2 -0
- data/lib/hanami/model/relation_name.rb +4 -2
- data/lib/hanami/model/sql.rb +9 -7
- data/lib/hanami/model/sql/console.rb +10 -8
- data/lib/hanami/model/sql/consoles/abstract.rb +3 -1
- data/lib/hanami/model/sql/consoles/mysql.rb +4 -2
- data/lib/hanami/model/sql/consoles/postgresql.rb +10 -8
- data/lib/hanami/model/sql/consoles/sqlite.rb +6 -4
- data/lib/hanami/model/sql/entity/schema.rb +6 -4
- data/lib/hanami/model/sql/types.rb +7 -5
- data/lib/hanami/model/sql/types/schema/coercions.rb +5 -4
- data/lib/hanami/model/types.rb +4 -4
- data/lib/hanami/model/version.rb +3 -1
- data/lib/hanami/repository.rb +20 -31
- metadata +34 -3
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "pathname"
|
4
|
+
require "hanami/utils"
|
5
|
+
require "English"
|
4
6
|
|
5
7
|
module Hanami
|
6
8
|
module Model
|
@@ -71,7 +73,7 @@ module Hanami
|
|
71
73
|
# @api private
|
72
74
|
def path
|
73
75
|
root.join(
|
74
|
-
@connection.uri.sub(/\A(jdbc:sqlite:\/\/|sqlite:\/\/)/,
|
76
|
+
@connection.uri.sub(/\A(jdbc:sqlite:\/\/|sqlite:\/\/)/, "")
|
75
77
|
)
|
76
78
|
end
|
77
79
|
|
@@ -104,13 +106,11 @@ module Hanami
|
|
104
106
|
# @since 0.4.0
|
105
107
|
# @api private
|
106
108
|
#
|
107
|
-
# rubocop:disable Metrics/AbcSize
|
108
|
-
# rubocop:disable Metrics/MethodLength
|
109
109
|
def dump_migrations_data
|
110
110
|
execute "sqlite3 #{escape(path)} .dump" do |stdout|
|
111
111
|
begin
|
112
112
|
contents = stdout.read.split($INPUT_RECORD_SEPARATOR)
|
113
|
-
contents = contents.grep(/^INSERT INTO "
|
113
|
+
contents = contents.grep(/^INSERT INTO "?#{migrations_table}"?/)
|
114
114
|
|
115
115
|
::File.open(schema, ::File::CREAT | ::File::BINARY | ::File::WRONLY | ::File::APPEND) do |file|
|
116
116
|
file.write(contents.join($INPUT_RECORD_SEPARATOR))
|
@@ -120,8 +120,6 @@ module Hanami
|
|
120
120
|
end
|
121
121
|
end
|
122
122
|
end
|
123
|
-
# rubocop:enable Metrics/MethodLength
|
124
|
-
# rubocop:enable Metrics/AbcSize
|
125
123
|
end
|
126
124
|
end
|
127
125
|
end
|
data/lib/hanami/model/plugins.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Hanami
|
2
4
|
module Model
|
3
5
|
# Plugins to extend read/write operations from/to the database
|
@@ -17,9 +19,9 @@ module Hanami
|
|
17
19
|
end
|
18
20
|
end
|
19
21
|
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
22
|
+
require "hanami/model/plugins/mapping"
|
23
|
+
require "hanami/model/plugins/schema"
|
24
|
+
require "hanami/model/plugins/timestamps"
|
23
25
|
end
|
24
26
|
end
|
25
27
|
end
|
data/lib/hanami/model/sql.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rom-sql"
|
4
|
+
require "hanami/utils"
|
3
5
|
|
4
6
|
module Hanami
|
5
7
|
# Hanami::Model migrations
|
6
8
|
module Model
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
9
|
+
require "hanami/model/error"
|
10
|
+
require "hanami/model/association"
|
11
|
+
require "hanami/model/migration"
|
10
12
|
|
11
13
|
# Define a migration
|
12
14
|
#
|
@@ -53,8 +55,8 @@ module Hanami
|
|
53
55
|
#
|
54
56
|
# @since 0.7.0
|
55
57
|
module Sql
|
56
|
-
require
|
57
|
-
require
|
58
|
+
require "hanami/model/sql/types"
|
59
|
+
require "hanami/model/sql/entity/schema"
|
58
60
|
|
59
61
|
# Returns a SQL fragment that references a database function by the given name
|
60
62
|
# This is useful for database migrations
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "uri"
|
2
4
|
|
3
5
|
module Hanami
|
4
6
|
module Model
|
@@ -24,16 +26,16 @@ module Hanami
|
|
24
26
|
|
25
27
|
# @since 0.7.0
|
26
28
|
# @api private
|
27
|
-
def console
|
29
|
+
def console
|
28
30
|
case @uri.scheme
|
29
|
-
when
|
30
|
-
require
|
31
|
+
when "sqlite"
|
32
|
+
require "hanami/model/sql/consoles/sqlite"
|
31
33
|
Sql::Consoles::Sqlite.new(@uri)
|
32
|
-
when
|
33
|
-
require
|
34
|
+
when "postgres", "postgresql"
|
35
|
+
require "hanami/model/sql/consoles/postgresql"
|
34
36
|
Sql::Consoles::Postgresql.new(@uri)
|
35
|
-
when
|
36
|
-
require
|
37
|
+
when "mysql", "mysql2"
|
38
|
+
require "hanami/model/sql/consoles/mysql"
|
37
39
|
Sql::Consoles::Mysql.new(@uri)
|
38
40
|
end
|
39
41
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "abstract"
|
2
4
|
|
3
5
|
module Hanami
|
4
6
|
module Model
|
@@ -11,7 +13,7 @@ module Hanami
|
|
11
13
|
class Mysql < Abstract
|
12
14
|
# @since 0.7.0
|
13
15
|
# @api private
|
14
|
-
COMMAND =
|
16
|
+
COMMAND = "mysql"
|
15
17
|
|
16
18
|
# @since 0.7.0
|
17
19
|
# @api private
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "abstract"
|
4
|
+
require "cgi"
|
3
5
|
|
4
6
|
module Hanami
|
5
7
|
module Model
|
@@ -12,11 +14,11 @@ module Hanami
|
|
12
14
|
class Postgresql < Abstract
|
13
15
|
# @since 0.7.0
|
14
16
|
# @api private
|
15
|
-
COMMAND =
|
17
|
+
COMMAND = "psql"
|
16
18
|
|
17
19
|
# @since 0.7.0
|
18
20
|
# @api private
|
19
|
-
PASSWORD =
|
21
|
+
PASSWORD = "PGPASSWORD"
|
20
22
|
|
21
23
|
# @since 0.7.0
|
22
24
|
# @api private
|
@@ -48,22 +50,22 @@ module Hanami
|
|
48
50
|
# @since 0.7.0
|
49
51
|
# @api private
|
50
52
|
def port
|
51
|
-
port = query[
|
53
|
+
port = query["port"] || @uri.port
|
52
54
|
" -p #{port}" if port
|
53
55
|
end
|
54
56
|
|
55
57
|
# @since 0.7.0
|
56
58
|
# @api private
|
57
59
|
def username
|
58
|
-
username = query[
|
60
|
+
username = query["user"] || @uri.user
|
59
61
|
" -U #{username}" if username
|
60
62
|
end
|
61
63
|
|
62
64
|
# @since 0.7.0
|
63
65
|
# @api private
|
64
66
|
def configure_password
|
65
|
-
password = query[
|
66
|
-
ENV[PASSWORD] = CGI.unescape(query[
|
67
|
+
password = query["password"] || @uri.password
|
68
|
+
ENV[PASSWORD] = CGI.unescape(query["password"] || @uri.password) if password
|
67
69
|
end
|
68
70
|
|
69
71
|
# @since 1.1.0
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "abstract"
|
4
|
+
require "shellwords"
|
3
5
|
|
4
6
|
module Hanami
|
5
7
|
module Model
|
@@ -12,12 +14,12 @@ module Hanami
|
|
12
14
|
class Sqlite < Abstract
|
13
15
|
# @since 0.7.0
|
14
16
|
# @api private
|
15
|
-
COMMAND =
|
17
|
+
COMMAND = "sqlite3"
|
16
18
|
|
17
19
|
# @since 0.7.0
|
18
20
|
# @api private
|
19
21
|
def connection_string
|
20
|
-
concat(command,
|
22
|
+
concat(command, " ", host, database)
|
21
23
|
end
|
22
24
|
|
23
25
|
private
|
@@ -1,6 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/entity/schema"
|
4
|
+
require "hanami/model/types"
|
5
|
+
require "hanami/model/association"
|
4
6
|
|
5
7
|
module Hanami
|
6
8
|
module Model
|
@@ -52,7 +54,7 @@ module Hanami
|
|
52
54
|
|
53
55
|
# @since 1.0.1
|
54
56
|
# @api private
|
55
|
-
|
57
|
+
alias_method :[], :call
|
56
58
|
|
57
59
|
# Check if the attribute is known
|
58
60
|
#
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/model/types"
|
4
|
+
require "rom/types"
|
3
5
|
|
4
6
|
module Hanami
|
5
7
|
module Model
|
@@ -14,7 +16,7 @@ module Hanami
|
|
14
16
|
#
|
15
17
|
# @since 0.7.0
|
16
18
|
module Schema
|
17
|
-
require
|
19
|
+
require "hanami/model/sql/types/schema/coercions"
|
18
20
|
|
19
21
|
String = Types::Optional::Coercible::String
|
20
22
|
|
@@ -93,8 +95,8 @@ module Hanami
|
|
93
95
|
# @since 1.0.2
|
94
96
|
# @api private
|
95
97
|
def self.pg_json?(pristine)
|
96
|
-
pristine == pg_json_pristines[
|
97
|
-
pristine == pg_json_pristines[
|
98
|
+
pristine == pg_json_pristines["JSONB"] || # rubocop:disable Style/MultipleComparison
|
99
|
+
pristine == pg_json_pristines["JSON"]
|
98
100
|
end
|
99
101
|
|
100
102
|
private_class_method :pg_json?
|
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "hanami/utils/string"
|
4
|
+
require "hanami/utils/hash"
|
3
5
|
|
4
6
|
module Hanami
|
5
7
|
module Model
|
@@ -12,7 +14,6 @@ module Hanami
|
|
12
14
|
# @api private
|
13
15
|
#
|
14
16
|
# rubocop:disable Metrics/ModuleLength
|
15
|
-
# rubocop:disable Metrics/MethodLength
|
16
17
|
module Coercions
|
17
18
|
# Coerces given argument into Integer
|
18
19
|
#
|
@@ -215,7 +216,7 @@ module Hanami
|
|
215
216
|
end
|
216
217
|
end
|
217
218
|
end
|
218
|
-
|
219
|
+
|
219
220
|
# rubocop:enable Metrics/ModuleLength
|
220
221
|
end
|
221
222
|
end
|
data/lib/hanami/model/types.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rom/types"
|
2
4
|
|
3
5
|
module Hanami
|
4
6
|
module Model
|
@@ -17,7 +19,6 @@ module Hanami
|
|
17
19
|
# Class level interface
|
18
20
|
#
|
19
21
|
# @since 0.7.0
|
20
|
-
# rubocop:disable Naming/MethodName
|
21
22
|
module ClassMethods
|
22
23
|
# Define an entity of the given type
|
23
24
|
#
|
@@ -77,7 +78,6 @@ module Hanami
|
|
77
78
|
Types::Array.member(type)
|
78
79
|
end
|
79
80
|
end
|
80
|
-
# rubocop:enable Naming/MethodName
|
81
81
|
|
82
82
|
# Types for schema definitions
|
83
83
|
#
|
@@ -101,7 +101,7 @@ module Hanami
|
|
101
101
|
def call(value)
|
102
102
|
return if value.nil?
|
103
103
|
|
104
|
-
if valid?(value)
|
104
|
+
if valid?(value)
|
105
105
|
coerce(value)
|
106
106
|
else
|
107
107
|
raise TypeError.new("#{value.inspect} must be coercible into #{object}")
|
data/lib/hanami/model/version.rb
CHANGED
data/lib/hanami/repository.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rom-repository"
|
4
|
+
require "hanami/model/entity_name"
|
5
|
+
require "hanami/model/relation_name"
|
6
|
+
require "hanami/model/mapped_relation"
|
7
|
+
require "hanami/model/associations/dsl"
|
8
|
+
require "hanami/model/association"
|
9
|
+
require "hanami/utils/class"
|
10
|
+
require "hanami/utils/class_attribute"
|
11
|
+
require "hanami/utils/io"
|
10
12
|
|
11
13
|
module Hanami
|
12
14
|
# Mediates between the entities and the persistence layer, by offering an API
|
@@ -107,7 +109,7 @@ module Hanami
|
|
107
109
|
# @see Hanami::Entity
|
108
110
|
# @see http://martinfowler.com/eaaCatalog/repository.html
|
109
111
|
# @see http://en.wikipedia.org/wiki/Dependency_inversion_principle
|
110
|
-
class Repository < ROM::Repository::Root
|
112
|
+
class Repository < ROM::Repository::Root
|
111
113
|
# Plugins for database commands
|
112
114
|
#
|
113
115
|
# @since 0.7.0
|
@@ -155,7 +157,6 @@ module Hanami
|
|
155
157
|
def command(*args, **opts, &block)
|
156
158
|
opts[:use] = COMMAND_PLUGINS | Array(opts[:use])
|
157
159
|
opts[:mapper] = opts.fetch(:mapper, Model::MappedRelation.mapper_name)
|
158
|
-
|
159
160
|
super(*args, **opts, &block)
|
160
161
|
end
|
161
162
|
|
@@ -167,8 +168,6 @@ module Hanami
|
|
167
168
|
# @since 0.7.0
|
168
169
|
# @api private
|
169
170
|
#
|
170
|
-
# rubocop:disable Metrics/MethodLength
|
171
|
-
# rubocop:disable Metrics/AbcSize
|
172
171
|
def self.define_relation
|
173
172
|
a = @associations
|
174
173
|
s = @schema
|
@@ -191,8 +190,6 @@ module Hanami
|
|
191
190
|
end
|
192
191
|
}, __FILE__, __LINE__ - 4
|
193
192
|
end
|
194
|
-
# rubocop:enable Metrics/AbcSize
|
195
|
-
# rubocop:enable Metrics/MethodLength
|
196
193
|
|
197
194
|
# Defines the mapping between a database table and an entity.
|
198
195
|
#
|
@@ -201,8 +198,6 @@ module Hanami
|
|
201
198
|
# @since 0.7.0
|
202
199
|
# @api private
|
203
200
|
#
|
204
|
-
# rubocop:disable Metrics/MethodLength
|
205
|
-
# rubocop:disable Metrics/AbcSize
|
206
201
|
def self.define_mapping
|
207
202
|
self.entity = Utils::Class.load!(entity_name)
|
208
203
|
e = entity
|
@@ -219,8 +214,6 @@ module Hanami
|
|
219
214
|
configuration.define_mappings(root, &blk)
|
220
215
|
configuration.register_entity(relation, entity_name.underscore, e)
|
221
216
|
end
|
222
|
-
# rubocop:enable Metrics/AbcSize
|
223
|
-
# rubocop:enable Metrics/MethodLength
|
224
217
|
|
225
218
|
# It defines associations, by adding relations to the repository
|
226
219
|
#
|
@@ -302,8 +295,6 @@ module Hanami
|
|
302
295
|
# @since 0.7.0
|
303
296
|
# @api private
|
304
297
|
#
|
305
|
-
# rubocop:disable Metrics/MethodLength
|
306
|
-
# rubocop:disable Metrics/AbcSize
|
307
298
|
def self.inherited(klass)
|
308
299
|
klass.class_eval do
|
309
300
|
include Utils::ClassAttribute
|
@@ -332,8 +323,6 @@ module Hanami
|
|
332
323
|
|
333
324
|
Hanami::Model.repositories << klass
|
334
325
|
end
|
335
|
-
# rubocop:enable Metrics/AbcSize
|
336
|
-
# rubocop:enable Metrics/MethodLength
|
337
326
|
|
338
327
|
# Extend commands from ROM::Repository with error management
|
339
328
|
#
|
@@ -358,8 +347,8 @@ module Hanami
|
|
358
347
|
# entity.id # => nil - It doesn't mutate original entity
|
359
348
|
def create(*args)
|
360
349
|
super
|
361
|
-
rescue =>
|
362
|
-
raise Hanami::Model::Error.for(
|
350
|
+
rescue => exception
|
351
|
+
raise Hanami::Model::Error.for(exception)
|
363
352
|
end
|
364
353
|
|
365
354
|
# Update a record
|
@@ -387,8 +376,8 @@ module Hanami
|
|
387
376
|
# entity.id # => nil - It doesn't mutate original entity
|
388
377
|
def update(*args)
|
389
378
|
super
|
390
|
-
rescue =>
|
391
|
-
raise Hanami::Model::Error.for(
|
379
|
+
rescue => exception
|
380
|
+
raise Hanami::Model::Error.for(exception)
|
392
381
|
end
|
393
382
|
|
394
383
|
# Delete a record
|
@@ -406,8 +395,8 @@ module Hanami
|
|
406
395
|
# user = repository.delete(user.id)
|
407
396
|
def delete(*args)
|
408
397
|
super
|
409
|
-
rescue =>
|
410
|
-
raise Hanami::Model::Error.for(
|
398
|
+
rescue => exception
|
399
|
+
raise Hanami::Model::Error.for(exception)
|
411
400
|
end
|
412
401
|
end
|
413
402
|
|
@@ -436,8 +425,8 @@ module Hanami
|
|
436
425
|
# user = repository.find(user.id)
|
437
426
|
def find(id)
|
438
427
|
root.by_pk(id).as(:entity).one
|
439
|
-
rescue =>
|
440
|
-
raise Hanami::Model::Error.for(
|
428
|
+
rescue => exception
|
429
|
+
raise Hanami::Model::Error.for(exception)
|
441
430
|
end
|
442
431
|
|
443
432
|
# Return all the records for the relation
|