skyward-orm 0.3.0 → 0.4.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.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +200 -31
  3. data/lib/skyward/orm/adapters/postgresql.rb +140 -38
  4. data/lib/skyward/orm/adapters/sqlite/constraints.rb +18 -0
  5. data/lib/skyward/orm/adapters/sqlite.rb +130 -21
  6. data/lib/skyward/orm/adapters.rb +12 -2
  7. data/lib/skyward/orm/catalog/column.rb +2 -1
  8. data/lib/skyward/orm/cli/boot_file.rb +2 -2
  9. data/lib/skyward/orm/cli/migrate.rb +1 -1
  10. data/lib/skyward/orm/cli/migration.rb +1 -1
  11. data/lib/skyward/orm/cli/scaffold/source.rb +304 -0
  12. data/lib/skyward/orm/cli/scaffold.rb +70 -0
  13. data/lib/skyward/orm/cli/status.rb +5 -3
  14. data/lib/skyward/orm/commands.rb +12 -0
  15. data/lib/skyward/orm/connection/instrumented.rb +69 -0
  16. data/lib/skyward/orm/connection/session.rb +74 -5
  17. data/lib/skyward/orm/{connection/database → database}/schema_check.rb +20 -8
  18. data/lib/skyward/orm/{connection/database.rb → database.rb} +15 -15
  19. data/lib/skyward/orm/execution.rb +9 -0
  20. data/lib/skyward/orm/migrations/alter_table.rb +6 -2
  21. data/lib/skyward/orm/migrations/definitions.rb +7 -4
  22. data/lib/skyward/orm/migrations/diff.rb +154 -30
  23. data/lib/skyward/orm/migrations/rename_constraint.rb +8 -0
  24. data/lib/skyward/orm/migrations/runner.rb +13 -3
  25. data/lib/skyward/orm/migrations/script.rb +60 -20
  26. data/lib/skyward/orm/migrations/status.rb +32 -3
  27. data/lib/skyward/orm/migrations/summary.rb +3 -6
  28. data/lib/skyward/orm/migrations/versions.rb +1 -1
  29. data/lib/skyward/orm/migrations.rb +1 -1
  30. data/lib/skyward/orm/projection.rb +23 -4
  31. data/lib/skyward/orm/queries/clauses.rb +117 -32
  32. data/lib/skyward/orm/record.rb +23 -1
  33. data/lib/skyward/orm/{placeholders.rb → statements/placeholders.rb} +1 -1
  34. data/lib/skyward/orm/statements.rb +171 -30
  35. data/lib/skyward/orm/tables/column.rb +2 -1
  36. data/lib/skyward/orm/tables/column_types.rb +62 -15
  37. data/lib/skyward/orm/tables/declarations.rb +38 -10
  38. data/lib/skyward/orm/{members.rb → tables/members.rb} +1 -1
  39. data/lib/skyward/orm/tables/sources.rb +63 -0
  40. data/lib/skyward/orm/tables/validation.rb +12 -3
  41. data/lib/skyward/orm/tables/values.rb +115 -5
  42. data/lib/skyward/orm/tables.rb +2 -1
  43. data/lib/skyward/orm/transactional.rb +2 -2
  44. data/lib/skyward/orm/version.rb +1 -1
  45. data/lib/skyward/orm.rb +44 -10
  46. data/sig/generated/skyward/orm/adapters/postgresql.rbs +53 -14
  47. data/sig/generated/skyward/orm/adapters/sqlite/constraints.rbs +6 -0
  48. data/sig/generated/skyward/orm/adapters/sqlite.rbs +67 -7
  49. data/sig/generated/skyward/orm/adapters.rbs +3 -0
  50. data/sig/generated/skyward/orm/catalog/column.rbs +6 -4
  51. data/sig/generated/skyward/orm/cli/boot_file.rbs +4 -4
  52. data/sig/generated/skyward/orm/cli/migrate.rbs +2 -2
  53. data/sig/generated/skyward/orm/cli/migration.rbs +2 -2
  54. data/sig/generated/skyward/orm/cli/scaffold/source.rbs +147 -0
  55. data/sig/generated/skyward/orm/cli/scaffold.rbs +28 -0
  56. data/sig/generated/skyward/orm/cli/status.rbs +2 -2
  57. data/sig/generated/skyward/orm/connection/instrumented.rbs +45 -0
  58. data/sig/generated/skyward/orm/connection/session.rbs +40 -6
  59. data/sig/generated/skyward/orm/{connection/database → database}/schema_check.rbs +11 -4
  60. data/sig/generated/skyward/orm/{connection/database.rbs → database.rbs} +4 -5
  61. data/sig/generated/skyward/orm/execution.rbs +18 -0
  62. data/sig/generated/skyward/orm/migrations/alter_table.rbs +11 -5
  63. data/sig/generated/skyward/orm/migrations/definitions.rbs +2 -2
  64. data/sig/generated/skyward/orm/migrations/diff.rbs +77 -11
  65. data/sig/generated/skyward/orm/migrations/rename_constraint.rbs +16 -0
  66. data/sig/generated/skyward/orm/migrations/runner.rbs +5 -0
  67. data/sig/generated/skyward/orm/migrations/script.rbs +32 -8
  68. data/sig/generated/skyward/orm/migrations/status.rbs +15 -2
  69. data/sig/generated/skyward/orm/migrations/summary.rbs +0 -1
  70. data/sig/generated/skyward/orm/migrations/versions.rbs +2 -2
  71. data/sig/generated/skyward/orm/migrations.rbs +1 -1
  72. data/sig/generated/skyward/orm/projection.rbs +10 -2
  73. data/sig/generated/skyward/orm/queries/clauses.rbs +60 -24
  74. data/sig/generated/skyward/orm/record.rbs +8 -0
  75. data/sig/generated/skyward/orm/{placeholders.rbs → statements/placeholders.rbs} +2 -2
  76. data/sig/generated/skyward/orm/statements.rbs +83 -5
  77. data/sig/generated/skyward/orm/tables/column.rbs +6 -4
  78. data/sig/generated/skyward/orm/tables/column_types.rbs +18 -4
  79. data/sig/generated/skyward/orm/tables/declarations.rbs +15 -1
  80. data/sig/generated/skyward/orm/{members.rbs → tables/members.rbs} +2 -2
  81. data/sig/generated/skyward/orm/tables/sources.rbs +32 -0
  82. data/sig/generated/skyward/orm/tables/values.rbs +41 -1
  83. data/sig/generated/skyward/orm/tables.rbs +3 -1
  84. data/sig/generated/skyward/orm.rbs +6 -6
  85. data/sig/manual/pg.rbs +12 -0
  86. data/sig/manual/skyward/orm/cli/scaffold/source.rbs +3 -0
  87. data/sig/manual/skyward/orm/cli/scaffold.rbs +3 -0
  88. data/sig/manual/skyward/orm/database/schema_check.rbs +3 -0
  89. data/sig/manual/skyward/orm/statements/placeholders.rbs +3 -0
  90. data/sig/manual/skyward/orm/tables/members.rbs +3 -0
  91. data/sig/manual/skyward/orm/tables/sources.rbs +3 -0
  92. data/sig/manual/skyward/orm.rbs +32 -5
  93. metadata +43 -14
  94. data/sig/manual/skyward/orm/connection/database/schema_check.rbs +0 -3
  95. data/sig/manual/skyward/orm/members.rbs +0 -3
  96. data/sig/manual/skyward/orm/placeholders.rbs +0 -3
