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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +66 -48
  4. data/lib/skyward/orm/adapters/postgresql/connection.rb +64 -4
  5. data/lib/skyward/orm/adapters/postgresql.rb +95 -76
  6. data/lib/skyward/orm/adapters/sqlite/connection.rb +87 -0
  7. data/lib/skyward/orm/adapters/sqlite/constraints.rb +15 -15
  8. data/lib/skyward/orm/adapters/sqlite.rb +59 -55
  9. data/lib/skyward/orm/adapters/statement_cache.rb +51 -0
  10. data/lib/skyward/orm/adapters.rb +12 -6
  11. data/lib/skyward/orm/cli/boot_file.rb +2 -2
  12. data/lib/skyward/orm/cli/migration.rb +7 -7
  13. data/lib/skyward/orm/cli/scaffold/source.rb +49 -30
  14. data/lib/skyward/orm/cli/scaffold.rb +6 -6
  15. data/lib/skyward/orm/commands.rb +9 -9
  16. data/lib/skyward/orm/connection/instrumented.rb +4 -2
  17. data/lib/skyward/orm/connection/pool.rb +2 -2
  18. data/lib/skyward/orm/database/decoders.rb +41 -0
  19. data/lib/skyward/orm/database/schema_check.rb +3 -3
  20. data/lib/skyward/orm/database.rb +65 -31
  21. data/lib/skyward/orm/migrations/definitions.rb +13 -13
  22. data/lib/skyward/orm/migrations/diff.rb +4 -4
  23. data/lib/skyward/orm/migrations/directory.rb +5 -5
  24. data/lib/skyward/orm/migrations/runner.rb +8 -8
  25. data/lib/skyward/orm/migrations/script.rb +9 -9
  26. data/lib/skyward/orm/migrations/status.rb +7 -7
  27. data/lib/skyward/orm/migrations/summary.rb +22 -22
  28. data/lib/skyward/orm/migrations/versions.rb +2 -3
  29. data/lib/skyward/orm/projection.rb +6 -12
  30. data/lib/skyward/orm/queries/clauses.rb +201 -112
  31. data/lib/skyward/orm/query.rb +2 -2
  32. data/lib/skyward/orm/record.rb +5 -9
  33. data/lib/skyward/orm/statements/placeholders.rb +3 -3
  34. data/lib/skyward/orm/statements.rb +34 -28
  35. data/lib/skyward/orm/tables/column_types.rb +7 -7
  36. data/lib/skyward/orm/tables/declarations.rb +5 -5
  37. data/lib/skyward/orm/tables/rows.rb +28 -10
  38. data/lib/skyward/orm/tables/sources.rb +3 -3
  39. data/lib/skyward/orm/tables/values.rb +18 -18
  40. data/lib/skyward/orm/version.rb +1 -1
  41. data/lib/skyward/orm.rb +15 -14
  42. data/sig/generated/skyward/orm/adapters/postgresql/connection.rbs +35 -3
  43. data/sig/generated/skyward/orm/adapters/postgresql.rbs +14 -3
  44. data/sig/generated/skyward/orm/adapters/sqlite/connection.rbs +55 -0
  45. data/sig/generated/skyward/orm/adapters/sqlite.rbs +11 -6
  46. data/sig/generated/skyward/orm/adapters/statement_cache.rbs +37 -0
  47. data/sig/generated/skyward/orm/adapters.rbs +3 -0
  48. data/sig/generated/skyward/orm/cli/scaffold/source.rbs +11 -0
  49. data/sig/generated/skyward/orm/connection/instrumented.rbs +4 -2
  50. data/sig/generated/skyward/orm/database/decoders.rbs +19 -0
  51. data/sig/generated/skyward/orm/database.rbs +38 -14
  52. data/sig/generated/skyward/orm/queries/clauses.rbs +81 -28
  53. data/sig/generated/skyward/orm/record.rbs +6 -7
  54. data/sig/generated/skyward/orm/statements.rbs +8 -10
  55. data/sig/generated/skyward/orm/tables/rows.rbs +1 -1
  56. data/sig/generated/skyward/orm.rbs +2 -2
  57. data/sig/manual/pg.rbs +10 -0
  58. data/sig/manual/skyward/orm/database/decoders.rbs +3 -0
  59. data/sig/manual/skyward/orm.rbs +9 -3
  60. data/sig/manual/sqlite3.rbs +13 -2
  61. metadata +11 -3
@@ -1,19 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "skyward/orm"
3
+ require 'skyward/orm'
4
4
 
5
5
  discover = -> { Skyward::ORM::Migrations::Discovery.table_classes(Zeitwerk::Loader) }
6
6
  generate = lambda do
7
7
  Skyward::Core::Signatures::Inline.generate(
8
8
  Dir.pwd,
9
- rbs_inline: Skyward::Core::CLI::Executable.find("rbs-inline", feature: "rbs/inline"),
9
+ rbs_inline: Skyward::Core::CLI::Executable.find('rbs-inline', feature: 'rbs/inline'),
10
10
  capture: Skyward::Core::CLI::Executable::CAPTURE
11
11
  )
