skyward-orm 0.3.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 +247 -60
- data/lib/skyward/orm/adapters/postgresql/connection.rb +64 -4
- data/lib/skyward/orm/adapters/postgresql.rb +189 -68
- data/lib/skyward/orm/adapters/sqlite/connection.rb +87 -0
- data/lib/skyward/orm/adapters/sqlite/constraints.rb +32 -14
- data/lib/skyward/orm/adapters/sqlite.rb +158 -45
- data/lib/skyward/orm/adapters/statement_cache.rb +51 -0
- data/lib/skyward/orm/adapters.rb +23 -7
- data/lib/skyward/orm/catalog/column.rb +2 -1
- data/lib/skyward/orm/cli/boot_file.rb +4 -4
- data/lib/skyward/orm/cli/migrate.rb +1 -1
- data/lib/skyward/orm/cli/migration.rb +8 -8
- data/lib/skyward/orm/cli/scaffold/source.rb +323 -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 +20 -8
- data/lib/skyward/orm/connection/instrumented.rb +71 -0
- data/lib/skyward/orm/connection/pool.rb +2 -2
- data/lib/skyward/orm/connection/session.rb +74 -5
- data/lib/skyward/orm/database/decoders.rb +41 -0
- data/lib/skyward/orm/{connection/database → database}/schema_check.rb +22 -10
- data/lib/skyward/orm/{connection/database.rb → database.rb} +70 -36
- 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 +20 -17
- data/lib/skyward/orm/migrations/diff.rb +156 -32
- data/lib/skyward/orm/migrations/directory.rb +5 -5
- data/lib/skyward/orm/migrations/rename_constraint.rb +8 -0
- data/lib/skyward/orm/migrations/runner.rb +21 -11
- data/lib/skyward/orm/migrations/script.rb +67 -27
- data/lib/skyward/orm/migrations/status.rb +38 -9
- data/lib/skyward/orm/migrations/summary.rb +23 -26
- data/lib/skyward/orm/migrations/versions.rb +3 -4
- data/lib/skyward/orm/migrations.rb +1 -1
- data/lib/skyward/orm/projection.rb +24 -11
- data/lib/skyward/orm/queries/clauses.rb +298 -124
- data/lib/skyward/orm/query.rb +2 -2
- data/lib/skyward/orm/record.rb +27 -9
- data/lib/skyward/orm/{placeholders.rb → statements/placeholders.rb} +4 -4
- data/lib/skyward/orm/statements.rb +178 -31
- data/lib/skyward/orm/tables/column.rb +2 -1
- data/lib/skyward/orm/tables/column_types.rb +63 -16
- data/lib/skyward/orm/tables/declarations.rb +43 -15
- data/lib/skyward/orm/{members.rb → tables/members.rb} +1 -1
- data/lib/skyward/orm/tables/rows.rb +28 -10
- 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 +124 -14
- 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 +52 -17
- data/sig/generated/skyward/orm/adapters/postgresql/connection.rbs +35 -3
- data/sig/generated/skyward/orm/adapters/postgresql.rbs +64 -14
- data/sig/generated/skyward/orm/adapters/sqlite/connection.rbs +55 -0
- data/sig/generated/skyward/orm/adapters/sqlite/constraints.rbs +6 -0
- data/sig/generated/skyward/orm/adapters/sqlite.rbs +72 -7
- data/sig/generated/skyward/orm/adapters/statement_cache.rbs +37 -0
- data/sig/generated/skyward/orm/adapters.rbs +6 -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 +158 -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 +47 -0
- data/sig/generated/skyward/orm/connection/session.rbs +40 -6
- data/sig/generated/skyward/orm/database/decoders.rbs +19 -0
- data/sig/generated/skyward/orm/{connection/database → database}/schema_check.rbs +11 -4
- data/sig/generated/skyward/orm/{connection/database.rbs → database.rbs} +40 -17
- 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 +129 -40
- data/sig/generated/skyward/orm/record.rbs +14 -7
- data/sig/generated/skyward/orm/{placeholders.rbs → statements/placeholders.rbs} +2 -2
- data/sig/generated/skyward/orm/statements.rbs +84 -8
- 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/rows.rbs +1 -1
- 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 +22 -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/decoders.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 +41 -8
- data/sig/manual/sqlite3.rbs +13 -2
- metadata +51 -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
|
@@ -4,22 +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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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'
|
|
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,
|
|
20
26
|
# as PostgreSQL keeps it. It's the format of SQLite's own CURRENT_TIMESTAMP,
|
|
21
27
|
# with the fraction added, so the two compare.
|
|
22
|
-
TIME_FORMAT =
|
|
28
|
+
TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%6N' #: String
|
|
23
29
|
|
|
24
30
|
# SQLite's extended result codes: SQLITE_CONSTRAINT_CHECK, _FOREIGNKEY,
|
|
25
31
|
# _NOTNULL, _PRIMARYKEY and _UNIQUE.
|
|
@@ -34,16 +40,40 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
34
40
|
# SQLite enforces a RESTRICT foreign key as a trigger would, so it reports
|
|
35
41
|
# SQLITE_CONSTRAINT_TRIGGER, and only the message tells it from a trigger's RAISE.
|
|
36
42
|
TRIGGER = 1811 #: Integer
|
|
37
|
-
RESTRICTED =
|
|
43
|
+
RESTRICTED = 'FOREIGN KEY constraint failed' #: String
|
|
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]
|
|
38
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
|
|
|
@@ -60,7 +90,10 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
60
90
|
LITERAL = /\A(?:'(?:[^']|'')*'|x'\h*'|[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:e[+-]?\d+)?|0x\h+|null|true|false)\z/i #: Regexp
|
|
61
91
|
|
|
62
92
|
# @rbs return: String
|
|
63
|
-
def driver =
|
|
93
|
+
def driver = 'sqlite3'
|
|
94
|
+
|
|
95
|
+
# @rbs return: Symbol
|
|
96
|
+
def adapter_name = :sqlite
|
|
64
97
|
|
|
65
98
|
# @rbs location: String
|
|
66
99
|
# @rbs return: Skyward::ORM::_Connection
|
|
@@ -70,9 +103,9 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
70
103
|
# busy_timeout= would hold the GVL while it waits, so no other thread or
|
|
71
104
|
# fiber could run, including the one holding the lock.
|
|
72
105
|
connection.busy_handler_timeout = BUSY_TIMEOUT_MS
|
|
73
|
-
connection.execute(
|
|
74
|
-
require_version(connection.execute(
|
|
75
|
-
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)
|
|
76
109
|
end
|
|
77
110
|
|
|
78
111
|
# Each connection to :memory: opens its own empty database, so there can be
|
|
@@ -80,7 +113,7 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
80
113
|
# @rbs location: String
|
|
81
114
|
# @rbs size: Integer
|
|
82
115
|
# @rbs return: Integer
|
|
83
|
-
def pool_size(location, size) = location ==
|
|
116
|
+
def pool_size(location, size) = location == ':memory:' ? 1 : size
|
|
84
117
|
|
|
85
118
|
# @rbs version: String
|
|
86
119
|
# @rbs return: void
|
|
@@ -93,15 +126,21 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
93
126
|
end
|
|
94
127
|
|
|
95
128
|
# @rbs (Integer _position) -> String
|
|
96
|
-
def placeholder(_position) =
|
|
129
|
+
def placeholder(_position) = '?'
|
|
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
|
|
97
136
|
|
|
98
137
|
# SQLite takes an OFFSET only after a LIMIT, and a negative one is no limit.
|
|
99
138
|
# @rbs return: String
|
|
100
|
-
def unlimited =
|
|
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,53 @@ 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
|
|
221
|
+
# @rbs value: String
|
|
222
|
+
# @rbs return: String
|
|
223
|
+
def decoding(column, value)
|
|
224
|
+
element = column.element
|
|
225
|
+
return scalar(column.type, value) unless element
|
|
226
|
+
|
|
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"
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
144
233
|
# @rbs value: untyped
|
|
145
234
|
# @rbs return: untyped
|
|
146
|
-
def decode(
|
|
147
|
-
case type
|
|
148
|
-
when :bool then value != 0
|
|
149
|
-
when :date then Date.iso8601(value)
|
|
150
|
-
when :time then Time.new(value, in: "UTC")
|
|
151
|
-
when :enum then value.to_sym
|
|
152
|
-
# Rows are values, so even what the driver returns as it is comes back frozen.
|
|
153
|
-
else value.freeze
|
|
154
|
-
end
|
|
155
|
-
end
|
|
235
|
+
def decode(column, value) = Skyward::ORM::Adapters.decode(self, column, value)
|
|
156
236
|
|
|
157
237
|
# @rbs (Skyward::ORM::_Connection connection) -> Array[Skyward::ORM::Catalog::Table]
|
|
158
238
|
def tables(connection)
|
|
159
239
|
index_rows = connection.execute(INDEXES).group_by(&:first)
|
|
160
240
|
connection.execute(COLUMNS).group_by(&:first).map do |name, rows|
|
|
241
|
+
sql = rows.fetch(0).fetch(1)
|
|
242
|
+
generated = Constraints.generated(sql)
|
|
161
243
|
columns = rows.map do |_, _, column, type, not_null, primary_key, default|
|
|
162
244
|
Skyward::ORM::Catalog::Column.new(
|
|
163
245
|
name: column.to_sym,
|
|
@@ -165,14 +247,15 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
165
247
|
# A primary key column can't hold NULL in practice, though SQLite only
|
|
166
248
|
# enforces it for an INTEGER PRIMARY KEY or a NOT NULL column.
|
|
167
249
|
nullable: not_null.zero? && primary_key.zero?,
|
|
168
|
-
default: default(default)
|
|
250
|
+
default: default(default),
|
|
251
|
+
generated: generated[column.to_sym]
|
|
169
252
|
)
|
|
170
253
|
end
|
|
171
254
|
Skyward::ORM::Catalog::Table.new(
|
|
172
255
|
name: name.to_sym,
|
|
173
256
|
columns:,
|
|
174
257
|
indexes: indexes(index_rows.fetch(name, [])),
|
|
175
|
-
constraints: Constraints.read(
|
|
258
|
+
constraints: Constraints.read(sql, columns.map(&:name))
|
|
176
259
|
)
|
|
177
260
|
end
|
|
178
261
|
end
|
|
@@ -183,17 +266,17 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
183
266
|
# IMMEDIATE takes the write lock as the transaction begins rather than at its
|
|
184
267
|
# first write, so a second migrator waits there, and then sees what the first ran.
|
|
185
268
|
# @rbs return: String
|
|
186
|
-
def begin_migration =
|
|
269
|
+
def begin_migration = 'PRAGMA foreign_keys = OFF; BEGIN IMMEDIATE'
|
|
187
270
|
|
|
188
271
|
# @rbs (Skyward::ORM::_Connection connection) -> Array[String]
|
|
189
272
|
def foreign_key_violations(connection)
|
|
190
|
-
connection.execute(
|
|
273
|
+
connection.execute('PRAGMA foreign_key_check').map do |table, rowid, parent, key|
|
|
191
274
|
"#{table} row #{rowid} refers to a missing #{parent} row (foreign key #{key})"
|
|
192
275
|
end
|
|
193
276
|
end
|
|
194
277
|
|
|
195
278
|
# @rbs return: String
|
|
196
|
-
def end_migration =
|
|
279
|
+
def end_migration = 'PRAGMA foreign_keys = ON'
|
|
197
280
|
|
|
198
281
|
# The code says which kind of constraint failed. The message names the
|
|
199
282
|
# columns of a unique or not-null one, and the name of a check, and the rest
|
|
@@ -222,13 +305,43 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
222
305
|
|
|
223
306
|
private
|
|
224
307
|
|
|
308
|
+
# @rbs type: Skyward::ORM::Tables::column_type
|
|
309
|
+
# @rbs value: String
|
|
310
|
+
# @rbs return: String
|
|
311
|
+
def scalar(type, value)
|
|
312
|
+
case type
|
|
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)"
|
|
319
|
+
# Rows are values, so even what the driver returns as it is comes back frozen.
|
|
320
|
+
else "#{value}.freeze"
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# An Array[Float]'s JSON may hold an Integer, as an Array[Float] takes one.
|
|
325
|
+
# @rbs type: Skyward::ORM::Tables::column_type
|
|
326
|
+
# @rbs value: String
|
|
327
|
+
# @rbs return: String
|
|
328
|
+
def item(type, value)
|
|
329
|
+
case type
|
|
330
|
+
when :bool then value
|
|
331
|
+
when :float then "Float(#{value})"
|
|
332
|
+
# Only a String names a Symbol, and JSON may hold any value.
|
|
333
|
+
when :enum then "::String.try_convert(#{value})&.to_sym"
|
|
334
|
+
else scalar(type, value)
|
|
335
|
+
end
|
|
336
|
+
end
|
|
337
|
+
|
|
225
338
|
# @rbs sql_type: String
|
|
226
339
|
# @rbs return: Symbol
|
|
227
340
|
def affinity(sql_type)
|
|
228
341
|
declared = sql_type.upcase
|
|
229
|
-
if declared.include?(
|
|
342
|
+
if declared.include?('INT') then :integer
|
|
230
343
|
elsif declared.match?(/CHAR|CLOB|TEXT/) then :text
|
|
231
|
-
elsif declared.empty? || declared.include?(
|
|
344
|
+
elsif declared.empty? || declared.include?('BLOB') then :blob
|
|
232
345
|
elsif declared.match?(/REAL|FLOA|DOUB/) then :real
|
|
233
346
|
else :numeric
|
|
234
347
|
end
|
|
@@ -307,11 +420,11 @@ module Skyward::ORM::Adapters::SQLite
|
|
|
307
420
|
# @rbs message: String
|
|
308
421
|
# @rbs return: [Symbol, Array[Symbol]]
|
|
309
422
|
def failed_columns(message)
|
|
310
|
-
names = detail(message).split(
|
|
423
|
+
names = detail(message).split(', ').map { |name| name.split('.', 2) }
|
|
311
424
|
[names.fetch(0).fetch(0).to_sym, names.map { |name| name.fetch(-1).to_sym }]
|
|
312
425
|
end
|
|
313
426
|
|
|
314
427
|
# @rbs message: String
|
|
315
428
|
# @rbs return: String
|
|
316
|
-
def detail(message) = message.split(
|
|
429
|
+
def detail(message) = message.split(': ', 2).last.to_s
|
|
317
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,17 +4,23 @@ 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
|
+
# 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
|
-
scheme, location = url.split(
|
|
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.
|
|
@@ -27,12 +33,22 @@ module Skyward::ORM::Adapters
|
|
|
27
33
|
[adapter, location]
|
|
28
34
|
end
|
|
29
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
|
+
|
|
30
42
|
private
|
|
31
43
|
|
|
32
44
|
# The URL can hold a password, so only its scheme is shown.
|
|
33
45
|
# @rbs (String? scheme, Hash[String, Skyward::ORM::_Adapter] schemes) -> String
|
|
34
46
|
def unsupported(scheme, schemes)
|
|
35
|
-
url = scheme ? "a #{scheme}:// URL" :
|
|
36
|
-
|
|
47
|
+
url = scheme ? "a #{scheme}:// URL" : 'a URL with no scheme'
|
|
48
|
+
suggestion = Skyward::Core::Diagnostics::Suggestion.closest(scheme, schemes.keys) if scheme
|
|
49
|
+
hint = " Did you mean #{suggestion}://?" if suggestion
|
|
50
|
+
"Skyward ORM can't connect to #{url}. It connects to #{schemes.keys.map do |key|
|
|
51
|
+
"#{key}://"
|
|
52
|
+
end.join(', ')} URLs.#{hint}"
|
|
37
53
|
end
|
|
38
54
|
end
|
|
@@ -3,17 +3,17 @@
|
|
|
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
|
|
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)
|
|
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
|
|
@@ -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
|
|
@@ -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?)
|
|
@@ -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
|
|
@@ -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
|