@@ -6,19 +6,40 @@ module Skyward::ORM::Tables::Values
6
6
  ACCEPTED = {
7
7
  integer: [Integer],
8
8
  float: [Float, Integer],
9
+ decimal: [BigDecimal, Integer],
9
10
  string: [String],
10
11
  bool: [TrueClass, FalseClass],
11
12
  date: [Date],
12
- time: [Time]
13
+ time: [Time],
14
+ uuid: [String],
15
+ int32: [Integer],
16
+ int16: [Integer]
13
17
  }.freeze #: Hash[Symbol, Array[Module]]
14
18
 
15
19
  WRITTEN = {
16
20
  integer: "Integer",
17
21
  float: "Float",
22
+ decimal: "BigDecimal",
18
23
  string: "String",
19
24
  bool: "bool",
20
25
  date: "Date",
21
- time: "Time"
26
+ time: "Time",
27
+ uuid: "uuid",
28
+ int32: "int32",
29
+ int16: "int16",
30
+ json: "Hash[String, untyped]"
31
+ }.freeze #: Hash[Symbol, String]
32
+
33
+ UUID = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/ #: Regexp
34
+
35
+ RANGES = {
36
+ int32: -(2**31)...(2**31),
37
+ int16: -(2**15)...(2**15)
38
+ }.freeze #: Hash[Symbol, Range[Integer]]
39
+
40
+ WHOLE = {
41
+ json: "a #{WRITTEN.fetch(:json)}",
42
+ array: "an Array"
22
43
  }.freeze #: Hash[Symbol, String]
23
44
 
24
45
  UNTYPED = "untyped, which takes any value but a Hash or a nested Array" #: String
@@ -30,6 +51,15 @@ module Skyward::ORM::Tables::Values
30
51
  # @rbs value: untyped
31
52
  # @rbs return: String?
32
53
  def mismatch(table, column, value)
54
+ # PostgreSQL compares jsonb by value, but SQLite compares the JSON's text,
55
+ # so the two would match different rows for anything but NULL. An Array
56
+ # would otherwise mean IN, which on an Array column could mean either.
57
+ whole = WHOLE[column.type]
58
+ if whole && !value.nil?
59
+ return "#{table}.#{column.name} is #{whole} column, which where matches only to nil. " \
60
+ "Match its contents with an SQL string."
61
+ end
62
+
33
63
  refused = candidates(value).reject { |candidate| accepts?(column, candidate) }
34
64
  return if refused.empty?
35
65
 
@@ -37,12 +67,15 @@ module Skyward::ORM::Tables::Values
37
67
  end
38
68
 
39
69
  # A write binds each value to one placeholder, so an Array or a Range, which
40
- # a where unpacks, is never one value, and the driver can't bind a Range.
70
+ # a where unpacks, is never one value, but for an Array column's whole Array,
71
+ # and the driver can't bind a Range.
41
72
  # @rbs table: singleton(Skyward::ORM::Record)
42
73
  # @rbs column: Skyward::ORM::Tables::Column