12
12
  end
13
13
 
14
14
  Skyward::Core::CLI.register(
15
- "migration",
16
- "Write the migration that brings the database in line with your table classes"
15
+ 'migration',
16
+ 'Write the migration that brings the database in line with your table classes'
17
17
  ) do |args|
18
18
  Skyward::ORM::CLI::Migration.run(
19
19
  args,
@@ -28,7 +28,7 @@ Skyward::Core::CLI.register(
28
28
  )
29
29
  end
30
30
 
31
- Skyward::Core::CLI.register("migrate", "Run the pending migrations in db/migrate") do |_args|
31
+ Skyward::Core::CLI.register('migrate', 'Run the pending migrations in db/migrate') do |_args|
32
32
  Skyward::ORM::CLI::Migrate.run(
33
33
  directory: Dir.pwd,
34
34
  out: $stdout,
@@ -39,7 +39,7 @@ Skyward::Core::CLI.register("migrate", "Run the pending migrations in db/migrate
39
39
  )
40
40
  end
41
41
 
42
- Skyward::Core::CLI.register("rollback", "Undo the latest migration by running its -- down section") do |_args|
42
+ Skyward::Core::CLI.register('rollback', 'Undo the latest migration by running its -- down section') do |_args|
43
43
  Skyward::ORM::CLI::Rollback.run(
44
44
  directory: Dir.pwd,
45
45
  out: $stdout,
@@ -50,8 +50,8 @@ Skyward::Core::CLI.register("rollback", "Undo the latest migration by running it
50
50
  end
51
51
 
52
52
  Skyward::Core::CLI.register(
53
- "status",
54
- "List which migrations have run, and how your table classes differ from the database"
53
+ 'status',
54
+ 'List which migrations have run, and how your table classes differ from the database'
55
55
  ) do |_args|
56
56
  Skyward::ORM::CLI::Status.run(
57
57
  directory: Dir.pwd,
@@ -63,7 +63,7 @@ Skyward::Core::CLI.register(
63
63
  )
64
64
  end
65
65
 
66
- Skyward::Core::CLI.register("scaffold", "Print a table class for a table in the database") do |args|
66
+ Skyward::Core::CLI.register('scaffold', 'Print a table class for a table in the database') do |args|
67
67
  Skyward::ORM::CLI::Scaffold.run(
68
68
  args,
69
69
  directory: Dir.pwd,
@@ -19,13 +19,15 @@ class Skyward::ORM::Connection::Instrumented
19
19
 
20
20
  # @rbs sql: String
21
21
  # @rbs binds: Array[untyped]
22
+ # @rbs prepare: bool
22
23
  # @rbs return: Array[Array[untyped]]
23
- def execute(sql, binds = []) = instrumented(sql, binds) { @connection.execute(sql, binds) }
24
+ def execute(sql, binds = [], prepare: false) = instrumented(sql, binds) { @connection.execute(sql, binds, prepare:) }
24
25
 
25
26
  # @rbs sql: String
26
27
  # @rbs *binds: untyped
28
+ # @rbs prepare: bool
27
29
  # @rbs return: Array[Array[untyped]]
28
- def execute2(sql, *binds) = instrumented(sql, binds) { @connection.execute2(sql, *binds) }
30
+ def execute2(sql, *binds, prepare: false) = instrumented(sql, binds) { @connection.execute2(sql, *binds, prepare:) }
29
31
 
30
32
  # @rbs sql: String
31
33
  # @rbs return: void
@@ -38,7 +38,7 @@ class Skyward::ORM::Connection::Pool
38
38
  take(@idle.pop(timeout: 0)) || open || take(@idle.pop(timeout: @timeout)) || raise(
39
39
  Skyward::ORM::PoolTimeout,
40
40
  "Skyward ORM waited #{@timeout} seconds for a database connection, but all #{@size} in its pool " \
41
- "stayed in use. Pass Skyward.connect a larger pool:, or hold each connection for less time."
41
+ 'stayed in use. Pass Skyward.connect a larger pool:, or hold each connection for less time.'
42
42
  )
43
43
  end
44
44
 
@@ -65,7 +65,7 @@ class Skyward::ORM::Connection::Pool
65
65
  def reusable?(connection)
66
66
  return true unless connection.transaction_active?
67
67
 
68
- connection.execute("ROLLBACK")
68
+ connection.execute('ROLLBACK')
69
69
  !connection.transaction_active?
70
70
  rescue StandardError
71
71
  false
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skyward::ORM::Database::Decoders
4
+ extend self
5
+
6
+ # @rbs (Class row, Array[Skyward::ORM::Tables::Column] columns, Array[Integer] positions,
7
+ # Skyward::ORM::_Adapter adapter) -> ^(Array[untyped]) -> untyped
8
+ def compile(row, columns, positions, adapter)
9
+ # Evaluated in the row class, so the lambda calls its new or allocate.
10
+ row.instance_eval(source(row, columns, positions, adapter), __FILE__, __LINE__)
11
+ end
12
+
13
+ private
14
+
15
+ # @rbs (Class row, Array[Skyward::ORM::Tables::Column] columns, Array[Integer] positions,
16
+ # Skyward::ORM::_Adapter adapter) -> String
17
+ def source(row, columns, positions, adapter)
18
+ values = columns.each_with_index.map do |column, index|
19
+ value = "value#{index}"
20
+ # Only names go into the source, and the adapter's own decodings.
21
+ "(#{value} = values[#{positions.fetch(index)}]).nil? ? nil : #{adapter.decoding(column, value)}"
22
+ end
23
+ # Without it, each String literal a decoding writes would allocate for every row.
24
+ "# frozen_string_literal: true\n->(values) { #{construction(row, columns, values)} }"
25
+ end
26
+
27
+ # @rbs row: Class
28
+ # @rbs columns: Array[Skyward::ORM::Tables::Column]
29
+ # @rbs values: Array[String]
30
+ # @rbs return: String
31
+ def construction(row, columns, values)
32
+ # A table class's own initialize checks the columns it's given, which the
33
+ # source already guarantees, so its row is loaded directly.
34
+ if row.instance_method(:initialize).owner == Skyward::ORM::Record
35
+ "allocate.__send__(:load_columns, #{values.join(', ')})"
36
+ else
37
+ # A member's name as a quoted label, such as "end":, names any Symbol.
38
+ "new(#{columns.zip(values).map { |column, value| "#{column.name.name.inspect}: #{value}" }.join(', ')})"
39
+ end
40
+ end
41
+ end
@@ -52,7 +52,7 @@ module Skyward::ORM::Database::SchemaCheck
52
52
  # @rbs return: String
53
53
  def suggest(name, names)
54
54
  suggestion = Skyward::Core::Diagnostics::Suggestion.closest(name.to_s, names.map(&:to_s))
55
- suggestion ? " Did you mean #{suggestion}?" : ""
55
+ suggestion ? " Did you mean #{suggestion}?" : ''
56
56
  end
57
57
 
58
58
  # @rbs stored: String
@@ -72,8 +72,8 @@ module Skyward::ORM::Database::SchemaCheck
72
72
  def report(klass, table, problems)
73
73
  width = problems.map { |label, _, _| label.length }.max.to_i
74
74
  lines = problems.flat_map do |label, problem, fix|
75
- [" #{label.ljust(width)} #{problem}", " #{" " * width} #{fix}"]
75
+ [" #{label.ljust(width)} #{problem}", " #{' ' * width} #{fix}"]
76
76
  end
77
- ["#{klass} doesn't match the #{table} table", "", *lines].join("\n")
77
+ ["#{klass} doesn't match the #{table} table", '', *lines].join("\n")
78
78
  end
79
79
  end
@@ -14,9 +14,13 @@ class Skyward::ORM::Database
14
14
  :rollback #: Array[String]
15
15
  )
16
16
 
17
- TRANSACTION = Statements.new(start: "BEGIN", commit: ["COMMIT"], rollback: ["ROLLBACK"]) #: Statements
17
+ TRANSACTION = Statements.new(start: 'BEGIN', commit: ['COMMIT'], rollback: ['ROLLBACK']) #: Statements
18
18
 
19
- FOREIGN = "this transaction belongs to another fiber; start your own, or return the result to the block" #: String
19
+ FOREIGN = 'this transaction belongs to another fiber; start your own, or return the result to the block' #: String
20
+
21
+ # Each IN list's length is a shape of its own, and so is each SQL string an
22
+ # app builds at run time, so the cache is bounded.
23
+ SHAPES = 1_000 #: Integer
20
24
 
21
25
  # @rbs @adapter: Skyward::ORM::_Adapter
22
26
  # @rbs @pool: Skyward::ORM::Connection::Pool
@@ -26,19 +30,26 @@ class Skyward::ORM::Database
26
30
  # @rbs @checked: Set[singleton(Skyward::ORM::Record)]
27
31
  # @rbs @lock: Thread::Mutex
28
32
  # @rbs @decoders: Hash[[singleton(Skyward::ORM::Record), Class, Array[Symbol]], ^(Array[untyped]) -> untyped]
33
+ # @rbs @sql: Hash[untyped, String]
34
+ # @rbs @query_decoders: Hash[[Class, Array[Symbol]], ^(Array[untyped]) -> untyped]
29
35
  # @rbs @key: Symbol
36
+ # @rbs @prepared_statements: bool
30
37
 
31
38
  # @rbs (Skyward::ORM::_Adapter adapter, Skyward::ORM::Connection::Pool pool,
32
- # Skyward::ORM::Tables::ColumnTypes column_types, Array[Skyward::ORM::Catalog::Table] tables, check: bool) -> void
33
- def initialize(adapter, pool, column_types, tables, check:)
39
+ # Skyward::ORM::Tables::ColumnTypes column_types, Array[Skyward::ORM::Catalog::Table] tables, check: bool,
40
+ # ?prepared_statements: bool) -> void
41
+ def initialize(adapter, pool, column_types, tables, check:, prepared_statements: true)
34
42
  @adapter = adapter
35
43
  @pool = pool
36
44
  @column_types = column_types
37
45
  @tables = tables
38
46
  @check = check
39
- @checked = Set.new
47
+ @prepared_statements = prepared_statements
48
+ @checked = Set.new.freeze
40
49
  @lock = Thread::Mutex.new
41
50
  @decoders = {}
51
+ @sql = {}
52
+ @query_decoders = {}
42
53
  # Fiber storage takes only a Symbol key, and each database's transactions are its own.
43
54
  @key = :"skyward_orm_transaction_#{object_id}"
44
55
  end
@@ -104,7 +115,9 @@ class Skyward::ORM::Database
104
115
  # @rbs sql: String
105
116
  # @rbs binds: Array[untyped]
106
117
  # @rbs return: Array[Array[untyped]]
107
- def execute(sql, binds = []) = connection { |checked_out| checked_out.execute(sql, binds) }
118
+ def execute(sql, binds = [])
119
+ connection { |checked_out| checked_out.execute(sql, binds, prepare: @prepared_statements) }
120
+ end
108
121
 
109
122
  # @rbs tables: Array[singleton(Skyward::ORM::Record)]
110
123
  # @rbs return: void
@@ -117,7 +130,7 @@ class Skyward::ORM::Database
117
130
  end
118
131
  raise Skyward::ORM::SchemaMismatch, mismatches.join("\n\n") unless mismatches.empty?
119
132
 
120
- @checked.merge(unchecked)
133
+ @checked = (@checked | unchecked).freeze
121
134
  end
122
135
  end
123
136
 
@@ -129,12 +142,26 @@ class Skyward::ORM::Database
129
142
  # @rbs return: ^(Array[untyped]) -> untyped
130
143
  def decoder(table, row, expressed) = @decoders[[table, row, expressed]] ||= build(table, row, expressed)
131
144
 
145
+ # The cache is replaced under the lock rather than changed, so a hit reads
146
+ # it without one, on every Ruby. A miss renders outside the lock, so two
147
+ # fibers may render one shape, and the SQL the first adds is the one kept.
148
+ # @rbs key: untyped
149
+ # @rbs &: () -> String
150
+ # @rbs return: String
151
+ def cached_sql(key)
152
+ @sql.fetch(key) do
153
+ rendered = yield.freeze
154
+ @lock.synchronize { @sql[key] || cache_sql(key, rendered) }
155
+ end
156
+ end
157
+
132
158
  # Every query and write calls this before it touches its table, so a table
133
159
  # class loaded after connect is checked before its first use.
134
160
  # @rbs table: singleton(Skyward::ORM::Record)
135
161
  # @rbs return: void
136
162
  def prepare(table)
137
- check([table]) if @check
163
+ # The checked set is replaced, never changed, so reading it needs no lock.
164
+ check([table]) if @check && !@checked.include?(table)
138
165
  end
139
166
 
140
167
  # @rbs error: StandardError
@@ -150,17 +177,14 @@ class Skyward::ORM::Database
150
177
  rows.map { |row| columns.zip(row).to_h }
151
178
  end
152
179
 
153
- # Raw SQL picks its own columns, in its own order, so they're matched to the
154
- # row class's members by name.
155
180
  # @rbs row: Class
156
181
  # @rbs sql: String
157
182
  # @rbs values: Array[untyped]
158
183
  # @rbs return: Array[untyped]
159
184
  def rows(row, sql, values)
160
185
  columns, fetched = run(sql, values)
161
- members = @column_types.columns(row).to_h { |member| [member.name, member] }
162
- unmatched(row, columns, members.keys)
163
- fetched.map(&compile(row, members.fetch_values(*columns)))
186
+ decode = @query_decoders[[row, columns]] ||= query_decoder(row, columns)
187
+ fetched.map(&decode)
164
188
  end
165
189
 
166
190
  # @rbs migrations: Array[Skyward::ORM::Migrations::Runner::Migration]
@@ -184,7 +208,7 @@ class Skyward::ORM::Database
184
208
  def write(table, sql, binds)
185
209
  prepare(table)
186
210
  connection do |checked_out|
187
- checked_out.execute(sql, binds)
211
+ checked_out.execute(sql, binds, prepare: @prepared_statements)
188
212
  checked_out.changes
189
213
  end
190
214
  rescue StandardError => e
@@ -218,12 +242,23 @@ class Skyward::ORM::Database
218
242
  tables = @adapter.tables(checked_out)
219
243
  @lock.synchronize do
220
244
  @tables = tables
221
- @checked.clear
245
+ @checked = Set.new.freeze
222
246
  end
223
247
  changed
224
248
  end
225
249
  end
226
250
 
251
+ # Dropping the oldest, rather than the least recently used, leaves a hit
252
+ # nothing to change.
253
+ # @rbs key: untyped
254
+ # @rbs sql: String
255
+ # @rbs return: String
256
+ def cache_sql(key, sql)
257
+ kept = @sql.size < SHAPES ? @sql : @sql.except(@sql.each_key.first)
258
+ @sql = kept.merge(key => sql)
259
+ sql
260
+ end
261
+
227
262
  # @rbs held: Held
228
263
  # @rbs return: bool
229
264
  def owned?(held) = held.fiber.equal?(Fiber.current)
@@ -309,21 +344,18 @@ class Skyward::ORM::Database
309
344
  members = @column_types.columns(row).map do |member|
310
345
  member.type == :untyped ? columns.fetch(member.name, member) : member
311
346
  end
312
- compile(row, members)
347
+ Decoders.compile(row, members, members.each_index.to_a, @adapter)
313
348
  end
314
349
 
315
350
  # @rbs row: Class
316
- # @rbs columns: Array[Skyward::ORM::Tables::Column]
351
+ # @rbs columns: Array[Symbol]
317
352
  # @rbs return: ^(Array[untyped]) -> untyped
318
- def compile(row, columns)
319
- lambda do |values|
320
- row.new(
321
- **columns.each_with_index.to_h do |column, index|
322
- value = values.fetch(index)
323
- [column.name, value.nil? ? nil : @adapter.decode(column, value)]
324
- end
325
- )
326
- end
353
+ def query_decoder(row, columns)
354
+ members = @column_types.columns(row)
355
+ unmatched(row, columns, members.map(&:name))
356
+ # Raw SQL picks its own columns, in its own order.
357
+ positions = columns.each_with_index.to_h
358
+ Decoders.compile(row, members, members.map { |member| positions.fetch(member.name) }, @adapter)
327
359
  end
328
360
 
329
361
  # Raw SQL returns rows, with no room for a violation, so one raises.
@@ -331,10 +363,12 @@ class Skyward::ORM::Database
331
363
  # @rbs values: Array[untyped]
332
364
  # @rbs return: [Array[Symbol], Array[Array[untyped]]]
333
365
  def run(sql, values)
334
- statement = Skyward::ORM::Statements::Placeholders.replace(sql, values.size) { |position| @adapter.placeholder(position) }
366
+ statement = cached_sql([:query, sql, values.size]) do
367
+ Skyward::ORM::Statements::Placeholders.replace(sql, values.size) { |position| @adapter.placeholder(position) }
368
+ end
335
369
  binds = values.map { |value| @adapter.encode(value) }
336
- Skyward::ORM::Statements::Placeholders.require_scalars(binds, "Skyward.query")
337
- result = connection { |checked_out| checked_out.execute2(statement, *binds) }
370
+ Skyward::ORM::Statements::Placeholders.require_scalars(binds, 'Skyward.query')
371
+ result = connection { |checked_out| checked_out.execute2(statement, *binds, prepare: @prepared_statements) }
338
372
  [result.fetch(0).map(&:to_sym), result.drop(1)]
339
373
  rescue StandardError => e
340
374
  violation = violation(e, nil)
@@ -349,9 +383,9 @@ class Skyward::ORM::Database
349
383
  # @rbs return: void
350
384
  def unmatched(row, columns, members)
351
385
  extra = columns - members
352
- raise ArgumentError, "#{row} has no member for #{extra.join(", ")}, which the SQL returns" if extra.any?
386
+ raise ArgumentError, "#{row} has no member for #{extra.join(', ')}, which the SQL returns" if extra.any?
353
387
 
354
388
  missing = members - columns
355
- raise ArgumentError, "The SQL returns no #{missing.join(", ")} for #{row}" if missing.any?
389
+ raise ArgumentError, "The SQL returns no #{missing.join(', ')} for #{row}" if missing.any?
356
390
  end
357
391
  end
@@ -10,11 +10,11 @@ module Skyward::ORM::Migrations::Definitions
10
10
  extend self
11
11
 
12
12
  ON_DELETE = {
13
- cascade: "CASCADE",
14
- restrict: "RESTRICT",
15
- set_null: "SET NULL",
16
- set_default: "SET DEFAULT",
17
- no_action: "NO ACTION"
13
+ cascade: 'CASCADE',
14
+ restrict: 'RESTRICT',
15
+ set_null: 'SET NULL',
16
+ set_default: 'SET DEFAULT',
17
+ no_action: 'NO ACTION'
18
18
  }.freeze #: Hash[Skyward::ORM::Tables::on_delete, String]
19
19
 
20
20
  # Each column's constraints, in the order a column lists them, then each
@@ -43,12 +43,12 @@ module Skyward::ORM::Migrations::Definitions
43
43
  def indexes(klass)
44
44
  table = klass.table_name
45
45
  klass.schema.indexes.map do |columns|
46
- name = name(table, columns, "index")
46
+ name = name(table, columns, 'index')
47
47
  Skyward::ORM::Catalog::Index.new(
48
48
  name:,
49
49
  columns:,
50
50
  unique: false,
51
- definition: "CREATE INDEX #{name} ON #{table} (#{columns.join(", ")})"
51
+ definition: "CREATE INDEX #{name} ON #{table} (#{columns.join(', ')})"
52
52
  )
53
53
  end
54
54
  end
@@ -77,8 +77,8 @@ module Skyward::ORM::Migrations::Definitions
77
77
  # @rbs return: String
78
78
  def inline(constraint)
79
79
  clause = case constraint.kind
80
- when :primary_key then "PRIMARY KEY"
81
- when :unique then "UNIQUE"
80
+ when :primary_key then 'PRIMARY KEY'
81
+ when :unique then 'UNIQUE'
82
82
  when :check then constraint.definition
83
83
  else constraint.definition.delete_prefix("FOREIGN KEY (#{constraint.columns.fetch(0)}) ")
84
84
  end
@@ -89,7 +89,7 @@ module Skyward::ORM::Migrations::Definitions
89
89
  # @rbs return: String
90
90
  def literal(value)
91
91
  case value
92
- when nil then "NULL"
92
+ when nil then 'NULL'
93
93
  when String then "'#{value.gsub("'", "''")}'"
94
94
  when Integer, Float, true, false then value.to_s
95
95
  else literal(value.to_s)
@@ -112,7 +112,7 @@ module Skyward::ORM::Migrations::Definitions
112
112
  # @rbs columns: Array[Symbol]
113
113
  # @rbs return: Skyward::ORM::Catalog::Constraint
114
114
  def unique(table, columns)
115
- constraint(name(table, columns, "key"), :unique, columns, "UNIQUE (#{columns.join(", ")})")
115
+ constraint(name(table, columns, 'key'), :unique, columns, "UNIQUE (#{columns.join(', ')})")
116
116
  end
117
117
 
118
118
  # A symbol union's check comes first, as part of the column's type.
@@ -122,7 +122,7 @@ module Skyward::ORM::Migrations::Definitions
122
122
  # @rbs return: Array[Skyward::ORM::Catalog::Constraint]
123
123
  def checks(table, column, checks)
124
124
  name = column.name
125
- symbols = column.symbols.map { |symbol| literal(symbol.name) }.join(", ")
125
+ symbols = column.symbols.map { |symbol| literal(symbol.name) }.join(', ')
126
126
  rules = [
127
127
  *("#{name} IN (#{symbols})" if column.type == :enum),
128
128
  *checks.select { |check| check.column == name }.map { |check| rule(name, check.rule) }
@@ -175,5 +175,5 @@ module Skyward::ORM::Migrations::Definitions
175
175
  # @rbs columns: Array[Symbol]
176
176
  # @rbs suffix: String
177
177
  # @rbs return: String
178
- def name(table, columns, suffix) = "#{table}_#{columns.join("_")}_#{suffix}"
178
+ def name(table, columns, suffix) = "#{table}_#{columns.join('_')}_#{suffix}"
179
179
  end
@@ -5,7 +5,7 @@ module Skyward::ORM::Migrations::Diff
5
5
 
6
6
  ON_DELETE = /\bON\s+DELETE\s+(CASCADE|RESTRICT|SET\s+NULL|SET\s+DEFAULT|NO\s+ACTION)\b/i #: Regexp
7
7
 
8
- NULL = "NULL" #: String
8
+ NULL = 'NULL' #: String
9
9
 
10
10
  RENAMABLE = %i[foreign_key unique].freeze #: Array[Skyward::ORM::Catalog::constraint_kind]
11
11
 
@@ -79,7 +79,7 @@ module Skyward::ORM::Migrations::Diff
79
79
  problems = shared.map do |table, owners|
80
80
  sorted = owners.sort_by(&:to_s)
81
81
  Skyward::ORM::Tables::Sources.explain(
82
- "#{sorted.join(" and ")} each declare table :#{table}. Give each table one class.",
82
+ "#{sorted.join(' and ')} each declare table :#{table}. Give each table one class.",
83
83
  sorted.map { |owner| Skyward::ORM::Tables::Sources.declaration(owner, :table, table) }
84
84
  )
85
85
  end
@@ -268,7 +268,7 @@ module Skyward::ORM::Migrations::Diff
268
268
  # @rbs return: String
269
269
  def on_delete(constraint)
270
270
  action = constraint.definition[ON_DELETE, 1]
271
- action ? action.upcase.gsub(/\s+/, " ") : "NO ACTION"
271
+ action ? action.upcase.gsub(/\s+/, ' ') : 'NO ACTION'
272
272
  end
273
273
 
274
274
  # An index is matched by its columns, so one a database already has under
@@ -300,7 +300,7 @@ module Skyward::ORM::Migrations::Diff
300
300
  parent = tables.find { |table| table.name == constraint.references }
301
301
  key = parent&.constraints&.find { |candidate| candidate.kind == :primary_key }
302
302
  names = named.scan(NAME) #: Array[String]
303
- names.map { |name| name.delete("\"`[]").to_sym } == key&.columns
303
+ names.map { |name| name.delete('"`[]').to_sym } == key&.columns
304
304
  end
305
305
 
306
306
  # @rbs constraint: Skyward::ORM::Catalog::Constraint
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "fileutils"
3
+ require 'fileutils'
4
4
 
5
5
  module Skyward::ORM::Migrations::Directory
6
6
  extend self
7
7
 
8
- PATH = "db/migrate" #: String
8
+ PATH = 'db/migrate' #: String
9
9
 
10
10
  # @rbs directory: String
11
11
  # @rbs name: String
@@ -26,7 +26,7 @@ module Skyward::ORM::Migrations::Directory
26
26
  # @rbs sql: String
27
27
  # @rbs return: String
28
28
  def write(directory, time, name, sql)
29
- file = File.join(PATH, "#{time.getutc.strftime("%Y%m%d%H%M%S")}_#{name}.sql")
29
+ file = File.join(PATH, "#{time.getutc.strftime('%Y%m%d%H%M%S')}_#{name}.sql")
30
30
  FileUtils.mkdir_p(File.join(directory, PATH))
31
31
  File.write(File.join(directory, file), sql)
32
32
  file
@@ -34,11 +34,11 @@ module Skyward::ORM::Migrations::Directory
34
34
 
35
35
  # @rbs path: String
36
36
  # @rbs return: Array[String]
37
- def sql_files(path) = Dir.glob("*.sql", base: path).sort
37
+ def sql_files(path) = Dir.glob('*.sql', base: path).sort
38
38
 
39
39
  # @rbs file: String
40
40
  # @rbs return: String
41
- def version(file) = File.basename(file).split("_", 2).fetch(0)
41
+ def version(file) = File.basename(file).split('_', 2).fetch(0)
42
42
 
43
43
  private
44
44
 
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "digest"
3
+ require 'digest'
4
4
 
5
5
  module Skyward::ORM::Migrations::Runner
6
6
  extend self
@@ -108,7 +108,7 @@ module Skyward::ORM::Migrations::Runner
108
108
  when Edited
109
109
  files = failure.files.map { |file| " #{file}\n" }.join
110
110
  "These migrations changed after they ran, so none ran:\n#{files}" \
111
- "Put each back as it was, and make the change in a new migration. Only a -- down section can change."
111
+ 'Put each back as it was, and make the change in a new migration. Only a -- down section can change.'
112
112
  when Locked
113
113
  "#{failure.file} couldn't take the migration lock, which another migrator may hold, so neither it nor any " \
114
114
  "later migration ran: #{failure.message}"
@@ -123,10 +123,10 @@ module Skyward::ORM::Migrations::Runner
123
123
  when :nothing then "No migration has run, so there's nothing to roll back."
124
124
  when Missing
125
125
  "#{failure.version} is the latest migration to have run, but its file is gone, so it can't be rolled back. " \
126
- "Put its file back to roll it back."
126
+ 'Put its file back to roll it back.'
127
127
  when Irreversible
128
128
  "#{failure.file} has nothing in its -- down section, so it wasn't rolled back. " \
129
- "Write the SQL that undoes it there."
129
+ 'Write the SQL that undoes it there.'
130
130
  when Raced
131
131
  "#{failure.file} isn't the latest migration anymore, since another migrator ran or rolled back one while " \
132
132
  "this one waited for the lock, so it wasn't rolled back."
@@ -156,7 +156,7 @@ module Skyward::ORM::Migrations::Runner
156
156
  "CREATE TABLE IF NOT EXISTS #{TABLE} (version TEXT PRIMARY KEY, checksum TEXT NOT NULL, " \
157
157
  "ran_at #{adapter.sql_type(RAN_AT)} NOT NULL DEFAULT CURRENT_TIMESTAMP)"
158
158
  )
159
- connection.execute("COMMIT")
159
+ connection.execute('COMMIT')
160
160
  settled = true
161
161
  ensure
162
162
  cancel(connection) unless settled
@@ -187,7 +187,7 @@ module Skyward::ORM::Migrations::Runner
187
187
  settled = false
188
188
  begin
189
189
  applied = yield
190
- connection.execute(applied.is_a?(Failed) ? "ROLLBACK" : "COMMIT")
190
+ connection.execute(applied.is_a?(Failed) ? 'ROLLBACK' : 'COMMIT')
191
191
  settled = true
192
192
  applied
193
193
  rescue StandardError => e
@@ -207,7 +207,7 @@ module Skyward::ORM::Migrations::Runner
207
207
  # ROLLBACK, and then ROLLBACK raises, though the outcome is already decided.
208
208
  # @rbs (Skyward::ORM::_Connection connection) -> void
209
209
  def cancel(connection)
210
- connection.execute("ROLLBACK")
210
+ connection.execute('ROLLBACK')
211
211
  rescue StandardError
212
212
  nil
213
213
  end
@@ -228,7 +228,7 @@ module Skyward::ORM::Migrations::Runner
228
228
  connection.execute_batch(section)
229
229
  violations = adapter.foreign_key_violations(connection)
230
230
  if violations.any?
231
- return Failed.new(file: migration.file, message: "foreign key check failed: #{violations.join(", ")}")
231
+ return Failed.new(file: migration.file, message: "foreign key check failed: #{violations.join(', ')}")
232
232
  end
233
233
 
234
234
  yield
@@ -32,8 +32,8 @@ module Skyward::ORM::Migrations::Script
32
32
  next if generated.empty? || in_place?(change, adapter)
33
33
 
34
34
  "Changing table :#{change.table.name} rebuilds it, which would lose its generated " \
35
- "#{generated.one? ? "column" : "columns"} #{generated.join(", ")}. " \
36
- "Write this migration yourself with `skyward migration --empty`."
35
+ "#{generated.one? ? 'column' : 'columns'} #{generated.join(', ')}. " \
36
+ 'Write this migration yourself with `skyward migration --empty`.'
37
37
  end
38
38
  end
39
39
  raise Skyward::Core::BootError, problems.join("\n") unless problems.empty?
@@ -207,7 +207,7 @@ module Skyward::ORM::Migrations::Script
207
207
  # @rbs from: Array[Symbol]
208
208
  # @rbs return: String
209
209
  def copy(table, into, from)
210
- "INSERT INTO #{table}_new (#{into.join(", ")}) SELECT #{from.join(", ")} FROM #{table};\n" \
210
+ "INSERT INTO #{table}_new (#{into.join(', ')}) SELECT #{from.join(', ')} FROM #{table};\n" \
211
211
  "DROP TABLE #{table};\n" \
212
212
  "ALTER TABLE #{table}_new RENAME TO #{table};\n"
213
213
  end
@@ -240,7 +240,7 @@ module Skyward::ORM::Migrations::Script
240
240
  entries = [*lines, *table_constraints]
241
241
  last = entries.rindex { |definition, _| definition }
242
242
  body = entries.each_with_index.map do |(definition, text), index|
243
- " #{text}#{"," if definition && index != last}\n"
243
+ " #{text}#{',' if definition && index != last}\n"
244
244
  end
245
245
  ["CREATE TABLE #{name} (\n", *body, ");\n"].join
246
246
  end
@@ -281,12 +281,12 @@ module Skyward::ORM::Migrations::Script
281
281
  generated = generated?(klass.schema, column)
282
282
  default = Skyward::ORM::Migrations::Definitions.default(klass, column, adapter)
283
283
  clauses = [
284
- ("NOT NULL" unless (typed ? column.nullable : kept&.nullable) || generated),
284
+ ('NOT NULL' unless (typed ? column.nullable : kept&.nullable) || generated),
285
285
  ("DEFAULT #{default}" if default),
286
286
  *constraints.select { |constraint| constraint.columns == [name] }
287
287
  .map { |constraint| Skyward::ORM::Migrations::Definitions.inline(constraint) }
288
288
  ].compact
289
- [[sql_type, (adapter.identity if generated)].compact.join(" "), clauses.join(" ")]
289
+ [[sql_type, (adapter.identity if generated)].compact.join(' '), clauses.join(' ')]
290
290
  end
291
291
 
292
292
  # @rbs schema: Skyward::ORM::Tables::Schema
@@ -297,12 +297,12 @@ module Skyward::ORM::Migrations::Script
297
297
  # A column as the database has it.
298
298
  # @rbs column: Skyward::ORM::Catalog::Column
299
299
  # @rbs return: String
300
- def column(column) = [column.name, (column.type unless column.type.empty?), clauses(column)].compact.join(" ").rstrip
300
+ def column(column) = [column.name, (column.type unless column.type.empty?), clauses(column)].compact.join(' ').rstrip
301
301
 
302
302
  # @rbs column: Skyward::ORM::Catalog::Column
303
303
  # @rbs return: String
304
304
  def clauses(column)
305
- [("NOT NULL" unless column.nullable), ("DEFAULT #{column.default}" if column.default)].compact.join(" ")
305
+ [('NOT NULL' unless column.nullable), ("DEFAULT #{column.default}" if column.default)].compact.join(' ')
306
306
  end
307
307
 
308
308
  # @rbs table: Symbol
@@ -334,7 +334,7 @@ module Skyward::ORM::Migrations::Script
334
334
  # @rbs value: String?
335
335
  # @rbs return: String
336
336
  def default(table, column, value)
337
- "ALTER TABLE #{table} ALTER COLUMN #{column} #{value ? "SET DEFAULT #{value}" : "DROP DEFAULT"};\n"
337
+ "ALTER TABLE #{table} ALTER COLUMN #{column} #{value ? "SET DEFAULT #{value}" : 'DROP DEFAULT'};\n"
338
338
  end
339
339
 
340
340
  # @rbs table: Symbol