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.
- checksums.yaml +4 -4
- data/README.md +200 -31
- data/lib/skyward/orm/adapters/postgresql.rb +140 -38
- data/lib/skyward/orm/adapters/sqlite/constraints.rb +18 -0
- data/lib/skyward/orm/adapters/sqlite.rb +130 -21
- data/lib/skyward/orm/adapters.rb +12 -2
- data/lib/skyward/orm/catalog/column.rb +2 -1
- data/lib/skyward/orm/cli/boot_file.rb +2 -2
- data/lib/skyward/orm/cli/migrate.rb +1 -1
- data/lib/skyward/orm/cli/migration.rb +1 -1
- data/lib/skyward/orm/cli/scaffold/source.rb +304 -0
- data/lib/skyward/orm/cli/scaffold.rb +70 -0
- data/lib/skyward/orm/cli/status.rb +5 -3
- data/lib/skyward/orm/commands.rb +12 -0
- data/lib/skyward/orm/connection/instrumented.rb +69 -0
- data/lib/skyward/orm/connection/session.rb +74 -5
- data/lib/skyward/orm/{connection/database → database}/schema_check.rb +20 -8
- data/lib/skyward/orm/{connection/database.rb → database.rb} +15 -15
- data/lib/skyward/orm/execution.rb +9 -0
- data/lib/skyward/orm/migrations/alter_table.rb +6 -2
- data/lib/skyward/orm/migrations/definitions.rb +7 -4
- data/lib/skyward/orm/migrations/diff.rb +154 -30
- data/lib/skyward/orm/migrations/rename_constraint.rb +8 -0
- data/lib/skyward/orm/migrations/runner.rb +13 -3
- data/lib/skyward/orm/migrations/script.rb +60 -20
- data/lib/skyward/orm/migrations/status.rb +32 -3
- data/lib/skyward/orm/migrations/summary.rb +3 -6
- data/lib/skyward/orm/migrations/versions.rb +1 -1
- data/lib/skyward/orm/migrations.rb +1 -1
- data/lib/skyward/orm/projection.rb +23 -4
- data/lib/skyward/orm/queries/clauses.rb +117 -32
- data/lib/skyward/orm/record.rb +23 -1
- data/lib/skyward/orm/{placeholders.rb → statements/placeholders.rb} +1 -1
- data/lib/skyward/orm/statements.rb +171 -30
- data/lib/skyward/orm/tables/column.rb +2 -1
- data/lib/skyward/orm/tables/column_types.rb +62 -15
- data/lib/skyward/orm/tables/declarations.rb +38 -10
- data/lib/skyward/orm/{members.rb → tables/members.rb} +1 -1
- data/lib/skyward/orm/tables/sources.rb +63 -0
- data/lib/skyward/orm/tables/validation.rb +12 -3
- data/lib/skyward/orm/tables/values.rb +115 -5
- data/lib/skyward/orm/tables.rb +2 -1
- data/lib/skyward/orm/transactional.rb +2 -2
- data/lib/skyward/orm/version.rb +1 -1
- data/lib/skyward/orm.rb +44 -10
- data/sig/generated/skyward/orm/adapters/postgresql.rbs +53 -14
- data/sig/generated/skyward/orm/adapters/sqlite/constraints.rbs +6 -0
- data/sig/generated/skyward/orm/adapters/sqlite.rbs +67 -7
- data/sig/generated/skyward/orm/adapters.rbs +3 -0
- data/sig/generated/skyward/orm/catalog/column.rbs +6 -4
- data/sig/generated/skyward/orm/cli/boot_file.rbs +4 -4
- data/sig/generated/skyward/orm/cli/migrate.rbs +2 -2
- data/sig/generated/skyward/orm/cli/migration.rbs +2 -2
- data/sig/generated/skyward/orm/cli/scaffold/source.rbs +147 -0
- data/sig/generated/skyward/orm/cli/scaffold.rbs +28 -0
- data/sig/generated/skyward/orm/cli/status.rbs +2 -2
- data/sig/generated/skyward/orm/connection/instrumented.rbs +45 -0
- data/sig/generated/skyward/orm/connection/session.rbs +40 -6
- data/sig/generated/skyward/orm/{connection/database → database}/schema_check.rbs +11 -4
- data/sig/generated/skyward/orm/{connection/database.rbs → database.rbs} +4 -5
- data/sig/generated/skyward/orm/execution.rbs +18 -0
- data/sig/generated/skyward/orm/migrations/alter_table.rbs +11 -5
- data/sig/generated/skyward/orm/migrations/definitions.rbs +2 -2
- data/sig/generated/skyward/orm/migrations/diff.rbs +77 -11
- data/sig/generated/skyward/orm/migrations/rename_constraint.rbs +16 -0
- data/sig/generated/skyward/orm/migrations/runner.rbs +5 -0
- data/sig/generated/skyward/orm/migrations/script.rbs +32 -8
- data/sig/generated/skyward/orm/migrations/status.rbs +15 -2
- data/sig/generated/skyward/orm/migrations/summary.rbs +0 -1
- data/sig/generated/skyward/orm/migrations/versions.rbs +2 -2
- data/sig/generated/skyward/orm/migrations.rbs +1 -1
- data/sig/generated/skyward/orm/projection.rbs +10 -2
- data/sig/generated/skyward/orm/queries/clauses.rbs +60 -24
- data/sig/generated/skyward/orm/record.rbs +8 -0
- data/sig/generated/skyward/orm/{placeholders.rbs → statements/placeholders.rbs} +2 -2
- data/sig/generated/skyward/orm/statements.rbs +83 -5
- data/sig/generated/skyward/orm/tables/column.rbs +6 -4
- data/sig/generated/skyward/orm/tables/column_types.rbs +18 -4
- data/sig/generated/skyward/orm/tables/declarations.rbs +15 -1
- data/sig/generated/skyward/orm/{members.rbs → tables/members.rbs} +2 -2
- data/sig/generated/skyward/orm/tables/sources.rbs +32 -0
- data/sig/generated/skyward/orm/tables/values.rbs +41 -1
- data/sig/generated/skyward/orm/tables.rbs +3 -1
- data/sig/generated/skyward/orm.rbs +6 -6
- data/sig/manual/pg.rbs +12 -0
- data/sig/manual/skyward/orm/cli/scaffold/source.rbs +3 -0
- data/sig/manual/skyward/orm/cli/scaffold.rbs +3 -0
- data/sig/manual/skyward/orm/database/schema_check.rbs +3 -0
- data/sig/manual/skyward/orm/statements/placeholders.rbs +3 -0
- data/sig/manual/skyward/orm/tables/members.rbs +3 -0
- data/sig/manual/skyward/orm/tables/sources.rbs +3 -0
- data/sig/manual/skyward/orm.rbs +32 -5
- metadata +43 -14
- data/sig/manual/skyward/orm/connection/database/schema_check.rbs +0 -3
- data/sig/manual/skyward/orm/members.rbs +0 -3
- data/sig/manual/skyward/orm/placeholders.rbs +0 -3
|
@@ -6,24 +6,54 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
6
6
|
SQL_TYPES = {
|
|
7
7
|
integer: "bigint",
|
|
8
8
|
float: "double precision",
|
|
9
|
+
decimal: "numeric",
|
|
9
10
|
string: "text",
|
|
10
11
|
bool: "boolean",
|
|
11
12
|
date: "date",
|
|
12
13
|
time: "timestamptz",
|
|
13
|
-
enum: "text"
|
|
14
|
+
enum: "text",
|
|
15
|
+
uuid: "uuid",
|
|
16
|
+
int32: "integer",
|
|
17
|
+
int16: "smallint",
|
|
18
|
+
json: "jsonb"
|
|
14
19
|
}.freeze #: Hash[Skyward::ORM::Tables::column_type, String]
|
|
15
20
|
|
|
16
21
|
# As the catalog spells each type, which compatible? is given.
|
|
17
22
|
COMPATIBLE = {
|
|
18
23
|
integer: %w[bigint integer smallint],
|
|
19
24
|
float: ["double precision", "real"],
|
|
25
|
+
decimal: ["numeric"],
|
|
20
26
|
string: ["text", "character varying", "uuid", "citext"],
|
|
21
27
|
bool: ["boolean"],
|
|
22
28
|
date: ["date"],
|
|
23
29
|
time: ["timestamp with time zone", "timestamp without time zone"],
|
|
24
|
-
enum: ["text", "character varying"]
|
|
30
|
+
enum: ["text", "character varying"],
|
|
31
|
+
uuid: ["uuid"],
|
|
32
|
+
int32: ["integer"],
|
|
33
|
+
int16: ["smallint"],
|
|
34
|
+
json: %w[jsonb json]
|
|
25
35
|
}.freeze #: Hash[Skyward::ORM::Tables::column_type, Array[String]]
|
|
26
36
|
|
|
37
|
+
# COMPATIBLE read backwards, taking the narrowest type for each.
|
|
38
|
+
COLUMN_TYPES = {
|
|
39
|
+
"bigint" => :integer,
|
|
40
|
+
"integer" => :int32,
|
|
41
|
+
"smallint" => :int16,
|
|
42
|
+
"double precision" => :float,
|
|
43
|
+
"real" => :float,
|
|
44
|
+
"numeric" => :decimal,
|
|
45
|
+
"text" => :string,
|
|
46
|
+
"character varying" => :string,
|
|
47
|
+
"citext" => :string,
|
|
48
|
+
"uuid" => :uuid,
|
|
49
|
+
"boolean" => :bool,
|
|
50
|
+
"date" => :date,
|
|
51
|
+
"timestamp with time zone" => :time,
|
|
52
|
+
"timestamp without time zone" => :time,
|
|
53
|
+
"jsonb" => :json,
|
|
54
|
+
"json" => :json
|
|
55
|
+
}.freeze #: Hash[String, Skyward::ORM::Tables::column_type]
|
|
56
|
+
|
|
27
57
|
# The catalog puts a length or precision in parentheses.
|
|
28
58
|
MODIFIER = /\(\d+(?:,\d+)?\)/ #: Regexp
|
|
29
59
|
|
|
@@ -33,9 +63,10 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
33
63
|
TIME_FORMAT = "%Y-%m-%d %H:%M:%S.%6N" #: String
|
|
34
64
|
TRAILING_ZEROS = /\.?0+\z/ #: Regexp
|
|
35
65
|
|
|
36
|
-
# PostgreSQL reads a literal default back cast to its type, as 'normal'::text
|
|
37
|
-
# and quotes a number that isn't a plain integer or decimal,
|
|
38
|
-
|
|
66
|
+
# PostgreSQL reads a literal default back cast to its type, as 'normal'::text
|
|
67
|
+
# or '{}'::text[], and quotes a number that isn't a plain integer or decimal,
|
|
68
|
+
# as '-1'::integer.
|
|
69
|
+
CAST = /\A(?<literal>'(?:[^']|'')*')::(?<type>[a-z ]+(?:\[\])?)\z/ #: Regexp
|
|
39
70
|
NUMERIC = /\A(?:smallint|integer|bigint|numeric|real|double precision)\z/ #: Regexp
|
|
40
71
|
|
|
41
72
|
# Float() refuses the special values PostgreSQL writes out.
|
|
@@ -49,6 +80,7 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
49
80
|
|
|
50
81
|
# Each value comes back as text, and a boolean as t or f.
|
|
51
82
|
TRUE_TEXT = "t" #: String
|
|
83
|
+
FALSE_TEXT = "f" #: String
|
|
52
84
|
|
|
53
85
|
VIOLATIONS = {
|
|
54
86
|
"23505" => :unique,
|
|
@@ -68,8 +100,11 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
68
100
|
TABLES = "t.relkind IN ('r', 'p') AND t.relnamespace <> 'pg_catalog'::regnamespace " \
|
|
69
101
|
"AND pg_table_is_visible(t.oid)" #: String
|
|
70
102
|
|
|
103
|
+
# pg_attrdef holds a generated column's expression as if it were its default.
|
|
71
104
|
COLUMNS = <<~SQL.freeze #: String
|
|
72
|
-
SELECT t.relname, a.attname, format_type(a.atttypid, a.atttypmod), a.attnotnull,
|
|
105
|
+
SELECT t.relname, a.attname, format_type(a.atttypid, a.atttypmod), a.attnotnull,
|
|
106
|
+
CASE WHEN a.attgenerated = '' THEN pg_get_expr(d.adbin, d.adrelid) END,
|
|
107
|
+
CASE WHEN a.attgenerated <> '' THEN pg_get_expr(d.adbin, d.adrelid) END
|
|
73
108
|
FROM pg_catalog.pg_class AS t
|
|
74
109
|
JOIN pg_catalog.pg_attribute AS a ON a.attrelid = t.oid AND a.attnum > 0 AND NOT a.attisdropped
|
|
75
110
|
LEFT JOIN pg_catalog.pg_attrdef AS d ON d.adrelid = a.attrelid AND d.adnum = a.attnum
|
|
@@ -106,6 +141,9 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
106
141
|
# @rbs return: String
|
|
107
142
|
def driver = "pg"
|
|
108
143
|
|
|
144
|
+
# @rbs return: Symbol
|
|
145
|
+
def adapter_name = :postgresql
|
|
146
|
+
|
|
109
147
|
# Values are decoded from PostgreSQL's ISO text, whatever the server's
|
|
110
148
|
# defaults, and times read and written in UTC. libpq prints each notice the
|
|
111
149
|
# server sends, so only warnings and worse are sent, or `skyward migrate`
|
|
@@ -125,13 +163,20 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
125
163
|
# @rbs return: String
|
|
126
164
|
def placeholder(position) = "$#{position}"
|
|
127
165
|
|
|
166
|
+
# The protocol counts a statement's parameters in 16 bits.
|
|
167
|
+
# @rbs return: Integer
|
|
168
|
+
def bind_limit = 65_535
|
|
169
|
+
|
|
128
170
|
# PostgreSQL takes an OFFSET with no LIMIT before it.
|
|
129
171
|
# @rbs return: String?
|
|
130
172
|
def unlimited = nil
|
|
131
173
|
|
|
132
|
-
# @rbs
|
|
174
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
133
175
|
# @rbs return: String?
|
|
134
|
-
def sql_type(
|
|
176
|
+
def sql_type(column)
|
|
177
|
+
element = column.element
|
|
178
|
+
element ? "#{SQL_TYPES.fetch(element)}[]" : SQL_TYPES[column.type]
|
|
179
|
+
end
|
|
135
180
|
|
|
136
181
|
# @rbs return: String?
|
|
137
182
|
def identity = "GENERATED ALWAYS AS IDENTITY"
|
|
@@ -149,46 +194,82 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
149
194
|
def foreign_key_violations(_connection) = []
|
|
150
195
|
|
|
151
196
|
# Ending the transaction released the lock, and nothing else was set.
|
|
152
|
-
# @rbs return: String
|
|
153
|
-
def end_migration =
|
|
197
|
+
# @rbs return: String?
|
|
198
|
+
def end_migration = nil
|
|
154
199
|
|
|
155
200
|
# @rbs expression: String
|
|
156
201
|
# @rbs return: String
|
|
157
202
|
def default_expression(expression) = expression
|
|
158
203
|
|
|
159
|
-
# @rbs
|
|
204
|
+
# @rbs default: String
|
|
205
|
+
# @rbs return: String
|
|
206
|
+
def catalog_default(default)
|
|
207
|
+
cast = CAST.match(default)
|
|
208
|
+
return default unless cast
|
|
209
|
+
|
|
210
|
+
literal = cast[:literal].to_s
|
|
211
|
+
NUMERIC.match?(cast[:type].to_s) ? literal.delete("'") : literal
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
160
215
|
# @rbs sql_type: String
|
|
161
216
|
# @rbs return: bool
|
|
162
|
-
def compatible?(
|
|
163
|
-
|
|
164
|
-
|
|
217
|
+
def compatible?(column, sql_type)
|
|
218
|
+
element = column.element
|
|
219
|
+
return accepts?(column.type, sql_type) unless element
|
|
220
|
+
|
|
221
|
+
sql_type.end_with?("[]") && accepts?(element, sql_type.delete_suffix("[]"))
|
|
165
222
|
end
|
|
166
223
|
|
|
224
|
+
# PostgreSQL compares numeric as it is.
|
|
225
|
+
# @rbs (Skyward::ORM::Tables::column_type _type, String column) -> String
|
|
226
|
+
def comparable(_type, column) = column
|
|
227
|
+
|
|
228
|
+
# @rbs sql_type: String
|
|
229
|
+
# @rbs return: Skyward::ORM::Tables::column_type?
|
|
230
|
+
def column_type(sql_type) = COLUMN_TYPES[sql_type.sub(MODIFIER, "")]
|
|
231
|
+
|
|
167
232
|
# The driver sends every other value as its to_s, which PostgreSQL reads.
|
|
168
233
|
# @rbs value: untyped
|
|
169
234
|
# @rbs return: untyped
|
|
170
235
|
def encode(value)
|
|
171
236
|
case value
|
|
172
237
|
when Symbol then value.name
|
|
238
|
+
# Without the exponent BigDecimal#to_s writes, as 0.15e1, so both adapters
|
|
239
|
+
# bind the same text.
|
|
240
|
+
# -0.0 is 0, but would be written "-0.0" and miss an = against 0 on SQLite.
|
|
241
|
+
when BigDecimal then (value.zero? ? BigDecimal(0) : value).to_s("F")
|
|
173
242
|
when Time then "#{value.getutc.strftime(TIME_FORMAT).sub(TRAILING_ZEROS, "")}+00"
|
|
174
243
|
else value
|
|
175
244
|
end
|
|
176
245
|
end
|
|
177
246
|
|
|
178
|
-
#
|
|
247
|
+
# The encoder quotes each item that needs it, the string NULL included. A
|
|
248
|
+
# boolean is written as PostgreSQL writes one back, so a default reads back
|
|
249
|
+
# as it was written.
|
|
250
|
+
# @rbs items: Array[untyped]
|
|
251
|
+
# @rbs return: String
|
|
252
|
+
def encode_array(items)
|
|
253
|
+
PG::TextEncoder::Array.new.encode(
|
|
254
|
+
items.map do |item|
|
|
255
|
+
case item
|
|
256
|
+
when true then TRUE_TEXT
|
|
257
|
+
when false then FALSE_TEXT
|
|
258
|
+
else encode(item)
|
|
259
|
+
end
|
|
260
|
+
end
|
|
261
|
+
)
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
179
265
|
# @rbs value: untyped
|
|
180
266
|
# @rbs return: untyped
|
|
181
|
-
def decode(
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
# A timestamp column's text has no zone, so it's read as the UTC it was written in.
|
|
188
|
-
when :time then Time.new(value, in: "UTC").getutc
|
|
189
|
-
when :enum then value.to_sym
|
|
190
|
-
else value.freeze
|
|
191
|
-
end
|
|
267
|
+
def decode(column, value)
|
|
268
|
+
element = column.element
|
|
269
|
+
return scalar(column.type, value) unless element
|
|
270
|
+
|
|
271
|
+
# Other SQL can write a NULL item, which the ORM never does; it stays nil rather than reading as false.
|
|
272
|
+
PG::TextDecoder::Array.new.decode(value).map { |item| item.nil? ? nil : scalar(element, item).freeze }.freeze
|
|
192
273
|
end
|
|
193
274
|
|
|
194
275
|
# @rbs (Skyward::ORM::_Connection connection) -> Array[Skyward::ORM::Catalog::Table]
|
|
@@ -198,9 +279,13 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
198
279
|
connection.execute(COLUMNS).group_by(&:first).map do |name, rows|
|
|
199
280
|
Skyward::ORM::Catalog::Table.new(
|
|
200
281
|
name: name.to_sym,
|
|
201
|
-
columns: rows.map do |_, column, type, not_null, default|
|
|
282
|
+
columns: rows.map do |_, column, type, not_null, default, generated|
|
|
202
283
|
Skyward::ORM::Catalog::Column.new(
|
|
203
|
-
name: column.to_sym,
|
|
284
|
+
name: column.to_sym,
|
|
285
|
+
type:,
|
|
286
|
+
nullable: not_null != TRUE_TEXT,
|
|
287
|
+
default: default && catalog_default(default),
|
|
288
|
+
generated:
|
|
204
289
|
)
|
|
205
290
|
end,
|
|
206
291
|
indexes: indexes(index_rows.fetch(name, [])),
|
|
@@ -231,6 +316,33 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
231
316
|
|
|
232
317
|
private
|
|
233
318
|
|
|
319
|
+
# @rbs type: Skyward::ORM::Tables::column_type
|
|
320
|
+
# @rbs sql_type: String
|
|
321
|
+
# @rbs return: bool
|
|
322
|
+
def accepts?(type, sql_type)
|
|
323
|
+
accepted = COMPATIBLE[type]
|
|
324
|
+
accepted.nil? || accepted.include?(sql_type.sub(MODIFIER, ""))
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
# @rbs type: Skyward::ORM::Tables::column_type
|
|
328
|
+
# @rbs value: untyped
|
|
329
|
+
# @rbs return: untyped
|
|
330
|
+
def scalar(type, value)
|
|
331
|
+
case type
|
|
332
|
+
when :integer, :int32, :int16 then Integer(value, 10)
|
|
333
|
+
when :float then SPECIAL_FLOATS.fetch(value) { Float(value) }
|
|
334
|
+
when :decimal then BigDecimal(value)
|
|
335
|
+
when :bool then value == TRUE_TEXT
|
|
336
|
+
when :date then Date.iso8601(value)
|
|
337
|
+
# A timestamp column's text has no zone, so it's read as the UTC it was written in.
|
|
338
|
+
when :time then Time.new(value, in: "UTC").getutc
|
|
339
|
+
# An Array's item may be NULL, or an Array of its own, which names no Symbol.
|
|
340
|
+
when :enum then String.try_convert(value)&.to_sym
|
|
341
|
+
when :json then JSON.parse(value, freeze: true)
|
|
342
|
+
else value.freeze
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
|
|
234
346
|
# @rbs rows: Array[Array[untyped]]
|
|
235
347
|
# @rbs return: Array[Skyward::ORM::Catalog::Index]
|
|
236
348
|
def indexes(rows)
|
|
@@ -260,16 +372,6 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
260
372
|
end
|
|
261
373
|
end
|
|
262
374
|
|
|
263
|
-
# @rbs expression: String?
|
|
264
|
-
# @rbs return: String?
|
|
265
|
-
def default(expression)
|
|
266
|
-
cast = CAST.match(expression.to_s)
|
|
267
|
-
return expression unless cast
|
|
268
|
-
|
|
269
|
-
literal = cast[:literal].to_s
|
|
270
|
-
NUMERIC.match?(cast[:type].to_s) ? literal.delete("'") : literal
|
|
271
|
-
end
|
|
272
|
-
|
|
273
375
|
# @rbs tables: Array[Skyward::ORM::Catalog::Table]
|
|
274
376
|
# @rbs table: String?
|
|
275
377
|
# @rbs name: String?
|
|
@@ -45,6 +45,24 @@ module Skyward::ORM::Adapters::SQLite::Constraints
|
|
|
45
45
|
# @rbs return: Array[Skyward::ORM::Catalog::Constraint]
|
|
46
46
|
def read(sql, columns) = definitions(sql).flat_map { |definition| constraints(sql, definition, columns) }
|
|
47
47
|
|
|
48
|
+
# SQLite's grammar makes GENERATED ALWAYS AS a column constraint, and
|
|
49
|
+
# table_xinfo doesn't give its expression, so that's read from here too.
|
|
50
|
+
# @rbs sql: String
|
|
51
|
+
# @rbs return: Hash[Symbol, String]
|
|
52
|
+
def generated(sql)
|
|
53
|
+
definitions(sql).filter_map do |definition|
|
|
54
|
+
tokens = definition.map(&:first)
|
|
55
|
+
as = definition.each_index.find do |index|
|
|
56
|
+
index.positive? && definition.fetch(index).last.zero? && word(tokens.fetch(index)) == "AS"
|
|
57
|
+
end
|
|
58
|
+
next unless as
|
|
59
|
+
|
|
60
|
+
# The parenthesized tokens end with the closing parenthesis.
|
|
61
|
+
expression = first_parenthesized(definition.drop(as + 1))
|
|
62
|
+
[tokens.fetch(0)[1].to_sym, text(sql, expression.take(expression.size - 1))]
|
|
63
|
+
end.to_h
|
|
64
|
+
end
|
|
65
|
+
|
|
48
66
|
private
|
|
49
67
|
|
|
50
68
|
# The scan covers every character, since its last alternative matches any
|
|
@@ -9,11 +9,17 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
9
9
|
SQL_TYPES = {
|
|
10
10
|
integer: "INTEGER",
|
|
11
11
|
float: "REAL",
|
|
12
|
+
decimal: "TEXT",
|
|
12
13
|
string: "TEXT",
|
|
13
14
|
bool: "INTEGER",
|
|
14
15
|
date: "TEXT",
|
|
15
16
|
time: "TEXT",
|
|
16
|
-
enum: "TEXT"
|
|
17
|
+
enum: "TEXT",
|
|
18
|
+
uuid: "TEXT",
|
|
19
|
+
int32: "INTEGER",
|
|
20
|
+
int16: "INTEGER",
|
|
21
|
+
json: "TEXT",
|
|
22
|
+
array: "TEXT"
|
|
17
23
|
}.freeze #: Hash[Skyward::ORM::Tables::column_type, String]
|
|
18
24
|
|
|
19
25
|
# Fixed width and UTC, so text order is time order, and to the microsecond,
|
|
@@ -36,14 +42,38 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
36
42
|
TRIGGER = 1811 #: Integer
|
|
37
43
|
RESTRICTED = "FOREIGN KEY constraint failed" #: String
|
|
38
44
|
|
|
45
|
+
# Types whose values keep their meaning under NUMERIC affinity, which Rails
|
|
46
|
+
# gives BOOLEAN, DATE and DATETIME. A boolean is already a number, and a date
|
|
47
|
+
# or time is never a well-formed number, so none is converted. A String or a
|
|
48
|
+
# BigDecimal would be: NUMERIC turns "1.10" into 1.1.
|
|
49
|
+
NUMERIC_SAFE = %i[bool date time].freeze #: Array[Skyward::ORM::Tables::column_type]
|
|
50
|
+
|
|
51
|
+
# The names Rails writes for the NUMERIC_SAFE types, with any precision, as
|
|
52
|
+
# in DATETIME(6). Nothing else in the catalog tells a bool, Date or Time from
|
|
53
|
+
# an Integer or String, since a migration stores them as INTEGER and TEXT.
|
|
54
|
+
NUMERIC_NAMES = {
|
|
55
|
+
"BOOLEAN" => :bool,
|
|
56
|
+
"DATE" => :date,
|
|
57
|
+
"DATETIME" => :time
|
|
58
|
+
}.freeze #: Hash[String, Skyward::ORM::Tables::column_type]
|
|
59
|
+
PRECISION = /\s*\(.*\)\z/ #: Regexp
|
|
60
|
+
|
|
61
|
+
AFFINITY_TYPES = {
|
|
62
|
+
integer: :integer,
|
|
63
|
+
real: :float,
|
|
64
|
+
text: :string
|
|
65
|
+
}.freeze #: Hash[Symbol, Skyward::ORM::Tables::column_type]
|
|
66
|
+
|
|
39
67
|
UNIQUE_KINDS = %i[unique primary_key].freeze #: Array[Skyward::ORM::Catalog::constraint_kind]
|
|
40
68
|
|
|
41
69
|
BUSY_TIMEOUT_MS = 5000 #: Integer
|
|
42
70
|
|
|
71
|
+
# table_info leaves out generated columns, which table_xinfo marks hidden 2
|
|
72
|
+
# (virtual) or 3 (stored). Hidden 1 is a virtual table's own hidden column.
|
|
43
73
|
COLUMNS = <<~SQL #: String
|
|
44
74
|
SELECT m.name, m.sql, c.name, c.type, c."notnull", c.pk, c.dflt_value
|
|
45
|
-
FROM sqlite_schema AS m JOIN
|
|
46
|
-
WHERE m.type = 'table' AND m.name NOT LIKE 'sqlite\\_%' ESCAPE '\\'
|
|
75
|
+
FROM sqlite_schema AS m JOIN pragma_table_xinfo(m.name) AS c
|
|
76
|
+
WHERE m.type = 'table' AND m.name NOT LIKE 'sqlite\\_%' ESCAPE '\\' AND c.hidden <> 1
|
|
47
77
|
ORDER BY m.name, c.cid
|
|
48
78
|
SQL
|
|
49
79
|
|
|
@@ -62,6 +92,9 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
62
92
|
# @rbs return: String
|
|
63
93
|
def driver = "sqlite3"
|
|
64
94
|
|
|
95
|
+
# @rbs return: Symbol
|
|
96
|
+
def adapter_name = :sqlite
|
|
97
|
+
|
|
65
98
|
# @rbs location: String
|
|
66
99
|
# @rbs return: Skyward::ORM::_Connection
|
|
67
100
|
def connect(location)
|
|
@@ -95,13 +128,19 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
95
128
|
# @rbs (Integer _position) -> String
|
|
96
129
|
def placeholder(_position) = "?"
|
|
97
130
|
|
|
131
|
+
# SQLITE_MAX_VARIABLE_NUMBER's default since 3.32. A SQLite built with a
|
|
132
|
+
# lower one refuses with its own error, since the sqlite3 gem doesn't expose
|
|
133
|
+
# sqlite3_limit to read it.
|
|
134
|
+
# @rbs return: Integer
|
|
135
|
+
def bind_limit = 32_766
|
|
136
|
+
|
|
98
137
|
# SQLite takes an OFFSET only after a LIMIT, and a negative one is no limit.
|
|
99
138
|
# @rbs return: String
|
|
100
139
|
def unlimited = "LIMIT -1"
|
|
101
140
|
|
|
102
|
-
# @rbs
|
|
141
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
103
142
|
# @rbs return: String?
|
|
104
|
-
def sql_type(
|
|
143
|
+
def sql_type(column) = SQL_TYPES[column.type]
|
|
105
144
|
|
|
106
145
|
# An INTEGER PRIMARY KEY is SQLite's rowid, which numbers rows on its own.
|
|
107
146
|
# @rbs return: String?
|
|
@@ -117,16 +156,36 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
117
156
|
# @rbs return: String
|
|
118
157
|
def default_expression(expression) = "(#{expression})"
|
|
119
158
|
|
|
159
|
+
# The catalog puts back the parentheses SQLite drops, so it reads a default
|
|
160
|
+
# back as it was written.
|
|
161
|
+
# @rbs default: String
|
|
162
|
+
# @rbs return: String
|
|
163
|
+
def catalog_default(default) = default
|
|
164
|
+
|
|
120
165
|
# SQLite stores a value by its column's affinity, which it reads from the
|
|
121
|
-
# declared type, so any declared type with the same affinity is compatible
|
|
122
|
-
#
|
|
166
|
+
# declared type, so any declared type with the same affinity is compatible,
|
|
167
|
+
# and for NUMERIC_SAFE types, NUMERIC as well.
|
|
168
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
123
169
|
# @rbs sql_type: String
|
|
124
170
|
# @rbs return: bool
|
|
125
|
-
def compatible?(
|
|
171
|
+
def compatible?(column, sql_type)
|
|
172
|
+
type = column.type
|
|
126
173
|
expected = SQL_TYPES[type]
|
|
127
|
-
|
|
174
|
+
declared = affinity(sql_type)
|
|
175
|
+
expected.nil? || declared == affinity(expected) || (declared == :numeric && NUMERIC_SAFE.include?(type))
|
|
128
176
|
end
|
|
129
177
|
|
|
178
|
+
# A decimal's text orders "10.5" before "9.0", so a range or an order
|
|
179
|
+
# compares it as a REAL, which is exact to 15 significant digits.
|
|
180
|
+
# @rbs type: Skyward::ORM::Tables::column_type
|
|
181
|
+
# @rbs column: String
|
|
182
|
+
# @rbs return: String
|
|
183
|
+
def comparable(type, column) = type == :decimal ? "CAST(#{column} AS REAL)" : column
|
|
184
|
+
|
|
185
|
+
# @rbs sql_type: String
|
|
186
|
+
# @rbs return: Skyward::ORM::Tables::column_type?
|
|
187
|
+
def column_type(sql_type) = NUMERIC_NAMES[sql_type.upcase.sub(PRECISION, "")] || AFFINITY_TYPES[affinity(sql_type)]
|
|
188
|
+
|
|
130
189
|
# @rbs value: untyped
|
|
131
190
|
# @rbs return: untyped
|
|
132
191
|
def encode(value)
|
|
@@ -134,30 +193,49 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
134
193
|
when true then 1
|
|
135
194
|
when false then 0
|
|
136
195
|
when Symbol then value.name
|
|
196
|
+
# A decimal is stored as text, which = compares, so it's always written the
|
|
197
|
+
# same way, without the exponent BigDecimal#to_s writes, as 0.15e1.
|
|
198
|
+
# -0.0 is 0, but would be written "-0.0" and miss an = against 0 on SQLite.
|
|
199
|
+
when BigDecimal then (value.zero? ? BigDecimal(0) : value).to_s("F")
|
|
137
200
|
when Time then value.getutc.strftime(TIME_FORMAT)
|
|
138
201
|
when Date then value.iso8601
|
|
139
202
|
else value
|
|
140
203
|
end
|
|
141
204
|
end
|
|
142
205
|
|
|
143
|
-
#
|
|
206
|
+
# JSON holds a boolean as one, where a column holds it as 0 or 1.
|
|
207
|
+
# @rbs items: Array[untyped]
|
|
208
|
+
# @rbs return: String
|
|
209
|
+
def encode_array(items)
|
|
210
|
+
JSON.generate(
|
|
211
|
+
items.map do |item|
|
|
212
|
+
case item
|
|
213
|
+
when true, false then item
|
|
214
|
+
else encode(item)
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
144
221
|
# @rbs value: untyped
|
|
145
222
|
# @rbs return: untyped
|
|
146
|
-
def decode(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
end
|
|
223
|
+
def decode(column, value)
|
|
224
|
+
element = column.element
|
|
225
|
+
return scalar(column.type, value) unless element
|
|
226
|
+
|
|
227
|
+
items = JSON.parse(value)
|
|
228
|
+
raise TypeError, "#{value} isn't a JSON array" unless items.is_a?(Array)
|
|
229
|
+
|
|
230
|
+
items.map { |entry| item(element, entry).freeze }.freeze
|
|
155
231
|
end
|
|
156
232
|
|
|
157
233
|
# @rbs (Skyward::ORM::_Connection connection) -> Array[Skyward::ORM::Catalog::Table]
|
|
158
234
|
def tables(connection)
|
|
159
235
|
index_rows = connection.execute(INDEXES).group_by(&:first)
|
|
160
236
|
connection.execute(COLUMNS).group_by(&:first).map do |name, rows|
|
|
237
|
+
sql = rows.fetch(0).fetch(1)
|
|
238
|
+
generated = Constraints.generated(sql)
|
|
161
239
|
columns = rows.map do |_, _, column, type, not_null, primary_key, default|
|
|
162
240
|
Skyward::ORM::Catalog::Column.new(
|
|
163
241
|
name: column.to_sym,
|
|
@@ -165,14 +243,15 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
165
243
|
# A primary key column can't hold NULL in practice, though SQLite only
|
|
166
244
|
# enforces it for an INTEGER PRIMARY KEY or a NOT NULL column.
|
|
167
245
|
nullable: not_null.zero? && primary_key.zero?,
|
|
168
|
-
default: default(default)
|
|
246
|
+
default: default(default),
|
|
247
|
+
generated: generated[column.to_sym]
|
|
169
248
|
)
|
|
170
249
|
end
|
|
171
250
|
Skyward::ORM::Catalog::Table.new(
|
|
172
251
|
name: name.to_sym,
|
|
173
252
|
columns:,
|
|
174
253
|
indexes: indexes(index_rows.fetch(name, [])),
|
|
175
|
-
constraints: Constraints.read(
|
|
254
|
+
constraints: Constraints.read(sql, columns.map(&:name))
|
|
176
255
|
)
|
|
177
256
|
end
|
|
178
257
|
end
|
|
@@ -222,6 +301,36 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
222
301
|
|
|
223
302
|
private
|
|
224
303
|
|
|
304
|
+
# @rbs type: Skyward::ORM::Tables::column_type
|
|
305
|
+
# @rbs value: untyped
|
|
306
|
+
# @rbs return: untyped
|
|
307
|
+
def scalar(type, value)
|
|
308
|
+
case type
|
|
309
|
+
when :bool then value != 0
|
|
310
|
+
when :decimal then BigDecimal(value)
|
|
311
|
+
when :date then Date.iso8601(value)
|
|
312
|
+
when :time then Time.new(value, in: "UTC")
|
|
313
|
+
when :enum then value.to_sym
|
|
314
|
+
when :json then JSON.parse(value, freeze: true)
|
|
315
|
+
# Rows are values, so even what the driver returns as it is comes back frozen.
|
|
316
|
+
else value.freeze
|
|
317
|
+
end
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# An Array[Float]'s JSON may hold an Integer, as an Array[Float] takes one.
|
|
321
|
+
# @rbs type: Skyward::ORM::Tables::column_type
|
|
322
|
+
# @rbs value: untyped
|
|
323
|
+
# @rbs return: untyped
|
|
324
|
+
def item(type, value)
|
|
325
|
+
case type
|
|
326
|
+
when :bool then value
|
|
327
|
+
when :float then Float(value)
|
|
328
|
+
# Only a String names a Symbol, and JSON may hold any value.
|
|
329
|
+
when :enum then String.try_convert(value)&.to_sym
|
|
330
|
+
else scalar(type, value)
|
|
331
|
+
end
|
|
332
|
+
end
|
|
333
|
+
|
|
225
334
|
# @rbs sql_type: String
|
|
226
335
|
# @rbs return: Symbol
|
|
227
336
|
def affinity(sql_type)
|
data/lib/skyward/orm/adapters.rb
CHANGED
|
@@ -9,12 +9,18 @@ module Skyward::ORM::Adapters
|
|
|
9
9
|
"sqlite" => SQLite
|
|
10
10
|
}.freeze #: Hash[String, Skyward::ORM::_Adapter]
|
|
11
11
|
|
|
12
|
+
# What comes before "://" is shown only when it's a scheme: in a malformed URL it can hold a password.
|
|
13
|
+
SCHEME = /\A[a-z][a-z0-9+.-]*\z/i #: Regexp
|
|
14
|
+
|
|
12
15
|
# @rbs (String url, schemes: Hash[String, Skyward::ORM::_Adapter], requirer: ^(String) -> void)
|
|
13
16
|
# -> [Skyward::ORM::_Adapter, String]
|
|
14
17
|
def resolve(url, schemes:, requirer:)
|
|
15
18
|
scheme, location = url.split("://", 2)
|
|
16
19
|
adapter = schemes[scheme.to_s] if location
|
|
17
|
-
|
|
20
|
+
unless adapter && location
|
|
21
|
+
raise Skyward::Core::BootError,
|
|
22
|
+
unsupported((scheme if location && scheme.to_s.match?(SCHEME)), schemes)
|
|
23
|
+
end
|
|
18
24
|
|
|
19
25
|
begin
|
|
20
26
|
# Required only now, so an app's Gemfile names just the one driver it uses.
|
|
@@ -33,6 +39,10 @@ module Skyward::ORM::Adapters
|
|
|
33
39
|
# @rbs (String? scheme, Hash[String, Skyward::ORM::_Adapter] schemes) -> String
|
|
34
40
|
def unsupported(scheme, schemes)
|
|
35
41
|
url = scheme ? "a #{scheme}:// URL" : "a URL with no scheme"
|
|
36
|
-
|
|
42
|
+
suggestion = Skyward::Core::Diagnostics::Suggestion.closest(scheme, schemes.keys) if scheme
|
|
43
|
+
hint = " Did you mean #{suggestion}://?" if suggestion
|
|
44
|
+
"Skyward ORM can't connect to #{url}. It connects to #{schemes.keys.map do |key|
|
|
45
|
+
"#{key}://"
|
|
46
|
+
end.join(", ")} URLs.#{hint}"
|
|
37
47
|
end
|
|
38
48
|
end
|
|
@@ -8,7 +8,7 @@ module Skyward::ORM::CLI::BootFile
|
|
|
8
8
|
# @rbs directory: String
|
|
9
9
|
# @rbs session: Skyward::ORM::Connection::Session
|
|
10
10
|
# @rbs generate: ^() -> String?
|
|
11
|
-
# @rbs return: Skyward::ORM::
|
|
11
|
+
# @rbs return: Skyward::ORM::Database | String
|
|
12
12
|
def load(directory, session, generate)
|
|
13
13
|
path = File.join(directory, FILE)
|
|
14
14
|
unless File.file?(path)
|
|
@@ -29,7 +29,7 @@ module Skyward::ORM::CLI::BootFile
|
|
|
29
29
|
# @rbs session: Skyward::ORM::Connection::Session
|
|
30
30
|
# @rbs kernel: singleton(Kernel)
|
|
31
31
|
# @rbs generate: ^() -> String?
|
|
32
|
-
# @rbs &: (Skyward::ORM::
|
|
32
|
+
# @rbs &: (Skyward::ORM::Database) -> void
|
|
33
33
|
# @rbs return: void
|
|
34
34
|
def boot(directory, session, kernel, generate, &)
|
|
35
35
|
database = session.without_check { load(directory, session, generate) }
|
|
@@ -15,7 +15,7 @@ module Skyward::ORM::CLI::Migrate
|
|
|
15
15
|
|
|
16
16
|
private
|
|
17
17
|
|
|
18
|
-
# @rbs (Skyward::ORM::
|
|
18
|
+
# @rbs (Skyward::ORM::Database database, directory: String, out: _Writer, kernel: singleton(Kernel),
|
|
19
19
|
# discover: ^() -> Array[singleton(Skyward::ORM::Record)]) -> void
|
|
20
20
|
def migrate(database, directory:, out:, kernel:, discover:)
|
|
21
21
|
runner = Skyward::ORM::Migrations::Runner
|
|
@@ -47,7 +47,7 @@ module Skyward::ORM::CLI::Migration
|
|
|
47
47
|
# change twice, so a pending one stops it. With no terminal to ask whether a
|
|
48
48
|
# column was renamed, it stops rather than guess, since a guess of no drops
|
|
49
49
|
# the column's data.
|
|
50
|
-
# @rbs (String name, Skyward::ORM::
|
|
50
|
+
# @rbs (String name, Skyward::ORM::Database database, directory: String, time: Time, input: IO,
|
|
51
51
|
# out: _Writer, kernel: singleton(Kernel), discover: ^() -> Array[singleton(Skyward::ORM::Record)]) -> void
|
|
52
52
|
def write(name, database, directory:, time:, input:, out:, kernel:, discover:)
|
|
53
53
|
ran = Skyward::ORM::Migrations::Versions.checksums(database).keys
|