43
74
  # @rbs value: untyped
44
75
  # @rbs return: String?
45
76
  def write_mismatch(table, column, value)
77
+ return json_mismatch(table, column, value) if column.type == :json && !value.nil?
78
+ return array_mismatch(table, column, value) if column.type == :array && !value.nil?
46
79
  return if accepts?(column, value) && !value.is_a?(Range)
47
80
 
48
81
  refusal(table, column, value, UNTYPED_WRITE)
@@ -50,6 +83,67 @@ module Skyward::ORM::Tables::Values
50
83
 
51
84
  private
52
85
 
86
+ # SQLite holds the Array as JSON, which has no Infinity, so neither adapter
87
+ # takes one in an Array, though a Float column does.
88
+ # @rbs table: singleton(Skyward::ORM::Record)
89
+ # @rbs column: Skyward::ORM::Tables::Column
90
+ # @rbs value: untyped
91
+ # @rbs return: String?
92
+ def array_mismatch(table, column, value)
93
+ return refusal(table, column, value, UNTYPED_WRITE) unless value.is_a?(Array)
94
+
95
+ element = item(column)
96
+ index = value.index do |entry|
97
+ !accepts?(element, entry) || (entry.is_a?(Float) && entry.infinite?)
98
+ end
99
+ return unless index
100
+
101
+ "#{refusal(table, column, value.fetch(index), UNTYPED_WRITE)} at #{column.name}[#{index}]"
102
+ end
103
+
104
+ # @rbs column: Skyward::ORM::Tables::Column
105
+ # @rbs return: Skyward::ORM::Tables::Column
106
+ def item(column)
107
+ # Only an :array column, which always has an element, has items.
108
+ type = column.element #: Skyward::ORM::Tables::column_type
109
+ column.with(type:, nullable: false, element: nil)
110
+ end
111
+
112
+ # A row reads back == to the one written only when its JSON holds each value
113
+ # as it is: a Symbol or a Time would come back a String.
114
+ # @rbs table: singleton(Skyward::ORM::Record)
115
+ # @rbs column: Skyward::ORM::Tables::Column
116
+ # @rbs value: untyped
117
+ # @rbs return: String?
118
+ def json_mismatch(table, column, value)
119
+ return refusal(table, column, value, UNTYPED_WRITE) unless value.is_a?(Hash)
120
+
121
+ found = stray(value, column.name.name)
122
+ return unless found
123
+
124
+ refused, at = found
125
+ "#{refusal(table, column, refused, UNTYPED_WRITE)} #{at}"
126
+ end
127
+
128
+ # The first value JSON doesn't hold as it is, and where it is.
129
+ # @rbs value: untyped
130
+ # @rbs path: String
131
+ # @rbs return: [untyped, String]?
132
+ def stray(value, path)
133
+ case value
134
+ when nil, true, false, Integer, String then nil
135
+ when Float then [value, "at #{path}"] unless value.finite?
136
+ when Array then value.each_with_index.lazy.filter_map { |item, index| stray(item, "#{path}[#{index}]") }.first
137
+ when Hash
138
+ value.lazy.filter_map do |key, item|
139
+ if key.is_a?(String) then stray(item, "#{path}[#{key.inspect}]")
140
+ else [key, "as a key of #{path}"] #: [untyped, String]
141
+ end
142
+ end.first
143
+ else [value, "at #{path}"]
144
+ end
145
+ end
146
+
53
147
  # @rbs table: singleton(Skyward::ORM::Record)
54
148
  # @rbs column: Skyward::ORM::Tables::Column
55
149
  # @rbs value: untyped
@@ -82,8 +176,23 @@ module Skyward::ORM::Tables::Values
82
176
  # A DateTime is a Date, but it encodes with its time, so it never equals a
83
177
  # stored date.
84
178
  elsif value.is_a?(DateTime) then false
85
- # SQLite stores NaN as NULL.
86
- else ACCEPTED.fetch(column.type).any? { |klass| value.is_a?(klass) } && !(value.is_a?(Float) && value.nan?)
179
+ else ACCEPTED.fetch(column.type).any? { |klass| value.is_a?(klass) } && holds?(column.type, value)
180
+ end
181
+ end
182
+
183
+ # A value of the right class that the column can't store would only fail in
184
+ # the database, with an error that doesn't name the column.
185
+ # @rbs type: Skyward::ORM::Tables::column_type
186
+ # @rbs value: untyped
187
+ # @rbs return: bool
188
+ def holds?(type, value)
189
+ case type
190
+ # SQLite stores a Float NaN as NULL, and a BigDecimal NaN never equals
191
+ # itself, so a row holding one would never be == to itself.
192
+ when :float, :decimal then value.is_a?(Integer) || !value.nan?
193
+ when :uuid then UUID.match?(value)
194
+ when :int32, :int16 then RANGES.fetch(type).cover?(value)
195
+ else true
87
196
  end
88
197
  end
89
198
 
@@ -96,6 +205,7 @@ module Skyward::ORM::Tables::Values
96
205
  when :enum
97
206
  symbols = column.symbols.map(&:inspect).join(" | ")
98
207
  column.nullable ? "(#{symbols})?" : symbols
208
+ when :array then "Array[#{written(item(column), untyped)}]#{"?" if column.nullable}"
99
209
  else "#{WRITTEN.fetch(column.type)}#{"?" if column.nullable}"
