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,54 +4,54 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
4
4
|
extend self
|
|
5
5
|
|
|
6
6
|
SQL_TYPES = {
|
|
7
|
-
integer:
|
|
8
|
-
float:
|
|
9
|
-
decimal:
|
|
10
|
-
string:
|
|
11
|
-
bool:
|
|
12
|
-
date:
|
|
13
|
-
time:
|
|
14
|
-
enum:
|
|
15
|
-
uuid:
|
|
16
|
-
int32:
|
|
17
|
-
int16:
|
|
18
|
-
json:
|
|
7
|
+
integer: 'bigint',
|
|
8
|
+
float: 'double precision',
|
|
9
|
+
decimal: 'numeric',
|
|
10
|
+
string: 'text',
|
|
11
|
+
bool: 'boolean',
|
|
12
|
+
date: 'date',
|
|
13
|
+
time: 'timestamptz',
|
|
14
|
+
enum: 'text',
|
|
15
|
+
uuid: 'uuid',
|
|
16
|
+
int32: 'integer',
|
|
17
|
+
int16: 'smallint',
|
|
18
|
+
json: 'jsonb'
|
|
19
19
|
}.freeze #: Hash[Skyward::ORM::Tables::column_type, String]
|
|
20
20
|
|
|
21
21
|
# As the catalog spells each type, which compatible? is given.
|
|
22
22
|
COMPATIBLE = {
|
|
23
23
|
integer: %w[bigint integer smallint],
|
|
24
|
-
float: [
|
|
25
|
-
decimal: [
|
|
26
|
-
string: [
|
|
27
|
-
bool: [
|
|
28
|
-
date: [
|
|
29
|
-
time: [
|
|
30
|
-
enum: [
|
|
31
|
-
uuid: [
|
|
32
|
-
int32: [
|
|
33
|
-
int16: [
|
|
24
|
+
float: ['double precision', 'real'],
|
|
25
|
+
decimal: ['numeric'],
|
|
26
|
+
string: ['text', 'character varying', 'uuid', 'citext'],
|
|
27
|
+
bool: ['boolean'],
|
|
28
|
+
date: ['date'],
|
|
29
|
+
time: ['timestamp with time zone', 'timestamp without time zone'],
|
|
30
|
+
enum: ['text', 'character varying'],
|
|
31
|
+
uuid: ['uuid'],
|
|
32
|
+
int32: ['integer'],
|
|
33
|
+
int16: ['smallint'],
|
|
34
34
|
json: %w[jsonb json]
|
|
35
35
|
}.freeze #: Hash[Skyward::ORM::Tables::column_type, Array[String]]
|
|
36
36
|
|
|
37
37
|
# COMPATIBLE read backwards, taking the narrowest type for each.
|
|
38
38
|
COLUMN_TYPES = {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
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
55
|
}.freeze #: Hash[String, Skyward::ORM::Tables::column_type]
|
|
56
56
|
|
|
57
57
|
# The catalog puts a length or precision in parentheses.
|
|
@@ -60,7 +60,7 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
60
60
|
# UTC, so a timestamp column, which has no zone, holds the same instant as a
|
|
61
61
|
# timestamptz, and to the microsecond, as PostgreSQL keeps it. Its trailing
|
|
62
62
|
# zeros go, as PostgreSQL writes a time back, so a default compares as text.
|
|
63
|
-
TIME_FORMAT =
|
|
63
|
+
TIME_FORMAT = '%Y-%m-%d %H:%M:%S.%6N' #: String
|
|
64
64
|
TRAILING_ZEROS = /\.?0+\z/ #: Regexp
|
|
65
65
|
|
|
66
66
|
# PostgreSQL reads a literal default back cast to its type, as 'normal'::text
|
|
@@ -71,34 +71,34 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
71
71
|
|
|
72
72
|
# Float() refuses the special values PostgreSQL writes out.
|
|
73
73
|
SPECIAL_FLOATS = {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
'Infinity' => Float::INFINITY,
|
|
75
|
+
'-Infinity' => -Float::INFINITY,
|
|
76
|
+
'NaN' => Float::NAN
|
|
77
77
|
}.freeze #: Hash[String, Float]
|
|
78
78
|
|
|
79
79
|
MIGRATION_LOCK = 7_310_420_115 #: Integer
|
|
80
80
|
|
|
81
81
|
# Each value comes back as text, and a boolean as t or f.
|
|
82
|
-
TRUE_TEXT =
|
|
83
|
-
FALSE_TEXT =
|
|
82
|
+
TRUE_TEXT = 't' #: String
|
|
83
|
+
FALSE_TEXT = 'f' #: String
|
|
84
84
|
|
|
85
85
|
VIOLATIONS = {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
86
|
+
'23505' => :unique,
|
|
87
|
+
'23503' => :foreign_key,
|
|
88
|
+
'23502' => :not_null,
|
|
89
|
+
'23514' => :check
|
|
90
90
|
}.freeze #: Hash[String, Skyward::ORM::violation_kind]
|
|
91
91
|
|
|
92
92
|
CONSTRAINT_KINDS = {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
93
|
+
'p' => :primary_key,
|
|
94
|
+
'u' => :unique,
|
|
95
|
+
'f' => :foreign_key,
|
|
96
|
+
'c' => :check
|
|
97
97
|
}.freeze #: Hash[String, Skyward::ORM::Catalog::constraint_kind]
|
|
98
98
|
|
|
99
99
|
# The tables an unqualified name finds, as every query names them.
|
|
100
100
|
TABLES = "t.relkind IN ('r', 'p') AND t.relnamespace <> 'pg_catalog'::regnamespace " \
|
|
101
|
-
|
|
101
|
+
'AND pg_table_is_visible(t.oid)' #: String
|
|
102
102
|
|
|
103
103
|
# pg_attrdef holds a generated column's expression as if it were its default.
|
|
104
104
|
COLUMNS = <<~SQL.freeze #: String
|
|
@@ -139,7 +139,7 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
139
139
|
SQL
|
|
140
140
|
|
|
141
141
|
# @rbs return: String
|
|
142
|
-
def driver =
|
|
142
|
+
def driver = 'pg'
|
|
143
143
|
|
|
144
144
|
# @rbs return: Symbol
|
|
145
145
|
def adapter_name = :postgresql
|
|
@@ -151,9 +151,22 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
151
151
|
# @rbs location: String
|
|
152
152
|
# @rbs return: Skyward::ORM::_Connection
|
|
153
153
|
def connect(location)
|
|
154
|
+
require_close_prepared(PG::Connection)
|
|
154
155
|
connection = PG.connect("postgresql://#{location}")
|
|
155
156
|
connection.exec("SET TimeZone = 'UTC'; SET DateStyle = 'ISO'; SET client_min_messages = warning")
|
|
156
|
-
Connection.new(connection)
|
|
157
|
+
Connection.new(connection, Skyward::ORM::Adapters::StatementCache.new)
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# A connection closes each statement it evicts with close_prepared, which pg
|
|
161
|
+
# compiles only against libpq 17 and later.
|
|
162
|
+
# @rbs connection: Module
|
|
163
|
+
# @rbs return: void
|
|
164
|
+
def require_close_prepared(connection)
|
|
165
|
+
return if connection.method_defined?(:close_prepared)
|
|
166
|
+
|
|
167
|
+
raise Skyward::Core::BootError,
|
|
168
|
+
'Skyward ORM needs the pg gem 1.6 or later, built against libpq 17 or later, for ' \
|
|
169
|
+
"PG::Connection#close_prepared. Update the pg gem, or the libpq it's built against."
|
|
157
170
|
end
|
|
158
171
|
|
|
159
172
|
# @rbs (String _location, Integer size) -> Integer
|
|
@@ -179,7 +192,7 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
179
192
|
end
|
|
180
193
|
|
|
181
194
|
# @rbs return: String?
|
|
182
|
-
def identity =
|
|
195
|
+
def identity = 'GENERATED ALWAYS AS IDENTITY'
|
|
183
196
|
|
|
184
197
|
# @rbs return: bool
|
|
185
198
|
def alters_in_place? = true
|
|
@@ -218,7 +231,7 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
218
231
|
element = column.element
|
|
219
232
|
return accepts?(column.type, sql_type) unless element
|
|
220
233
|
|
|
221
|
-
sql_type.end_with?(
|
|
234
|
+
sql_type.end_with?('[]') && accepts?(element, sql_type.delete_suffix('[]'))
|
|
222
235
|
end
|
|
223
236
|
|
|
224
237
|
# PostgreSQL compares numeric as it is.
|
|
@@ -227,7 +240,7 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
227
240
|
|
|
228
241
|
# @rbs sql_type: String
|
|
229
242
|
# @rbs return: Skyward::ORM::Tables::column_type?
|
|
230
|
-
def column_type(sql_type) = COLUMN_TYPES[sql_type.sub(MODIFIER,
|
|
243
|
+
def column_type(sql_type) = COLUMN_TYPES[sql_type.sub(MODIFIER, '')]
|
|
231
244
|
|
|
232
245
|
# The driver sends every other value as its to_s, which PostgreSQL reads.
|
|
233
246
|
# @rbs value: untyped
|
|
@@ -238,8 +251,8 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
238
251
|
# Without the exponent BigDecimal#to_s writes, as 0.15e1, so both adapters
|
|
239
252
|
# bind the same text.
|
|
240
253
|
# -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(
|
|
242
|
-
when Time then "#{value.getutc.strftime(TIME_FORMAT).sub(TRAILING_ZEROS,
|
|
254
|
+
when BigDecimal then (value.zero? ? BigDecimal(0) : value).to_s('F')
|
|
255
|
+
when Time then "#{value.getutc.strftime(TIME_FORMAT).sub(TRAILING_ZEROS, '')}+00"
|
|
243
256
|
else value
|
|
244
257
|
end
|
|
245
258
|
end
|
|
@@ -262,16 +275,22 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
262
275
|
end
|
|
263
276
|
|
|
264
277
|
# @rbs column: Skyward::ORM::Tables::Column
|
|
265
|
-
# @rbs value:
|
|
266
|
-
# @rbs return:
|
|
267
|
-
def
|
|
278
|
+
# @rbs value: String
|
|
279
|
+
# @rbs return: String
|
|
280
|
+
def decoding(column, value)
|
|
268
281
|
element = column.element
|
|
269
282
|
return scalar(column.type, value) unless element
|
|
270
283
|
|
|
284
|
+
decoded = scalar(element, 'item')
|
|
271
285
|
# 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 :
|
|
286
|
+
"::PG::TextDecoder::Array.new.decode(#{value}).map { |item| item.nil? ? nil : (#{decoded}).freeze }.freeze"
|
|
273
287
|
end
|
|
274
288
|
|
|
289
|
+
# @rbs column: Skyward::ORM::Tables::Column
|
|
290
|
+
# @rbs value: untyped
|
|
291
|
+
# @rbs return: untyped
|
|
292
|
+
def decode(column, value) = Skyward::ORM::Adapters.decode(self, column, value)
|
|
293
|
+
|
|
275
294
|
# @rbs (Skyward::ORM::_Connection connection) -> Array[Skyward::ORM::Catalog::Table]
|
|
276
295
|
def tables(connection)
|
|
277
296
|
index_rows = connection.execute(INDEXES).group_by(&:first)
|
|
@@ -321,25 +340,25 @@ module Skyward::ORM::Adapters::PostgreSQL
|
|
|
321
340
|
# @rbs return: bool
|
|
322
341
|
def accepts?(type, sql_type)
|
|
323
342
|
accepted = COMPATIBLE[type]
|
|
324
|
-
accepted.nil? || accepted.include?(sql_type.sub(MODIFIER,
|
|
343
|
+
accepted.nil? || accepted.include?(sql_type.sub(MODIFIER, ''))
|
|
325
344
|
end
|
|
326
345
|
|
|
327
346
|
# @rbs type: Skyward::ORM::Tables::column_type
|
|
328
|
-
# @rbs value:
|
|
329
|
-
# @rbs return:
|
|
347
|
+
# @rbs value: String
|
|
348
|
+
# @rbs return: String
|
|
330
349
|
def scalar(type, value)
|
|
331
350
|
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)
|
|
351
|
+
when :integer, :int32, :int16 then "Integer(#{value}, 10)"
|
|
352
|
+
when :float then "::Skyward::ORM::Adapters::PostgreSQL::SPECIAL_FLOATS.fetch(#{value}) { Float(#{value}) }"
|
|
353
|
+
when :decimal then "BigDecimal(#{value})"
|
|
354
|
+
when :bool then "#{value} == ::Skyward::ORM::Adapters::PostgreSQL::TRUE_TEXT"
|
|
355
|
+
when :date then "::Date.iso8601(#{value})"
|
|
337
356
|
# 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
|
|
357
|
+
when :time then "::Time.new(#{value}, in: \"UTC\").getutc"
|
|
339
358
|
# 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
|
|
359
|
+
when :enum then "::String.try_convert(#{value})&.to_sym"
|
|
360
|
+
when :json then "::JSON.parse(#{value}, freeze: true)"
|
|
361
|
+
else "#{value}.freeze"
|
|
343
362
|
end
|
|
344
363
|
end
|
|
345
364
|
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# SQLite3::Database#execute prepares, runs and closes a new statement on every
|
|
4
|
+
# call, so this keeps the statements it's asked to prepare, to run again.
|
|
5
|
+
class Skyward::ORM::Adapters::SQLite::Connection
|
|
6
|
+
# @rbs @database: SQLite3::Database[SQLite3::result_as_array]
|
|
7
|
+
# @rbs @statements: Skyward::ORM::Adapters::StatementCache[SQLite3::Statement]
|
|
8
|
+
|
|
9
|
+
# @rbs database: SQLite3::Database[SQLite3::result_as_array]
|
|
10
|
+
# @rbs statements: Skyward::ORM::Adapters::StatementCache[SQLite3::Statement]
|
|
11
|
+
# @rbs return: void
|
|
12
|
+
def initialize(database, statements)
|
|
13
|
+
@database = database
|
|
14
|
+
@statements = statements
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# @rbs sql: String
|
|
18
|
+
# @rbs binds: Array[untyped]
|
|
19
|
+
# @rbs prepare: bool
|
|
20
|
+
# @rbs return: Array[Array[untyped]]
|
|
21
|
+
def execute(sql, binds = [], prepare: false)
|
|
22
|
+
statement = kept(sql, binds, prepare)
|
|
23
|
+
return @database.execute(sql, binds) unless statement
|
|
24
|
+
|
|
25
|
+
run(statement, binds)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @rbs sql: String
|
|
29
|
+
# @rbs *binds: untyped
|
|
30
|
+
# @rbs prepare: bool
|
|
31
|
+
# @rbs return: Array[Array[untyped]]
|
|
32
|
+
def execute2(sql, *binds, prepare: false)
|
|
33
|
+
statement = kept(sql, binds, prepare)
|
|
34
|
+
return @database.execute2(sql, *binds) unless statement
|
|
35
|
+
|
|
36
|
+
rows = run(statement, binds)
|
|
37
|
+
# Read after the run, rather than through Statement#columns, which caches
|
|
38
|
+
# them: SQLite prepares a statement again once the schema changes, so a
|
|
39
|
+
# SELECT * returns a column a migration added.
|
|
40
|
+
[Array.new(statement.column_count) { |index| statement.column_name(index) }, *rows]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# @rbs sql: String
|
|
44
|
+
# @rbs return: void
|
|
45
|
+
def execute_batch(sql)
|
|
46
|
+
@database.execute_batch(sql)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @rbs return: Integer
|
|
50
|
+
def changes = @database.changes
|
|
51
|
+
|
|
52
|
+
# @rbs return: bool
|
|
53
|
+
def transaction_active? = @database.transaction_active?
|
|
54
|
+
|
|
55
|
+
# SQLite holds a closed database open until each of its statements is closed too.
|
|
56
|
+
# @rbs return: void
|
|
57
|
+
def close
|
|
58
|
+
@statements.clear.each(&:close)
|
|
59
|
+
@database.close
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
# @rbs sql: String
|
|
65
|
+
# @rbs binds: Array[untyped]
|
|
66
|
+
# @rbs prepare: bool
|
|
67
|
+
# @rbs return: SQLite3::Statement?
|
|
68
|
+
def kept(sql, binds, prepare)
|
|
69
|
+
return unless prepare && @statements.keeps?(binds)
|
|
70
|
+
|
|
71
|
+
@statements.fetch(sql) || @database.prepare(sql).tap do |statement|
|
|
72
|
+
@statements.store(sql, statement)&.close
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Reset however it ends, so no kept statement is left running, where it would
|
|
77
|
+
# stop a migration's DROP TABLE with "database table is locked".
|
|
78
|
+
# @rbs statement: SQLite3::Statement
|
|
79
|
+
# @rbs binds: Array[untyped]
|
|
80
|
+
# @rbs return: Array[Array[untyped]]
|
|
81
|
+
def run(statement, binds)
|
|
82
|
+
statement.bind_params(binds)
|
|
83
|
+
statement.to_a
|
|
84
|
+
ensure
|
|
85
|
+
statement.reset!
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -25,11 +25,11 @@ module Skyward::ORM::Adapters::SQLite::Constraints
|
|
|
25
25
|
TABLE_CONSTRAINTS = %w[CONSTRAINT PRIMARY UNIQUE CHECK FOREIGN].freeze #: Array[String]
|
|
26
26
|
|
|
27
27
|
KINDS = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
'PRIMARY' => :primary_key,
|
|
29
|
+
'UNIQUE' => :unique,
|
|
30
|
+
'CHECK' => :check,
|
|
31
|
+
'FOREIGN' => :foreign_key,
|
|
32
|
+
'REFERENCES' => :foreign_key
|
|
33
33
|
}.freeze #: Hash[String, Skyward::ORM::Catalog::constraint_kind]
|
|
34
34
|
|
|
35
35
|
# The words that start a column's next constraint, or its default or collation.
|
|
@@ -53,7 +53,7 @@ module Skyward::ORM::Adapters::SQLite::Constraints
|
|
|
53
53
|
definitions(sql).filter_map do |definition|
|
|
54
54
|
tokens = definition.map(&:first)
|
|
55
55
|
as = definition.each_index.find do |index|
|
|
56
|
-
index.positive? && definition.fetch(index).last.zero? && word(tokens.fetch(index)) ==
|
|
56
|
+
index.positive? && definition.fetch(index).last.zero? && word(tokens.fetch(index)) == 'AS'
|
|
57
57
|
end
|
|
58
58
|
next unless as
|
|
59
59
|
|
|
@@ -89,12 +89,12 @@ module Skyward::ORM::Adapters::SQLite::Constraints
|
|
|
89
89
|
case text
|
|
90
90
|
when %r{\A(?:\s|--|/\*)} then nil
|
|
91
91
|
when /\A"/ then [:name, text[1...-1].to_s.gsub('""', '"'), start, finish]
|
|
92
|
-
when /\A`/ then [:name, text[1...-1].to_s.gsub(
|
|
92
|
+
when /\A`/ then [:name, text[1...-1].to_s.gsub('``', '`'), start, finish]
|
|
93
93
|
when /\A\[/ then [:name, text[1...-1].to_s, start, finish]
|
|
94
94
|
when /\A'/ then [:string, text, start, finish]
|
|
95
|
-
when
|
|
96
|
-
when
|
|
97
|
-
when
|
|
95
|
+
when '(' then [:open, text, start, finish]
|
|
96
|
+
when ')' then [:close, text, start, finish]
|
|
97
|
+
when ',' then [:comma, text, start, finish]
|
|
98
98
|
when /\A[\p{L}_]/ then [:word, text, start, finish]
|
|
99
99
|
else [:other, text, start, finish]
|
|
100
100
|
end
|
|
@@ -114,10 +114,10 @@ module Skyward::ORM::Adapters::SQLite::Constraints
|
|
|
114
114
|
definition.each_with_index.filter_map do |(token, depth), index|
|
|
115
115
|
kind = KINDS[word(token).to_s] if depth.zero?
|
|
116
116
|
# A table's FOREIGN KEY is followed by its REFERENCES.
|
|
117
|
-
next if kind.nil? || (column.nil? && word(token) ==
|
|
117
|
+
next if kind.nil? || (column.nil? && word(token) == 'REFERENCES')
|
|
118
118
|
|
|
119
119
|
Skyward::ORM::Catalog::Constraint.new(
|
|
120
|
-
name: (tokens.fetch(index - 1)[1] if index > 1 && word(tokens.fetch(index - 2)) ==
|
|
120
|
+
name: (tokens.fetch(index - 1)[1] if index > 1 && word(tokens.fetch(index - 2)) == 'CONSTRAINT'),
|
|
121
121
|
kind:,
|
|
122
122
|
columns: column ? [column] : mentioned(first_parenthesized(definition.drop(index + 1)), columns),
|
|
123
123
|
references: (referenced(tokens.drop(index)) if kind == :foreign_key),
|
|
@@ -154,8 +154,8 @@ module Skyward::ORM::Adapters::SQLite::Constraints
|
|
|
154
154
|
clause = word(tokens.fetch(index))
|
|
155
155
|
following = tokens[index + 1]
|
|
156
156
|
COLUMN_CLAUSES.include?(clause.to_s) &&
|
|
157
|
-
!(SET_TO.include?(clause) && word(tokens.fetch(index - 1)) ==
|
|
158
|
-
!(clause ==
|
|
157
|
+
!(SET_TO.include?(clause) && word(tokens.fetch(index - 1)) == 'SET') &&
|
|
158
|
+
!(clause == 'NOT' && following && word(following) == 'DEFERRABLE')
|
|
159
159
|
end
|
|
160
160
|
tokens.take(ending || tokens.size)
|
|
161
161
|
end
|
|
@@ -186,5 +186,5 @@ module Skyward::ORM::Adapters::SQLite::Constraints
|
|
|
186
186
|
|
|
187
187
|
# @rbs tokens: Array[token]
|
|
188
188
|
# @rbs return: Symbol
|
|
189
|
-
def referenced(tokens) = tokens.drop_while { |token| word(token) !=
|
|
189
|
+
def referenced(tokens) = tokens.drop_while { |token| word(token) != 'REFERENCES' }.fetch(1)[1].to_sym
|
|
190
190
|
end
|