skyward-orm 0.4.0 → 0.4.1
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/LICENSE +21 -0
- data/README.md +66 -48
- data/lib/skyward/orm/adapters/postgresql/connection.rb +64 -4
- data/lib/skyward/orm/adapters/postgresql.rb +95 -76
- data/lib/skyward/orm/adapters/sqlite/connection.rb +87 -0
- data/lib/skyward/orm/adapters/sqlite/constraints.rb +15 -15
- data/lib/skyward/orm/adapters/sqlite.rb +59 -55
- data/lib/skyward/orm/adapters/statement_cache.rb +51 -0
- data/lib/skyward/orm/adapters.rb +12 -6
- data/lib/skyward/orm/cli/boot_file.rb +2 -2
- data/lib/skyward/orm/cli/migration.rb +7 -7
- data/lib/skyward/orm/cli/scaffold/source.rb +49 -30
- data/lib/skyward/orm/cli/scaffold.rb +6 -6
- data/lib/skyward/orm/commands.rb +9 -9
- data/lib/skyward/orm/connection/instrumented.rb +4 -2
- data/lib/skyward/orm/connection/pool.rb +2 -2
- data/lib/skyward/orm/database/decoders.rb +41 -0
- data/lib/skyward/orm/database/schema_check.rb +3 -3
- data/lib/skyward/orm/database.rb +65 -31
- data/lib/skyward/orm/migrations/definitions.rb +13 -13
- data/lib/skyward/orm/migrations/diff.rb +4 -4
- data/lib/skyward/orm/migrations/directory.rb +5 -5
- data/lib/skyward/orm/migrations/runner.rb +8 -8
- data/lib/skyward/orm/migrations/script.rb +9 -9
- data/lib/skyward/orm/migrations/status.rb +7 -7
- data/lib/skyward/orm/migrations/summary.rb +22 -22
- data/lib/skyward/orm/migrations/versions.rb +2 -3
- data/lib/skyward/orm/projection.rb +6 -12
- data/lib/skyward/orm/queries/clauses.rb +201 -112
- data/lib/skyward/orm/query.rb +2 -2
- data/lib/skyward/orm/record.rb +5 -9
- data/lib/skyward/orm/statements/placeholders.rb +3 -3
- data/lib/skyward/orm/statements.rb +34 -28
- data/lib/skyward/orm/tables/column_types.rb +7 -7
- data/lib/skyward/orm/tables/declarations.rb +5 -5
- data/lib/skyward/orm/tables/rows.rb +28 -10
- data/lib/skyward/orm/tables/sources.rb +3 -3
- data/lib/skyward/orm/tables/values.rb +18 -18
- data/lib/skyward/orm/version.rb +1 -1
- data/lib/skyward/orm.rb +15 -14
- data/sig/generated/skyward/orm/adapters/postgresql/connection.rbs +35 -3
- data/sig/generated/skyward/orm/adapters/postgresql.rbs +14 -3
- data/sig/generated/skyward/orm/adapters/sqlite/connection.rbs +55 -0
- data/sig/generated/skyward/orm/adapters/sqlite.rbs +11 -6
- data/sig/generated/skyward/orm/adapters/statement_cache.rbs +37 -0
- data/sig/generated/skyward/orm/adapters.rbs +3 -0
- data/sig/generated/skyward/orm/cli/scaffold/source.rbs +11 -0
- data/sig/generated/skyward/orm/connection/instrumented.rbs +4 -2
- data/sig/generated/skyward/orm/database/decoders.rbs +19 -0
- data/sig/generated/skyward/orm/database.rbs +38 -14
- data/sig/generated/skyward/orm/queries/clauses.rbs +81 -28
- data/sig/generated/skyward/orm/record.rbs +6 -7
- data/sig/generated/skyward/orm/statements.rbs +8 -10
- data/sig/generated/skyward/orm/tables/rows.rbs +1 -1
- data/sig/generated/skyward/orm.rbs +2 -2
- data/sig/manual/pg.rbs +10 -0
- data/sig/manual/skyward/orm/database/decoders.rbs +3 -0
- data/sig/manual/skyward/orm.rbs +9 -3
- data/sig/manual/sqlite3.rbs +13 -2
- metadata +11 -3
|
@@ -4,28 +4,28 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
4
4
|
extend self
|
|
5
5
|
|
|
6
6
|
# The first SQLite with RETURNING, which every write into a row class uses.
|
|
7
|
-
MINIMUM_VERSION = Gem::Version.new(
|
|
7
|
+
MINIMUM_VERSION = Gem::Version.new('3.35') #: Gem::Version
|
|
8
8
|
|
|
9
9
|
SQL_TYPES = {
|
|
10
|
-
integer:
|
|
11
|
-
float:
|
|
12
|
-
decimal:
|
|
13
|
-
string:
|
|
14
|
-
bool:
|
|
15
|
-
date:
|
|
16
|
-
time:
|
|
17
|
-
enum:
|
|
18
|
-
uuid:
|
|
19
|
-
int32:
|
|
20
|
-
int16:
|
|
21
|
-
json:
|
|
22
|
-
array:
|
|
10
|
+
integer: 'INTEGER',
|
|
11
|
+
float: 'REAL',
|
|
12
|
+
decimal: 'TEXT',
|
|
13
|
+
string: 'TEXT',
|
|
14
|
+
bool: 'INTEGER',
|
|
15
|
+
date: 'TEXT',
|
|
16
|
+
time: 'TEXT',
|
|
17
|
+
enum: 'TEXT',
|
|
18
|
+
uuid: 'TEXT',
|
|
19
|
+
int32: 'INTEGER',
|
|
20
|
+
int16: 'INTEGER',
|
|
21
|
+
json: 'TEXT',
|
|
22
|
+
array: 'TEXT'
|
|
23
23
|
}.freeze #: Hash[Skyward::ORM::Tables::column_type, String]
|
|
24
24
|
|
|
25
25
|
# Fixed width and UTC, so text order is time order, and to the microsecond,
|
|
26
26
|
# as PostgreSQL keeps it. It's the format of SQLite's own CURRENT_TIMESTAMP,
|
|
27
27
|
# with the fraction added, so the two compare.
|
|
28
|
-
TIME_FORMAT =
|
|
28
|
+
TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%6N' #: String
|
|
29
29
|
|
|
30
30
|
# SQLite's extended result codes: SQLITE_CONSTRAINT_CHECK, _FOREIGNKEY,
|
|
31
31
|
# _NOTNULL, _PRIMARYKEY and _UNIQUE.
|
|
@@ -40,7 +40,7 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
40
40
|
# SQLite enforces a RESTRICT foreign key as a trigger would, so it reports
|
|
41
41
|
# SQLITE_CONSTRAINT_TRIGGER, and only the message tells it from a trigger's RAISE.
|
|
42
42
|
TRIGGER = 1811 #: Integer
|
|
43
|
-
RESTRICTED =
|
|
43
|
+
RESTRICTED = 'FOREIGN KEY constraint failed' #: String
|
|
44
44
|
|
|
45
45
|
# Types whose values keep their meaning under NUMERIC affinity, which Rails
|
|
46
46
|
# gives BOOLEAN, DATE and DATETIME. A boolean is already a number, and a date
|
|
@@ -52,9 +52,9 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
52
52
|
# in DATETIME(6). Nothing else in the catalog tells a bool, Date or Time from
|
|
53
53
|
# an Integer or String, since a migration stores them as INTEGER and TEXT.
|
|
54
54
|
NUMERIC_NAMES = {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
'BOOLEAN' => :bool,
|
|
56
|
+
'DATE' => :date,
|
|
57
|
+
'DATETIME' => :time
|
|
58
58
|
}.freeze #: Hash[String, Skyward::ORM::Tables::column_type]
|
|
59
59
|
PRECISION = /\s*\(.*\)\z/ #: Regexp
|
|
60
60
|
|
|
@@ -90,7 +90,7 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
90
90
|
LITERAL = /\A(?:'(?:[^']|'')*'|x'\h*'|[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?|0x\h+|null|true|false)\z/i #: Regexp
|
|
91
91
|
|
|
92
92
|
# @rbs return: String
|
|
93
|
-
def driver =
|
|
93
|
+
def driver = 'sqlite3'
|
|
94
94
|
|
|
95
95
|
# @rbs return: Symbol
|
|
96
96
|
def adapter_name = :sqlite
|
|
@@ -103,9 +103,9 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
103
103
|
# busy_timeout= would hold the GVL while it waits, so no other thread or
|
|
104
104
|
# fiber could run, including the one holding the lock.
|
|
105
105
|
connection.busy_handler_timeout = BUSY_TIMEOUT_MS
|
|
106
|
-
connection.execute(
|
|
107
|
-
require_version(connection.execute(
|
|
108
|
-
connection
|
|
106
|
+
connection.execute('PRAGMA foreign_keys = ON')
|
|
107
|
+
require_version(connection.execute('SELECT sqlite_version()').dig(0, 0).to_s)
|
|
108
|
+
Connection.new(connection, Skyward::ORM::Adapters::StatementCache.new)
|
|
109
109
|
end
|
|
110
110
|
|
|
111
111
|
# Each connection to :memory: opens its own empty database, so there can be
|
|
@@ -113,7 +113,7 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
113
113
|
# @rbs location: String
|
|
114
114
|
# @rbs size: Integer
|
|
115
115
|
# @rbs return: Integer
|
|
116
|
-
def pool_size(location, size) = location ==
|
|
116
|
+
def pool_size(location, size) = location == ':memory:' ? 1 : size
|
|
117
117
|
|
|
118
118
|
# @rbs version: String
|
|
119
119
|
# @rbs return: void
|
|
@@ -126,7 +126,7 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
126
126
|
end
|
|
127
127
|
|
|
128
128
|
# @rbs (Integer _position) -> String
|
|
129
|
-
def placeholder(_position) =
|
|
129
|
+
def placeholder(_position) = '?'
|
|
130
130
|
|
|
131
131
|
# SQLITE_MAX_VARIABLE_NUMBER's default since 3.32. A SQLite built with a
|
|
132
132
|
# lower one refuses with its own error, since the sqlite3 gem doesn't expose
|
|
@@ -136,7 +136,7 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
136
136
|
|
|
137
137
|
# SQLite takes an OFFSET only after a LIMIT, and a negative one is no limit.
|
|
138
138
|
# @rbs return: String
|
|
139
|
-
def unlimited =
|
|
139
|
+
def unlimited = 'LIMIT -1'
|
|
140
140
|
|
|
141
141
|
# @rbs column: Skyward::ORM::Tables::Column
|
|
142
142
|
# @rbs return: String?
|
|
@@ -184,7 +184,7 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
184
184
|
|
|
185
185
|
# @rbs sql_type: String
|
|
186
186
|
# @rbs return: Skyward::ORM::Tables::column_type?
|
|
187
|
-
def column_type(sql_type) = NUMERIC_NAMES[sql_type.upcase.sub(PRECISION,
|
|
187
|
+
def column_type(sql_type) = NUMERIC_NAMES[sql_type.upcase.sub(PRECISION, '')] || AFFINITY_TYPES[affinity(sql_type)]
|
|
188
188
|
|
|
189
189
|
# @rbs value: untyped
|
|
190
190
|
# @rbs return: untyped
|
|
@@ -196,7 +196,7 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
196
196
|
# A decimal is stored as text, which = compares, so it's always written the
|
|
197
197
|
# same way, without the exponent BigDecimal#to_s writes, as 0.15e1.
|
|
198
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(
|
|
199
|
+
when BigDecimal then (value.zero? ? BigDecimal(0) : value).to_s('F')
|
|
200
200
|
when Time then value.getutc.strftime(TIME_FORMAT)
|
|
201
201
|
when Date then value.iso8601
|
|
202
202
|
else value
|
|
@@ -218,18 +218,22 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
218
218
|
end
|
|
219
219
|
|
|
220
220
|
# @rbs column: Skyward::ORM::Tables::Column
|
|
221
|
-
# @rbs value:
|
|
222
|
-
# @rbs return:
|
|
223
|
-
def
|
|
221
|
+
# @rbs value: String
|
|
222
|
+
# @rbs return: String
|
|
223
|
+
def decoding(column, value)
|
|
224
224
|
element = column.element
|
|
225
225
|
return scalar(column.type, value) unless element
|
|
226
226
|
|
|
227
|
-
|
|
228
|
-
raise
|
|
229
|
-
|
|
230
|
-
items.map { |entry| item(element, entry).freeze }.freeze
|
|
227
|
+
# Of the values JSON holds, only an Array converts to one.
|
|
228
|
+
"(::Array.try_convert(::JSON.parse(#{value})) || raise(::TypeError, \"\#{#{value}} isn't a JSON array\"))" \
|
|
229
|
+
".map { |item| (#{item(element, 'item')}).freeze }.freeze"
|
|
231
230
|
end
|
|
232
231
|
|
|
232
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
233
|
+
# @rbs value: untyped
|
|
234
|
+
# @rbs return: untyped
|
|
235
|
+
def decode(column, value) = Skyward::ORM::Adapters.decode(self, column, value)
|
|
236
|
+
|
|
233
237
|
# @rbs (Skyward::ORM::_Connection connection) -> Array[Skyward::ORM::Catalog::Table]
|
|
234
238
|
def tables(connection)
|
|
235
239
|
index_rows = connection.execute(INDEXES).group_by(&:first)
|
|
@@ -262,17 +266,17 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
262
266
|
# IMMEDIATE takes the write lock as the transaction begins rather than at its
|
|
263
267
|
# first write, so a second migrator waits there, and then sees what the first ran.
|
|
264
268
|
# @rbs return: String
|
|
265
|
-
def begin_migration =
|
|
269
|
+
def begin_migration = 'PRAGMA foreign_keys = OFF; BEGIN IMMEDIATE'
|
|
266
270
|
|
|
267
271
|
# @rbs (Skyward::ORM::_Connection connection) -> Array[String]
|
|
268
272
|
def foreign_key_violations(connection)
|
|
269
|
-
connection.execute(
|
|
273
|
+
connection.execute('PRAGMA foreign_key_check').map do |table, rowid, parent, key|
|
|
270
274
|
"#{table} row #{rowid} refers to a missing #{parent} row (foreign key #{key})"
|
|
271
275
|
end
|
|
272
276
|
end
|
|
273
277
|
|
|
274
278
|
# @rbs return: String
|
|
275
|
-
def end_migration =
|
|
279
|
+
def end_migration = 'PRAGMA foreign_keys = ON'
|
|
276
280
|
|
|
277
281
|
# The code says which kind of constraint failed. The message names the
|
|
278
282
|
# columns of a unique or not-null one, and the name of a check, and the rest
|
|
@@ -302,31 +306,31 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
302
306
|
private
|
|
303
307
|
|
|
304
308
|
# @rbs type: Skyward::ORM::Tables::column_type
|
|
305
|
-
# @rbs value:
|
|
306
|
-
# @rbs return:
|
|
309
|
+
# @rbs value: String
|
|
310
|
+
# @rbs return: String
|
|
307
311
|
def scalar(type, value)
|
|
308
312
|
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)
|
|
313
|
+
when :bool then "#{value} != 0"
|
|
314
|
+
when :decimal then "BigDecimal(#{value})"
|
|
315
|
+
when :date then "::Date.iso8601(#{value})"
|
|
316
|
+
when :time then "::Time.new(#{value}, in: \"UTC\")"
|
|
317
|
+
when :enum then "#{value}.to_sym"
|
|
318
|
+
when :json then "::JSON.parse(#{value}, freeze: true)"
|
|
315
319
|
# Rows are values, so even what the driver returns as it is comes back frozen.
|
|
316
|
-
else value.freeze
|
|
320
|
+
else "#{value}.freeze"
|
|
317
321
|
end
|
|
318
322
|
end
|
|
319
323
|
|
|
320
324
|
# An Array[Float]'s JSON may hold an Integer, as an Array[Float] takes one.
|
|
321
325
|
# @rbs type: Skyward::ORM::Tables::column_type
|
|
322
|
-
# @rbs value:
|
|
323
|
-
# @rbs return:
|
|
326
|
+
# @rbs value: String
|
|
327
|
+
# @rbs return: String
|
|
324
328
|
def item(type, value)
|
|
325
329
|
case type
|
|
326
330
|
when :bool then value
|
|
327
|
-
when :float then Float(value)
|
|
331
|
+
when :float then "Float(#{value})"
|
|
328
332
|
# Only a String names a Symbol, and JSON may hold any value.
|
|
329
|
-
when :enum then String.try_convert(value)&.to_sym
|
|
333
|
+
when :enum then "::String.try_convert(#{value})&.to_sym"
|
|
330
334
|
else scalar(type, value)
|
|
331
335
|
end
|
|
332
336
|
end
|
|
@@ -335,9 +339,9 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
335
339
|
# @rbs return: Symbol
|
|
336
340
|
def affinity(sql_type)
|
|
337
341
|
declared = sql_type.upcase
|
|
338
|
-
if declared.include?(
|
|
342
|
+
if declared.include?('INT') then :integer
|
|
339
343
|
elsif declared.match?(/CHAR|CLOB|TEXT/) then :text
|
|
340
|
-
elsif declared.empty? || declared.include?(
|
|
344
|
+
elsif declared.empty? || declared.include?('BLOB') then :blob
|
|
341
345
|
elsif declared.match?(/REAL|FLOA|DOUB/) then :real
|
|
342
346
|
else :numeric
|
|
343
347
|
end
|
|
@@ -416,11 +420,11 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
416
420
|
# @rbs message: String
|
|
417
421
|
# @rbs return: [Symbol, Array[Symbol]]
|
|
418
422
|
def failed_columns(message)
|
|
419
|
-
names = detail(message).split(
|
|
423
|
+
names = detail(message).split(', ').map { |name| name.split('.', 2) }
|
|
420
424
|
[names.fetch(0).fetch(0).to_sym, names.map { |name| name.fetch(-1).to_sym }]
|
|
421
425
|
end
|
|
422
426
|
|
|
423
427
|
# @rbs message: String
|
|
424
428
|
# @rbs return: String
|
|
425
|
-
def detail(message) = message.split(
|
|
429
|
+
def detail(message) = message.split(': ', 2).last.to_s
|
|
426
430
|
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Only one fiber uses a connection at a time, so its cache takes no lock.
|
|
4
|
+
# @rbs generic S
|
|
5
|
+
class Skyward::ORM::Adapters::StatementCache
|
|
6
|
+
LIMIT = 1_000 #: Integer
|
|
7
|
+
|
|
8
|
+
# A statement with more binds isn't kept. Varying IN lists and multi-row
|
|
9
|
+
# inserts are what reach it: each length is different SQL that would churn
|
|
10
|
+
# the cache, PostgreSQL keeps a large plan for each, and parsing is a small
|
|
11
|
+
# part of their cost.
|
|
12
|
+
MAX_BINDS = 100 #: Integer
|
|
13
|
+
|
|
14
|
+
# @rbs @limit: Integer
|
|
15
|
+
# @rbs @statements: Hash[String, S]
|
|
16
|
+
|
|
17
|
+
# @rbs limit: Integer
|
|
18
|
+
# @rbs return: void
|
|
19
|
+
def initialize(limit = LIMIT)
|
|
20
|
+
@limit = limit
|
|
21
|
+
@statements = {}
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# @rbs binds: Array[untyped]
|
|
25
|
+
# @rbs return: bool
|
|
26
|
+
def keeps?(binds) = binds.size <= MAX_BINDS
|
|
27
|
+
|
|
28
|
+
# @rbs sql: String
|
|
29
|
+
# @rbs return: S?
|
|
30
|
+
def fetch(sql)
|
|
31
|
+
# A Hash keeps its keys in insertion order, so moving each hit to the end
|
|
32
|
+
# keeps the least recently used first.
|
|
33
|
+
statement = @statements.delete(sql)
|
|
34
|
+
@statements[sql] = statement if statement
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @rbs sql: String
|
|
38
|
+
# @rbs statement: S
|
|
39
|
+
# @rbs return: S?
|
|
40
|
+
def store(sql, statement)
|
|
41
|
+
@statements[sql] = statement
|
|
42
|
+
@statements.shift&.last if @statements.size > @limit
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @rbs return: Array[S]
|
|
46
|
+
def clear
|
|
47
|
+
cleared = @statements.values
|
|
48
|
+
@statements = {}
|
|
49
|
+
cleared
|
|
50
|
+
end
|
|
51
|
+
end
|
data/lib/skyward/orm/adapters.rb
CHANGED
|
@@ -4,9 +4,9 @@ module Skyward::ORM::Adapters
|
|
|
4
4
|
extend self
|
|
5
5
|
|
|
6
6
|
SCHEMES = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
'postgres' => PostgreSQL,
|
|
8
|
+
'postgresql' => PostgreSQL,
|
|
9
|
+
'sqlite' => SQLite
|
|
10
10
|
}.freeze #: Hash[String, Skyward::ORM::_Adapter]
|
|
11
11
|
|
|
12
12
|
# What comes before "://" is shown only when it's a scheme: in a malformed URL it can hold a password.
|
|
@@ -15,7 +15,7 @@ module Skyward::ORM::Adapters
|
|
|
15
15
|
# @rbs (String url, schemes: Hash[String, Skyward::ORM::_Adapter], requirer: ^(String) -> void)
|
|
16
16
|
# -> [Skyward::ORM::_Adapter, String]
|
|
17
17
|
def resolve(url, schemes:, requirer:)
|
|
18
|
-
scheme, location = url.split(
|
|
18
|
+
scheme, location = url.split('://', 2)
|
|
19
19
|
adapter = schemes[scheme.to_s] if location
|
|
20
20
|
unless adapter && location
|
|
21
21
|
raise Skyward::Core::BootError,
|
|
@@ -33,16 +33,22 @@ module Skyward::ORM::Adapters
|
|
|
33
33
|
[adapter, location]
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
+
# @rbs (Skyward::ORM::_Adapter adapter, Skyward::ORM::Tables::Column column, untyped value) -> untyped
|
|
37
|
+
def decode(adapter, column, value)
|
|
38
|
+
source = "->(value) { #{adapter.decoding(column, 'value')} }"
|
|
39
|
+
instance_eval(source, __FILE__, __LINE__).call(value)
|
|
40
|
+
end
|
|
41
|
+
|
|
36
42
|
private
|
|
37
43
|
|
|
38
44
|
# The URL can hold a password, so only its scheme is shown.
|
|
39
45
|
# @rbs (String? scheme, Hash[String, Skyward::ORM::_Adapter] schemes) -> String
|
|
40
46
|
def unsupported(scheme, schemes)
|
|
41
|
-
url = scheme ? "a #{scheme}:// URL" :
|
|
47
|
+
url = scheme ? "a #{scheme}:// URL" : 'a URL with no scheme'
|
|
42
48
|
suggestion = Skyward::Core::Diagnostics::Suggestion.closest(scheme, schemes.keys) if scheme
|
|
43
49
|
hint = " Did you mean #{suggestion}://?" if suggestion
|
|
44
50
|
"Skyward ORM can't connect to #{url}. It connects to #{schemes.keys.map do |key|
|
|
45
51
|
"#{key}://"
|
|
46
|
-
end.join(
|
|
52
|
+
end.join(', ')} URLs.#{hint}"
|
|
47
53
|
end
|
|
48
54
|
end
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
module Skyward::ORM::CLI::BootFile
|
|
4
4
|
extend self
|
|
5
5
|
|
|
6
|
-
FILE =
|
|
6
|
+
FILE = 'boot.rb' #: String
|
|
7
7
|
|
|
8
8
|
# @rbs directory: String
|
|
9
9
|
# @rbs session: Skyward::ORM::Connection::Session
|
|
@@ -13,7 +13,7 @@ module Skyward::ORM::CLI::BootFile
|
|
|
13
13
|
path = File.join(directory, FILE)
|
|
14
14
|
unless File.file?(path)
|
|
15
15
|
return "skyward: no #{FILE} in #{directory}. Run skyward where your app's #{FILE} loads its table classes " \
|
|
16
|
-
|
|
16
|
+
'and calls Skyward.connect.'
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
failure = generate.call
|
|
@@ -5,11 +5,11 @@ module Skyward::ORM::CLI::Migration
|
|
|
5
5
|
|
|
6
6
|
NAME = /\A[A-Za-z0-9_]+\z/ #: Regexp
|
|
7
7
|
|
|
8
|
-
EMPTY =
|
|
8
|
+
EMPTY = '--empty' #: String
|
|
9
9
|
|
|
10
10
|
USAGE = "Usage: skyward migration [--empty] NAME\n" \
|
|
11
11
|
"NAME is letters, digits and _, such as add_notes_to_todos.\n" \
|
|
12
|
-
|
|
12
|
+
'--empty writes an empty migration for SQL you write yourself.' #: String
|
|
13
13
|
|
|
14
14
|
NOTHING = "Every table matches its class. Nothing to write.\n" #: String
|
|
15
15
|
|
|
@@ -19,7 +19,7 @@ module Skyward::ORM::CLI::Migration
|
|
|
19
19
|
# session: Skyward::ORM::Connection::Session, discover: ^() -> Array[singleton(Skyward::ORM::Record)],
|
|
20
20
|
# generate: ^() -> String?) -> void
|
|
21
21
|
def run(args, directory:, time:, input:, out:, kernel:, session:, discover:, generate:)
|
|
22
|
-
flags, names = args.partition { |arg| arg.start_with?(
|
|
22
|
+
flags, names = args.partition { |arg| arg.start_with?('-') }
|
|
23
23
|
name = names.fetch(0) if names.one?
|
|
24
24
|
empty = flags == [EMPTY]
|
|
25
25
|
return kernel.abort(USAGE) unless name&.match?(NAME) && (empty || flags.empty?)
|
|
@@ -98,14 +98,14 @@ module Skyward::ORM::CLI::Migration
|
|
|
98
98
|
# @rbs renames: Array[String]
|
|
99
99
|
# @rbs return: String
|
|
100
100
|
def unasked(renames)
|
|
101
|
-
"skyward: did you rename #{renames.join(
|
|
102
|
-
|
|
101
|
+
"skyward: did you rename #{renames.join(', or ')}? There's no terminal to ask, so nothing was written. " \
|
|
102
|
+
'Run `skyward migration` in a terminal to answer.'
|
|
103
103
|
end
|
|
104
104
|
|
|
105
105
|
# @rbs files: Array[String]
|
|
106
106
|
# @rbs return: String
|
|
107
107
|
def pending(files)
|
|
108
|
-
"skyward: #{files.join(
|
|
109
|
-
|
|
108
|
+
"skyward: #{files.join(', ')} #{files.one? ? "hasn't" : "haven't"} run yet. " \
|
|
109
|
+
'Run `skyward migrate` before writing the next migration.'
|
|
110
110
|
end
|
|
111
111
|
end
|
|
@@ -8,7 +8,7 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
8
8
|
|
|
9
9
|
# Only a trailing s is dropped: ies becomes y, sses becomes ss, and ss stays.
|
|
10
10
|
SINGULAR = /ies\z|sses\z|(?<!s)s\z/ #: Regexp
|
|
11
|
-
SINGULARS = {
|
|
11
|
+
SINGULARS = { 'ies' => 'y', 'sses' => 'ss' }.freeze #: Hash[String, String]
|
|
12
12
|
|
|
13
13
|
QUOTED = /'((?:[^']|'')*)'/ #: Regexp
|
|
14
14
|
LITERAL = /\A#{QUOTED}\z/ #: Regexp
|
|
@@ -17,7 +17,7 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
17
17
|
# a default back with a cast, so no Time is written back the same there.
|
|
18
18
|
ISO_TIME = /\A(\d{4}-\d{2}-\d{2}) (?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d{1,6})?\z/ #: Regexp
|
|
19
19
|
|
|
20
|
-
BOOLS = {
|
|
20
|
+
BOOLS = { 'true' => true, 'false' => false, '1' => true, '0' => false }.freeze #: Hash[String, bool]
|
|
21
21
|
|
|
22
22
|
ENUM = Skyward::ORM::Tables::Column.new(
|
|
23
23
|
name: :enum, type: :enum, nullable: false, symbols: [], element: nil
|
|
@@ -41,8 +41,8 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
41
41
|
REFERENCED = /\bREFERENCES\s+(?:"(?:[^"]|"")*"|[^\s(]+)\s*(?:\(([^)]*)\))?/i #: Regexp
|
|
42
42
|
|
|
43
43
|
DROPPED = " # `skyward migration` will drop these, since a table class can't declare them:\n" #: String
|
|
44
|
-
REWRITTEN =
|
|
45
|
-
|
|
44
|
+
REWRITTEN = '`skyward migration` will rewrite these defaults once, as the values below, since SQLite keeps each ' \
|
|
45
|
+
'as it was written: ' #: String
|
|
46
46
|
|
|
47
47
|
# @rbs (Skyward::ORM::Catalog::Table table, Array[Skyward::ORM::Catalog::Table] tables,
|
|
48
48
|
# Skyward::ORM::_Adapter adapter, Array[singleton(Skyward::ORM::Record)] classes) -> String
|
|
@@ -57,9 +57,9 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
57
57
|
"# rbs_inline: enabled\n",
|
|
58
58
|
("# @rbs use Skyward::ORM::*\n" if refined),
|
|
59
59
|
"\nclass #{class_name(table.name)} < Skyward::ORM::Record\n",
|
|
60
|
-
" table #{table.name
|
|
60
|
+
" table #{literal(table.name)}\n",
|
|
61
61
|
*declarations(table, tables, adapter, classes, checks - unions.values.map(&:first), types),
|
|
62
|
-
"\n # @dynamic #{table.columns.map(&:name).join(
|
|
62
|
+
"\n # @dynamic #{table.columns.map(&:name).join(', ')}\n",
|
|
63
63
|
*readers(table.columns, unions, types),
|
|
64
64
|
"end\n"
|
|
65
65
|
].compact.join
|
|
@@ -68,7 +68,7 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
68
68
|
# @rbs table: Symbol
|
|
69
69
|
# @rbs return: String
|
|
70
70
|
def class_name(table)
|
|
71
|
-
table.name.split(
|
|
71
|
+
table.name.split('_').map(&:capitalize).join.sub(SINGULAR) { |ending| SINGULARS.fetch(ending, '') }
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
# @rbs table: Skyward::ORM::Catalog::Table?
|
|
@@ -94,29 +94,29 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
94
94
|
declared, unread = defaults.partition { |_, value| value }
|
|
95
95
|
rewritten = declared.reject(&:last).map { |column, _| column.name }
|
|
96
96
|
undeclared = [*unread.map(&:first), *unreferenced, *unchecked, *unindexed].map do |undeclarable|
|
|
97
|
-
comment(definition(undeclarable),
|
|
97
|
+
comment(definition(undeclarable), ' # ')
|
|
98
98
|
end
|
|
99
99
|
[
|
|
100
100
|
(DROPPED unless undeclared.empty?),
|
|
101
101
|
*undeclared,
|
|
102
|
-
(" primary_key #{key
|
|
102
|
+
(" primary_key #{literal(key)}\n" unless key == :id),
|
|
103
103
|
*references.flat_map { |reference| reference(table.name, reference, classes) },
|
|
104
|
-
(comment("#{REWRITTEN}#{rewritten.join(
|
|
105
|
-
(" default #{declared.map { |column, value| "#{column.name}: #{value}" }.join(
|
|
106
|
-
*table.constraints.select { |constraint| constraint.kind == :unique }.map { |unique| columns(
|
|
104
|
+
(comment("#{REWRITTEN}#{rewritten.join(', ')}") unless rewritten.empty?),
|
|
105
|
+
(" default #{declared.map { |column, value| "#{column.name}: #{value}" }.join(', ')}\n" unless declared.empty?),
|
|
106
|
+
*table.constraints.select { |constraint| constraint.kind == :unique }.map { |unique| columns('unique', unique) },
|
|
107
107
|
*unique_indexes.flat_map do |index|
|
|
108
108
|
[
|
|
109
109
|
comment(
|
|
110
110
|
"`skyward migration` will replace the unique index #{index.name} with a constraint on the same " \
|
|
111
|
-
|
|
111
|
+
'columns.'
|
|
112
112
|
),
|
|
113
|
-
columns(
|
|
113
|
+
columns('unique', index)
|
|
114
114
|
]
|
|
115
115
|
end,
|
|
116
116
|
*column_checks.map do |check|
|
|
117
|
-
" check #{check.columns.fetch(0)
|
|
117
|
+
" check #{literal(check.columns.fetch(0))}, Skyward.sql(#{literal(rule(check))})\n"
|
|
118
118
|
end,
|
|
119
|
-
*indexes.map { |index| columns(
|
|
119
|
+
*indexes.map { |index| columns('index', index) }
|
|
120
120
|
].compact
|
|
121
121
|
end
|
|
122
122
|
|
|
@@ -130,9 +130,9 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
130
130
|
name = column.name
|
|
131
131
|
column_type = types[name]
|
|
132
132
|
symbols = unions[name]&.last
|
|
133
|
-
type = if symbols then symbols.map(&:inspect).join(
|
|
133
|
+
type = if symbols then symbols.map(&:inspect).join(' | ')
|
|
134
134
|
elsif column_type then Skyward::ORM::Tables::Values::WRITTEN.fetch(column_type)
|
|
135
|
-
else
|
|
135
|
+
else 'untyped'
|
|
136
136
|
end
|
|
137
137
|
rbs = if column_type.nil? || !column.nullable then type
|
|
138
138
|
elsif symbols then "(#{type})?"
|
|
@@ -157,7 +157,7 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
157
157
|
|
|
158
158
|
found = checks.find do |check|
|
|
159
159
|
rule = rule(check)
|
|
160
|
-
residue = rule.gsub(QUOTED,
|
|
160
|
+
residue = rule.gsub(QUOTED, '').gsub(CAST, '').gsub(PUNCTUATION, '').upcase
|
|
161
161
|
check.columns == [column.name] && rule.match?(QUOTED) &&
|
|
162
162
|
LISTS.any? { |list| residue == "#{column.name.upcase}#{list}" }
|
|
163
163
|
end
|
|
@@ -187,7 +187,7 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
187
187
|
return [column, literal, true] if literal
|
|
188
188
|
|
|
189
189
|
expression = [sql, sql[PARENTHESIZED, 1]].compact.find { |candidate| adapter.default_expression(candidate) == sql }
|
|
190
|
-
return [column, "Skyward.sql(#{expression
|
|
190
|
+
return [column, "Skyward.sql(#{literal(expression)})", true] if expression
|
|
191
191
|
|
|
192
192
|
[column, (ruby(read.first) unless read.empty?), false]
|
|
193
193
|
end
|
|
@@ -226,7 +226,7 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
226
226
|
# @rbs string: String?
|
|
227
227
|
# @rbs return: Time?
|
|
228
228
|
def time(string)
|
|
229
|
-
Time.new(string, in:
|
|
229
|
+
Time.new(string, in: 'UTC') if string && date(string[ISO_TIME, 1])
|
|
230
230
|
end
|
|
231
231
|
|
|
232
232
|
# @rbs value: untyped
|
|
@@ -237,11 +237,30 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
237
237
|
when Time
|
|
238
238
|
parts = [value.year, value.month, value.day, value.hour, value.min, value.sec,
|
|
239
239
|
(value.usec unless value.usec.zero?)]
|
|
240
|
-
"Time.utc(#{parts.compact.join(
|
|
240
|
+
"Time.utc(#{parts.compact.join(', ')})"
|
|
241
|
+
else literal(value)
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# Ruby source for a value in the shared style: a String or a quoted Symbol in
|
|
246
|
+
# single quotes, unless it holds a quote, a backslash or a character only an
|
|
247
|
+
# escape can write, which double quotes are for.
|
|
248
|
+
# @rbs value: untyped
|
|
249
|
+
# @rbs return: String
|
|
250
|
+
def literal(value)
|
|
251
|
+
case value
|
|
252
|
+
when String then single_quotable?(value) ? "'#{value}'" : value.inspect
|
|
253
|
+
when Symbol
|
|
254
|
+
written = value.inspect
|
|
255
|
+
written.start_with?(':"') && single_quotable?(value.name) ? ":'#{value.name}'" : written
|
|
241
256
|
else value.inspect
|
|
242
257
|
end
|
|
243
258
|
end
|
|
244
259
|
|
|
260
|
+
# @rbs text: String
|
|
261
|
+
# @rbs return: bool
|
|
262
|
+
def single_quotable?(text) = !text.match?(/['\\]/) && text.match?(/\A[[:print:]]*\z/)
|
|
263
|
+
|
|
245
264
|
# A foreign key a class declares is on one column, and refers to the
|
|
246
265
|
# primary key of the table it references.
|
|
247
266
|
# @rbs constraint: Skyward::ORM::Catalog::Constraint
|
|
@@ -249,7 +268,7 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
249
268
|
# @rbs return: bool
|
|
250
269
|
def reference?(constraint, tables)
|
|
251
270
|
key = primary_key(tables.find { |table| table.name == constraint.references })
|
|
252
|
-
listed = constraint.definition[REFERENCED, 1]&.split(
|
|
271
|
+
listed = constraint.definition[REFERENCED, 1]&.split(',')&.map { |column| column.strip.delete('"').to_sym }
|
|
253
272
|
constraint.columns.one? && key.one? && (listed.nil? || listed == key)
|
|
254
273
|
end
|
|
255
274
|
|
|
@@ -261,9 +280,9 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
261
280
|
# reference? found the table it references.
|
|
262
281
|
referenced = constraint.references #: Symbol
|
|
263
282
|
owner = classes.find { |klass| klass.table_name == referenced }
|
|
264
|
-
action = constraint.definition[Skyward::ORM::Migrations::Diff::ON_DELETE, 1].to_s.downcase.split.join(
|
|
265
|
-
on_delete = ", on_delete: :#{action}" unless [
|
|
266
|
-
line = " references #{constraint.columns.fetch(0)
|
|
283
|
+
action = constraint.definition[Skyward::ORM::Migrations::Diff::ON_DELETE, 1].to_s.downcase.split.join('_')
|
|
284
|
+
on_delete = ", on_delete: :#{action}" unless ['', 'no_action'].include?(action)
|
|
285
|
+
line = " references #{literal(constraint.columns.fetch(0))}, #{owner || class_name(referenced)}#{on_delete}\n"
|
|
267
286
|
return [line] if owner || referenced == table
|
|
268
287
|
|
|
269
288
|
[comment("#{referenced} has no class yet. Scaffold it too: skyward scaffold #{referenced}"), line]
|
|
@@ -273,7 +292,7 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
273
292
|
# @rbs return: bool
|
|
274
293
|
def plain?(index)
|
|
275
294
|
keys = index.definition[PLAIN_INDEX, 1]
|
|
276
|
-
!keys.nil? && keys.split(
|
|
295
|
+
!keys.nil? && keys.split(',').map { |key| key.strip.delete('"') } == index.columns.map(&:name)
|
|
277
296
|
end
|
|
278
297
|
|
|
279
298
|
# @rbs check: Skyward::ORM::Catalog::Constraint
|
|
@@ -283,14 +302,14 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
283
302
|
# @rbs declaration: String
|
|
284
303
|
# @rbs key: Skyward::ORM::Catalog::Constraint | Skyward::ORM::Catalog::Index
|
|
285
304
|
# @rbs return: String
|
|
286
|
-
def columns(declaration, key) = " #{declaration} #{key.columns.map(
|
|
305
|
+
def columns(declaration, key) = " #{declaration} #{key.columns.map { |column| literal(column) }.join(', ')}\n"
|
|
287
306
|
|
|
288
307
|
# Every line of the text is commented, since a definition or name the
|
|
289
308
|
# database keeps can hold a newline.
|
|
290
309
|
# @rbs text: String
|
|
291
310
|
# @rbs prefix: String
|
|
292
311
|
# @rbs return: String
|
|
293
|
-
def comment(text, prefix =
|
|
312
|
+
def comment(text, prefix = ' # ') = "#{text.gsub(/^/, prefix)}\n"
|
|
294
313
|
|
|
295
314
|
# @rbs undeclarable: Skyward::ORM::Catalog::Column | Skyward::ORM::Catalog::Constraint | Skyward::ORM::Catalog::Index
|
|
296
315
|
# @rbs return: String
|
|
@@ -298,7 +317,7 @@ module Skyward::ORM::CLI::Scaffold::Source
|
|
|
298
317
|
case undeclarable
|
|
299
318
|
when Skyward::ORM::Catalog::Column then "#{undeclarable.name} DEFAULT #{undeclarable.default}"
|
|
300
319
|
when Skyward::ORM::Catalog::Index then undeclarable.definition
|
|
301
|
-
else [("CONSTRAINT #{undeclarable.name}" if undeclarable.name), undeclarable.definition].compact.join(
|
|
320
|
+
else [("CONSTRAINT #{undeclarable.name}" if undeclarable.name), undeclarable.definition].compact.join(' ')
|
|
302
321
|
end
|
|
303
322
|
end
|
|
304
323
|
end
|
|
@@ -18,7 +18,7 @@ module Skyward::ORM::CLI::Scaffold
|
|
|
18
18
|
# generate: ^() -> String?) -> void
|
|
19
19
|
def run(args, directory:, out:, kernel:, session:, discover:, generate:)
|
|
20
20
|
name = args.fetch(0) if args.one?
|
|
21
|
-
return kernel.abort(USAGE) if name.nil? || name.start_with?(
|
|
21
|
+
return kernel.abort(USAGE) if name.nil? || name.start_with?('-')
|
|
22
22
|
|
|
23
23
|
Skyward::ORM::CLI::BootFile.boot(directory, session, kernel, generate) do |database|
|
|
24
24
|
classes = discover.call
|
|
@@ -58,13 +58,13 @@ module Skyward::ORM::CLI::Scaffold
|
|
|
58
58
|
"skyward: #{name} is Skyward ORM's own table, so it needs no class."
|
|
59
59
|
elsif owner then "skyward: #{owner} already declares table :#{name}."
|
|
60
60
|
elsif !key.one?
|
|
61
|
-
"skyward: #{name} has #{key.empty? ?
|
|
62
|
-
|
|
63
|
-
|
|
61
|
+
"skyward: #{name} has #{key.empty? ? 'no primary key' : "a primary key on (#{key.join(', ')})"}, and a table " \
|
|
62
|
+
'class has a one-column primary key. Give the table one in a `skyward migration --empty` migration, then ' \
|
|
63
|
+
'scaffold it.'
|
|
64
64
|
elsif !unreadable.empty?
|
|
65
65
|
"skyward: a table class reads each column as a lowercase Ruby method a row doesn't already have, so it can't " \
|
|
66
|
-
"read #{unreadable.map { |column| "#{name}.#{column}" }.join(
|
|
67
|
-
|
|
66
|
+
"read #{unreadable.map { |column| "#{name}.#{column}" }.join(' or ')}. Rename each in a " \
|
|
67
|
+
'`skyward migration --empty` migration, then scaffold it.'
|
|
68
68
|
end
|
|
69
69
|
end
|
|
70
70
|
end
|