100
210
  end
101
211
  end
@@ -3,5 +3,6 @@
3
3
  module Skyward::ORM::Tables
4
4
  # @rbs!
5
5
  # type on_delete = :cascade | :restrict | :set_null | :set_default | :no_action
6
- # type column_type = Skyward::Core::Types::scalar | :enum
6
+ # type refinement = :uuid | :int32 | :int16
7
+ # type column_type = Skyward::Core::Types::scalar | :enum | refinement | :json | :array
7
8
  end
@@ -4,7 +4,7 @@
4
4
  module Skyward::ORM::Transactional
5
5
  # @rbs return: void
6
6
  def before_setup
7
- Skyward::ORM::Connection::SESSION.database.hold
7
+ Skyward::ORM::Connection::SESSION.hold
8
8
  super
9
9
  end
10
10
 
@@ -13,6 +13,6 @@ module Skyward::ORM::Transactional
13
13
  def after_teardown
14
14
  super
15
15
  ensure
16
- Skyward::ORM::Connection::SESSION.database.release
16
+ Skyward::ORM::Connection::SESSION.release
17
17
  end
18
18
  end
@@ -4,7 +4,7 @@ module Skyward
4
4
  module ORM
5
5
  # x-release-please-start-version
6
6
 
7
- VERSION = "0.3.0" #: String
7
+ VERSION = "0.4.0" #: String
8
8
 
9
9
  # x-release-please-end
10
10
  end
data/lib/skyward/orm.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "bigdecimal"
3
4
  require "date"
5
+ require "json"
4
6
  require "skyward/core"
5
7
  require "zeitwerk"
6
8
  require_relative "orm/version"
@@ -18,23 +20,55 @@ dir = __dir__ #: String
18
20
  Skyward::Core::Signatures.register(File.expand_path("../../sig", dir))
19
21
 
20
22
  module Skyward
21
- # @rbs url: String
22
- # @rbs signatures: String
23
- # @rbs pool: Integer
24
- # @rbs check: bool
25
- # @rbs return: ORM::Connection::Database
26
- def self.connect(url, signatures: "sig", pool: 5, check: ORM::Connection::SESSION.check?)
27
- column_types = ORM::Tables::ColumnTypes.new(Core::Types::Reader.new(Core::Signatures.load(signatures)))
23
+ # @rbs (String url, ?signatures: String, ?pool: Integer, ?check: bool, ?default: bool,
24
+ # ?instrument: ORM::_Instrument?) -> ORM::Database
25
+ def self.connect(
26
+ url,
27
+ signatures: "sig",
28
+ pool: 5,
29
+ check: ORM::Connection::SESSION.check?,
30
+ default: true,
31
+ instrument: nil
32
+ )
33
+ # Steep knows instrument is one, but untyped code can pass anything.
34
+ unless instrument.nil? || instrument.respond_to?(:call) # steep:ignore NoMethod
35
+ raise Core::BootError,
36
+ "Skyward.connect's instrument: takes something that responds to call, such as a lambda, " \
37
+ "not a #{instrument.class}" # steep:ignore NoMethod
38
+ end
39
+
40
+ reader = Core::Types::Reader.new(Core::Signatures.load(signatures), refinements: ORM::Tables::ColumnTypes::REFINEMENTS)
41
+ column_types = ORM::Tables::ColumnTypes.new(reader)
28
42
  adapter, location = ORM::Adapters.resolve(
29
43
  url,
30
44
  schemes: ORM::Adapters::SCHEMES,
31
45
  requirer: ->(feature) { require(feature) }
32
46
  )
33
- connections = ORM::Connection::Pool.new(adapter.pool_size(location, pool), -> { adapter.connect(location) })
47
+ clock_ms = -> { Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond) }
48
+ opener = lambda do
49
+ connection = adapter.connect(location)
50
+ if instrument
51
+ ORM::Connection::Instrumented.new(connection, instrument, adapter.adapter_name, clock_ms:)
52
+ else
53
+ connection
54
+ end
55
+ end
56
+ connections = ORM::Connection::Pool.new(adapter.pool_size(location, pool), opener)
34
57
  tables = connections.with { |connection| adapter.tables(connection) }
35
- database = ORM::Connection::Database.new(adapter, connections, column_types, tables, check:)
58
+ database = ORM::Database.new(adapter, connections, column_types, tables, check:)
36
59
  database.check(ORM::Tables::Loaded.table_classes(ORM::Record)) if check
37
- ORM::Connection::SESSION.connect(database)
60
+ default ? ORM::Connection::SESSION.connect(database) : database
61
+ end
62
+
63
+ # @rbs [T] (ORM::Database database) { () -> T } -> T
64
+ def self.using(database, &)
65
+ # Steep knows database is one, but untyped code can pass anything.
66
+ unless database.is_a?(ORM::Database) # steep:ignore UnreachableBranch
67
+ raise ArgumentError, "Skyward.using takes a database Skyward.connect returned, not a #{database.class}"
68
+ end
69
+ raise ArgumentError, "Skyward.using takes a block to run on the database" unless block_given?
70
+
71
+ ORM::Connection::SESSION.using(database, &)
38
72
  end
39
73
 
40
74
  # @rbs [T] () { () -> T } -> T
@@ -6,6 +6,9 @@ module Skyward::ORM::Adapters::PostgreSQL
6
6
  # As the catalog spells each type, which compatible? is given.
7
7
  COMPATIBLE: Hash[Skyward::ORM::Tables::column_type, Array[String]]
8
8
 
9
+ # COMPATIBLE read backwards, taking the narrowest type for each.
10
+ COLUMN_TYPES: Hash[String, Skyward::ORM::Tables::column_type]
11
+
9
12
  # The catalog puts a length or precision in parentheses.
10
13
  MODIFIER: Regexp
11
14
 
@@ -16,8 +19,9 @@ module Skyward::ORM::Adapters::PostgreSQL
16
19
 
17
20
  TRAILING_ZEROS: Regexp
18
21
 
19
- # PostgreSQL reads a literal default back cast to its type, as 'normal'::text,
20
- # and quotes a number that isn't a plain integer or decimal, as '-1'::integer.
22
+ # PostgreSQL reads a literal default back cast to its type, as 'normal'::text
23
+ # or '{}'::text[], and quotes a number that isn't a plain integer or decimal,
24
+ # as '-1'::integer.
21
25
  CAST: Regexp
22
26
 
23
27
  NUMERIC: Regexp
@@ -30,6 +34,8 @@ module Skyward::ORM::Adapters::PostgreSQL
30
34
  # Each value comes back as text, and a boolean as t or f.
31
35
  TRUE_TEXT: String
32
36
 
37
+ FALSE_TEXT: String
38
+
33
39
  VIOLATIONS: Hash[String, Skyward::ORM::violation_kind]
34
40
 
35
41
  CONSTRAINT_KINDS: Hash[String, Skyward::ORM::Catalog::constraint_kind]
@@ -37,6 +43,7 @@ module Skyward::ORM::Adapters::PostgreSQL
37
43
  # The tables an unqualified name finds, as every query names them.
38
44
  TABLES: String
39
45
 
46
+ # pg_attrdef holds a generated column's expression as if it were its default.
40
47
  COLUMNS: String
41
48
 
42
49
  # Leaves out the indexes primary keys and unique constraints make, as SQLite's does.
@@ -48,6 +55,9 @@ module Skyward::ORM::Adapters::PostgreSQL
48
55
  # @rbs return: String
49
56
  def driver: () -> String
50
57
 
58
+ # @rbs return: Symbol
59
+ def adapter_name: () -> Symbol
60
+
51
61
  # Values are decoded from PostgreSQL's ISO text, whatever the server's
52
62
  # defaults, and times read and written in UTC. libpq prints each notice the
53
63
  # server sends, so only warnings and worse are sent, or `skyward migrate`
@@ -63,13 +73,17 @@ module Skyward::ORM::Adapters::PostgreSQL
63
73
  # @rbs return: String
64
74
  def placeholder: (Integer position) -> String
65
75
 
76
+ # The protocol counts a statement's parameters in 16 bits.
77
+ # @rbs return: Integer
78
+ def bind_limit: () -> Integer
79
+
66
80
  # PostgreSQL takes an OFFSET with no LIMIT before it.
67
81
  # @rbs return: String?
68
82
  def unlimited: () -> String?
69
83
 
70
- # @rbs type: Skyward::ORM::Tables::column_type
84
+ # @rbs column: Skyward::ORM::Tables::Column
71
85
  # @rbs return: String?
72
- def sql_type: (Skyward::ORM::Tables::column_type type) -> String?
86
+ def sql_type: (Skyward::ORM::Tables::Column column) -> String?
73
87
 
74
88
  # @rbs return: String?
75
89
  def identity: () -> String?
@@ -87,27 +101,46 @@ module Skyward::ORM::Adapters::PostgreSQL
87
101
  def foreign_key_violations: (Skyward::ORM::_Connection _connection) -> Array[String]
88
102
 
89
103
  # Ending the transaction released the lock, and nothing else was set.
90
- # @rbs return: String
91
- def end_migration: () -> String
104
+ # @rbs return: String?
105
+ def end_migration: () -> String?
92
106
 
93
107
  # @rbs expression: String
94
108
  # @rbs return: String
95
109
  def default_expression: (String expression) -> String
96
110
 
97
- # @rbs type: Skyward::ORM::Tables::column_type
111
+ # @rbs default: String
112
+ # @rbs return: String
113
+ def catalog_default: (String default) -> String
114
+
115
+ # @rbs column: Skyward::ORM::Tables::Column
98
116
  # @rbs sql_type: String
99
117
  # @rbs return: bool
100
- def compatible?: (Skyward::ORM::Tables::column_type type, String sql_type) -> bool
118
+ def compatible?: (Skyward::ORM::Tables::Column column, String sql_type) -> bool
119
+
120
+ # PostgreSQL compares numeric as it is.
121
+ # @rbs (Skyward::ORM::Tables::column_type _type, String column) -> String
122
+ def comparable: (Skyward::ORM::Tables::column_type _type, String column) -> String
123
+
124
+ # @rbs sql_type: String
125
+ # @rbs return: Skyward::ORM::Tables::column_type?
126
+ def column_type: (String sql_type) -> Skyward::ORM::Tables::column_type?
101
127
 
102
128
  # The driver sends every other value as its to_s, which PostgreSQL reads.
103
129
  # @rbs value: untyped
104
130
  # @rbs return: untyped
105
131
  def encode: (untyped value) -> untyped
106
132
 
107
- # @rbs type: Skyward::ORM::Tables::column_type
133
+ # The encoder quotes each item that needs it, the string NULL included. A
134
+ # boolean is written as PostgreSQL writes one back, so a default reads back
135
+ # as it was written.
136
+ # @rbs items: Array[untyped]
137
+ # @rbs return: String
138
+ def encode_array: (Array[untyped] items) -> String
139
+
140
+ # @rbs column: Skyward::ORM::Tables::Column
108
141
  # @rbs value: untyped
109
142
  # @rbs return: untyped
110
- def decode: (Skyward::ORM::Tables::column_type type, untyped value) -> untyped
143
+ def decode: (Skyward::ORM::Tables::Column column, untyped value) -> untyped
111
144
 
112
145
  # @rbs (Skyward::ORM::_Connection connection) -> Array[Skyward::ORM::Catalog::Table]
113
146
  def tables: (Skyward::ORM::_Connection connection) -> Array[Skyward::ORM::Catalog::Table]
@@ -118,6 +151,16 @@ module Skyward::ORM::Adapters::PostgreSQL
118
151
 
119
152
  private
120
153
 
154
+ # @rbs type: Skyward::ORM::Tables::column_type
155
+ # @rbs sql_type: String
156
+ # @rbs return: bool
157
+ def accepts?: (Skyward::ORM::Tables::column_type type, String sql_type) -> bool
158
+
159
+ # @rbs type: Skyward::ORM::Tables::column_type
160
+ # @rbs value: untyped
161
+ # @rbs return: untyped
162
+ def scalar: (Skyward::ORM::Tables::column_type type, untyped value) -> untyped
163
+
121
164
  # @rbs rows: Array[Array[untyped]]
122
165
  # @rbs return: Array[Skyward::ORM::Catalog::Index]
123
166
  def indexes: (Array[Array[untyped]] rows) -> Array[Skyward::ORM::Catalog::Index]
@@ -126,10 +169,6 @@ module Skyward::ORM::Adapters::PostgreSQL
126
169
  # @rbs return: Array[Skyward::ORM::Catalog::Constraint]
127
170
  def constraints: (Array[Array[untyped]] rows) -> Array[Skyward::ORM::Catalog::Constraint]
128
171
 
129
- # @rbs expression: String?
130
- # @rbs return: String?
131
- def default: (String? expression) -> String?
132
-
133
172
  # @rbs tables: Array[Skyward::ORM::Catalog::Table]
134
173
  # @rbs table: String?
135
174
  # @rbs name: String?
@@ -26,6 +26,12 @@ module Skyward::ORM::Adapters::SQLite::Constraints
26
26
  # @rbs return: Array[Skyward::ORM::Catalog::Constraint]
27
27
  def read: (String sql, Array[Symbol] columns) -> Array[Skyward::ORM::Catalog::Constraint]
28
28
 
29
+ # SQLite's grammar makes GENERATED ALWAYS AS a column constraint, and
30
+ # table_xinfo doesn't give its expression, so that's read from here too.
31
+ # @rbs sql: String
32
+ # @rbs return: Hash[Symbol, String]
33
+ def generated: (String sql) -> Hash[Symbol, String]
34
+
29
35
  private
30
36
 
31
37
  # The scan covers every character, since its last alternative matches any
@@ -21,10 +21,27 @@ module Skyward::ORM::Adapters::SQLite
21
21
 
22
22
  RESTRICTED: String
23
23
 
24
+ # Types whose values keep their meaning under NUMERIC affinity, which Rails
25
+ # gives BOOLEAN, DATE and DATETIME. A boolean is already a number, and a date
26
+ # or time is never a well-formed number, so none is converted. A String or a
27
+ # BigDecimal would be: NUMERIC turns "1.10" into 1.1.
28
+ NUMERIC_SAFE: Array[Skyward::ORM::Tables::column_type]
29
+
30
+ # The names Rails writes for the NUMERIC_SAFE types, with any precision, as
31
+ # in DATETIME(6). Nothing else in the catalog tells a bool, Date or Time from
32
+ # an Integer or String, since a migration stores them as INTEGER and TEXT.
33
+ NUMERIC_NAMES: Hash[String, Skyward::ORM::Tables::column_type]
34
+
35
+ PRECISION: Regexp
36
+
37
+ AFFINITY_TYPES: Hash[Symbol, Skyward::ORM::Tables::column_type]
38
+
24
39
  UNIQUE_KINDS: Array[Skyward::ORM::Catalog::constraint_kind]
25
40
 
26
41
  BUSY_TIMEOUT_MS: Integer
27
42
 
43
+ # table_info leaves out generated columns, which table_xinfo marks hidden 2
44
+ # (virtual) or 3 (stored). Hidden 1 is a virtual table's own hidden column.
28
45
  COLUMNS: String
29
46
 
30
47
  INDEXES: String
@@ -36,6 +53,9 @@ module Skyward::ORM::Adapters::SQLite
36
53
  # @rbs return: String
37
54
  def driver: () -> String
38
55
 
56
+ # @rbs return: Symbol
57
+ def adapter_name: () -> Symbol
58
+
39
59
  # @rbs location: String
40
60
  # @rbs return: Skyward::ORM::_Connection
41
61
  def connect: (String location) -> Skyward::ORM::_Connection
@@ -54,13 +74,19 @@ module Skyward::ORM::Adapters::SQLite
54
74
  # @rbs (Integer _position) -> String
55
75
  def placeholder: (Integer _position) -> String
56
76
 
77
+ # SQLITE_MAX_VARIABLE_NUMBER's default since 3.32. A SQLite built with a
78
+ # lower one refuses with its own error, since the sqlite3 gem doesn't expose
79
+ # sqlite3_limit to read it.
80
+ # @rbs return: Integer
81
+ def bind_limit: () -> Integer
82
+
57
83
  # SQLite takes an OFFSET only after a LIMIT, and a negative one is no limit.
58
84
  # @rbs return: String
59
85
  def unlimited: () -> String
60
86
 
61
- # @rbs type: Skyward::ORM::Tables::column_type
87
+ # @rbs column: Skyward::ORM::Tables::Column
62
88
  # @rbs return: String?
63
- def sql_type: (Skyward::ORM::Tables::column_type type) -> String?
89
+ def sql_type: (Skyward::ORM::Tables::Column column) -> String?
64
90
 
65
91
  # An INTEGER PRIMARY KEY is SQLite's rowid, which numbers rows on its own.
66
92
  # @rbs return: String?
@@ -76,21 +102,44 @@ module Skyward::ORM::Adapters::SQLite
76
102
  # @rbs return: String
77
103
  def default_expression: (String expression) -> String
78
104
 
105
+ # The catalog puts back the parentheses SQLite drops, so it reads a default
106
+ # back as it was written.
107
+ # @rbs default: String
108
+ # @rbs return: String
109
+ def catalog_default: (String default) -> String
110
+
79
111
  # SQLite stores a value by its column's affinity, which it reads from the
80
- # declared type, so any declared type with the same affinity is compatible.
81
- # @rbs type: Skyward::ORM::Tables::column_type
112
+ # declared type, so any declared type with the same affinity is compatible,
113
+ # and for NUMERIC_SAFE types, NUMERIC as well.
114
+ # @rbs column: Skyward::ORM::Tables::Column
82
115
  # @rbs sql_type: String
83
116
  # @rbs return: bool
84
- def compatible?: (Skyward::ORM::Tables::column_type type, String sql_type) -> bool
117
+ def compatible?: (Skyward::ORM::Tables::Column column, String sql_type) -> bool
118
+
119
+ # A decimal's text orders "10.5" before "9.0", so a range or an order
120
+ # compares it as a REAL, which is exact to 15 significant digits.
121
+ # @rbs type: Skyward::ORM::Tables::column_type
122
+ # @rbs column: String
123
+ # @rbs return: String
124
+ def comparable: (Skyward::ORM::Tables::column_type type, String column) -> String
125
+
126
+ # @rbs sql_type: String
127
+ # @rbs return: Skyward::ORM::Tables::column_type?
128
+ def column_type: (String sql_type) -> Skyward::ORM::Tables::column_type?
85
129
 
86
130
  # @rbs value: untyped
87
131
  # @rbs return: untyped
88
132
  def encode: (untyped value) -> untyped
89
133
 
90
- # @rbs type: Skyward::ORM::Tables::column_type
134
+ # JSON holds a boolean as one, where a column holds it as 0 or 1.
135
+ # @rbs items: Array[untyped]
136
+ # @rbs return: String
137
+ def encode_array: (Array[untyped] items) -> String
138
+
139
+ # @rbs column: Skyward::ORM::Tables::Column
91
140
  # @rbs value: untyped
92
141
  # @rbs return: untyped
93
- def decode: (Skyward::ORM::Tables::column_type type, untyped value) -> untyped
142
+ def decode: (Skyward::ORM::Tables::Column column, untyped value) -> untyped
94
143
 
95
144
  # @rbs (Skyward::ORM::_Connection connection) -> Array[Skyward::ORM::Catalog::Table]
96
145
  def tables: (Skyward::ORM::_Connection connection) -> Array[Skyward::ORM::Catalog::Table]
@@ -121,6 +170,17 @@ module Skyward::ORM::Adapters::SQLite
121
170
 
122
171
  private
123
172
 
173
+ # @rbs type: Skyward::ORM::Tables::column_type
174
+ # @rbs value: untyped
175
+ # @rbs return: untyped
176
+ def scalar: (Skyward::ORM::Tables::column_type type, untyped value) -> untyped
177
+
178
+ # An Array[Float]'s JSON may hold an Integer, as an Array[Float] takes one.
179
+ # @rbs type: Skyward::ORM::Tables::column_type
180
+ # @rbs value: untyped
181
+ # @rbs return: untyped
182
+ def item: (Skyward::ORM::Tables::column_type type, untyped value) -> untyped
183
+
124
184
  # @rbs sql_type: String
125
185
  # @rbs return: Symbol
126
186
  def affinity: (String sql_type) -> Symbol
@@ -3,6 +3,9 @@
3
3
  module Skyward::ORM::Adapters
4
4
  SCHEMES: Hash[String, Skyward::ORM::_Adapter]
5
5
 
6
+ # What comes before "://" is shown only when it's a scheme: in a malformed URL it can hold a password.
7
+ SCHEME: Regexp
8
+
6
9
  # @rbs (String url, schemes: Hash[String, Skyward::ORM::_Adapter], requirer: ^(String) -> void)
7
10
  # -> [Skyward::ORM::_Adapter, String]
8
11
  def resolve: (String url, schemes: Hash[String, Skyward::ORM::_Adapter], requirer: ^(String) -> void) -> [ Skyward::ORM::_Adapter, String ]
@@ -10,11 +10,13 @@ module Skyward::ORM::Catalog
10
10
 
11
11
  attr_reader default(): String?
12
12
 
13
- def self.new: (Symbol name, String type, bool nullable, String? default) -> instance
14
- | (name: Symbol, type: String, nullable: bool, default: String?) -> instance
13
+ attr_reader generated(): String?
15
14
 
16
- def self.members: () -> [ :name, :type, :nullable, :default ]
15
+ def self.new: (Symbol name, String type, bool nullable, String? default, String? generated) -> instance
16
+ | (name: Symbol, type: String, nullable: bool, default: String?, generated: String?) -> instance
17
17
 
18
- def members: () -> [ :name, :type, :nullable, :default ]
18
+ def self.members: () -> [ :name, :type, :nullable, :default, :generated ]
19
+
20
+ def members: () -> [ :name, :type, :nullable, :default, :generated ]
19
21
  end
20
22
  end
@@ -6,8 +6,8 @@ module Skyward::ORM::CLI::BootFile
6
6
  # @rbs directory: String
7
7
  # @rbs session: Skyward::ORM::Connection::Session
8
8
  # @rbs generate: ^() -> String?
9
- # @rbs return: Skyward::ORM::Connection::Database | String
10
- def load: (String directory, Skyward::ORM::Connection::Session session, ^() -> String? generate) -> (Skyward::ORM::Connection::Database | String)
9
+ # @rbs return: Skyward::ORM::Database | String
10
+ def load: (String directory, Skyward::ORM::Connection::Session session, ^() -> String? generate) -> (Skyward::ORM::Database | String)
11
11
 
12
12
  # The migration commands are what fixes a schema that doesn't match, so they
13
13
  # boot the app with the check off.
@@ -15,7 +15,7 @@ module Skyward::ORM::CLI::BootFile
15
15
  # @rbs session: Skyward::ORM::Connection::Session
16
16
  # @rbs kernel: singleton(Kernel)
17
17
  # @rbs generate: ^() -> String?
18
- # @rbs &: (Skyward::ORM::Connection::Database) -> void
18
+ # @rbs &: (Skyward::ORM::Database) -> void
19
19
  # @rbs return: void
20
- def boot: (String directory, Skyward::ORM::Connection::Session session, singleton(Kernel) kernel, ^() -> String? generate) { (Skyward::ORM::Connection::Database) -> void } -> void
20
+ def boot: (String directory, Skyward::ORM::Connection::Session session, singleton(Kernel) kernel, ^() -> String? generate) { (Skyward::ORM::Database) -> void } -> void
21
21
  end
@@ -9,7 +9,7 @@ module Skyward::ORM::CLI::Migrate
9
9
 
10
10
  private
11
11
 
12
- # @rbs (Skyward::ORM::Connection::Database database, directory: String, out: _Writer, kernel: singleton(Kernel),
12
+ # @rbs (Skyward::ORM::Database database, directory: String, out: _Writer, kernel: singleton(Kernel),
13
13
  # discover: ^() -> Array[singleton(Skyward::ORM::Record)]) -> void
14
- def migrate: (Skyward::ORM::Connection::Database database, directory: String, out: _Writer, kernel: singleton(Kernel), discover: ^() -> Array[singleton(Skyward::ORM::Record)]) -> void
14
+ def migrate: (Skyward::ORM::Database database, directory: String, out: _Writer, kernel: singleton(Kernel), discover: ^() -> Array[singleton(Skyward::ORM::Record)]) -> void
15
15
  end
@@ -25,9 +25,9 @@ module Skyward::ORM::CLI::Migration
25
25
  # change twice, so a pending one stops it. With no terminal to ask whether a
26
26
  # column was renamed, it stops rather than guess, since a guess of no drops
27
27
  # the column's data.
28
- # @rbs (String name, Skyward::ORM::Connection::Database database, directory: String, time: Time, input: IO,
28
+ # @rbs (String name, Skyward::ORM::Database database, directory: String, time: Time, input: IO,
29
29
  # out: _Writer, kernel: singleton(Kernel), discover: ^() -> Array[singleton(Skyward::ORM::Record)]) -> void
30
- def write: (String name, Skyward::ORM::Connection::Database database, directory: String, time: Time, input: IO, out: _Writer, kernel: singleton(Kernel), discover: ^() -> Array[singleton(Skyward::ORM::Record)]) -> void
30
+ def write: (String name, Skyward::ORM::Database database, directory: String, time: Time, input: IO, out: _Writer, kernel: singleton(Kernel), discover: ^() -> Array[singleton(Skyward::ORM::Record)]) -> void
31
31
 
32
32
  # @rbs input: IO
33
33
  # @rbs out: _Writer