skyward-orm 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +200 -31
  3. data/lib/skyward/orm/adapters/postgresql.rb +140 -38
  4. data/lib/skyward/orm/adapters/sqlite/constraints.rb +18 -0
  5. data/lib/skyward/orm/adapters/sqlite.rb +130 -21
  6. data/lib/skyward/orm/adapters.rb +12 -2
  7. data/lib/skyward/orm/catalog/column.rb +2 -1
  8. data/lib/skyward/orm/cli/boot_file.rb +2 -2
  9. data/lib/skyward/orm/cli/migrate.rb +1 -1
  10. data/lib/skyward/orm/cli/migration.rb +1 -1
  11. data/lib/skyward/orm/cli/scaffold/source.rb +304 -0
  12. data/lib/skyward/orm/cli/scaffold.rb +70 -0
  13. data/lib/skyward/orm/cli/status.rb +5 -3
  14. data/lib/skyward/orm/commands.rb +12 -0
  15. data/lib/skyward/orm/connection/instrumented.rb +69 -0
  16. data/lib/skyward/orm/connection/session.rb +74 -5
  17. data/lib/skyward/orm/{connection/database → database}/schema_check.rb +20 -8
  18. data/lib/skyward/orm/{connection/database.rb → database.rb} +15 -15
  19. data/lib/skyward/orm/execution.rb +9 -0
  20. data/lib/skyward/orm/migrations/alter_table.rb +6 -2
  21. data/lib/skyward/orm/migrations/definitions.rb +7 -4
  22. data/lib/skyward/orm/migrations/diff.rb +154 -30
  23. data/lib/skyward/orm/migrations/rename_constraint.rb +8 -0
  24. data/lib/skyward/orm/migrations/runner.rb +13 -3
  25. data/lib/skyward/orm/migrations/script.rb +60 -20
  26. data/lib/skyward/orm/migrations/status.rb +32 -3
  27. data/lib/skyward/orm/migrations/summary.rb +3 -6
  28. data/lib/skyward/orm/migrations/versions.rb +1 -1
  29. data/lib/skyward/orm/migrations.rb +1 -1
  30. data/lib/skyward/orm/projection.rb +23 -4
  31. data/lib/skyward/orm/queries/clauses.rb +117 -32
  32. data/lib/skyward/orm/record.rb +23 -1
  33. data/lib/skyward/orm/{placeholders.rb → statements/placeholders.rb} +1 -1
  34. data/lib/skyward/orm/statements.rb +171 -30
  35. data/lib/skyward/orm/tables/column.rb +2 -1
  36. data/lib/skyward/orm/tables/column_types.rb +62 -15
  37. data/lib/skyward/orm/tables/declarations.rb +38 -10
  38. data/lib/skyward/orm/{members.rb → tables/members.rb} +1 -1
  39. data/lib/skyward/orm/tables/sources.rb +63 -0
  40. data/lib/skyward/orm/tables/validation.rb +12 -3
  41. data/lib/skyward/orm/tables/values.rb +115 -5
  42. data/lib/skyward/orm/tables.rb +2 -1
  43. data/lib/skyward/orm/transactional.rb +2 -2
  44. data/lib/skyward/orm/version.rb +1 -1
  45. data/lib/skyward/orm.rb +44 -10
  46. data/sig/generated/skyward/orm/adapters/postgresql.rbs +53 -14
  47. data/sig/generated/skyward/orm/adapters/sqlite/constraints.rbs +6 -0
  48. data/sig/generated/skyward/orm/adapters/sqlite.rbs +67 -7
  49. data/sig/generated/skyward/orm/adapters.rbs +3 -0
  50. data/sig/generated/skyward/orm/catalog/column.rbs +6 -4
  51. data/sig/generated/skyward/orm/cli/boot_file.rbs +4 -4
  52. data/sig/generated/skyward/orm/cli/migrate.rbs +2 -2
  53. data/sig/generated/skyward/orm/cli/migration.rbs +2 -2
  54. data/sig/generated/skyward/orm/cli/scaffold/source.rbs +147 -0
  55. data/sig/generated/skyward/orm/cli/scaffold.rbs +28 -0
  56. data/sig/generated/skyward/orm/cli/status.rbs +2 -2
  57. data/sig/generated/skyward/orm/connection/instrumented.rbs +45 -0
  58. data/sig/generated/skyward/orm/connection/session.rbs +40 -6
  59. data/sig/generated/skyward/orm/{connection/database → database}/schema_check.rbs +11 -4
  60. data/sig/generated/skyward/orm/{connection/database.rbs → database.rbs} +4 -5
  61. data/sig/generated/skyward/orm/execution.rbs +18 -0
  62. data/sig/generated/skyward/orm/migrations/alter_table.rbs +11 -5
  63. data/sig/generated/skyward/orm/migrations/definitions.rbs +2 -2
  64. data/sig/generated/skyward/orm/migrations/diff.rbs +77 -11
  65. data/sig/generated/skyward/orm/migrations/rename_constraint.rbs +16 -0
  66. data/sig/generated/skyward/orm/migrations/runner.rbs +5 -0
  67. data/sig/generated/skyward/orm/migrations/script.rbs +32 -8
  68. data/sig/generated/skyward/orm/migrations/status.rbs +15 -2
  69. data/sig/generated/skyward/orm/migrations/summary.rbs +0 -1
  70. data/sig/generated/skyward/orm/migrations/versions.rbs +2 -2
  71. data/sig/generated/skyward/orm/migrations.rbs +1 -1
  72. data/sig/generated/skyward/orm/projection.rbs +10 -2
  73. data/sig/generated/skyward/orm/queries/clauses.rbs +60 -24
  74. data/sig/generated/skyward/orm/record.rbs +8 -0
  75. data/sig/generated/skyward/orm/{placeholders.rbs → statements/placeholders.rbs} +2 -2
  76. data/sig/generated/skyward/orm/statements.rbs +83 -5
  77. data/sig/generated/skyward/orm/tables/column.rbs +6 -4
  78. data/sig/generated/skyward/orm/tables/column_types.rbs +18 -4
  79. data/sig/generated/skyward/orm/tables/declarations.rbs +15 -1
  80. data/sig/generated/skyward/orm/{members.rbs → tables/members.rbs} +2 -2
  81. data/sig/generated/skyward/orm/tables/sources.rbs +32 -0
  82. data/sig/generated/skyward/orm/tables/values.rbs +41 -1
  83. data/sig/generated/skyward/orm/tables.rbs +3 -1
  84. data/sig/generated/skyward/orm.rbs +6 -6
  85. data/sig/manual/pg.rbs +12 -0
  86. data/sig/manual/skyward/orm/cli/scaffold/source.rbs +3 -0
  87. data/sig/manual/skyward/orm/cli/scaffold.rbs +3 -0
  88. data/sig/manual/skyward/orm/database/schema_check.rbs +3 -0
  89. data/sig/manual/skyward/orm/statements/placeholders.rbs +3 -0
  90. data/sig/manual/skyward/orm/tables/members.rbs +3 -0
  91. data/sig/manual/skyward/orm/tables/sources.rbs +3 -0
  92. data/sig/manual/skyward/orm.rbs +32 -5
  93. metadata +43 -14
  94. data/sig/manual/skyward/orm/connection/database/schema_check.rbs +0 -3
  95. data/sig/manual/skyward/orm/members.rbs +0 -3
  96. data/sig/manual/skyward/orm/placeholders.rbs +0 -3
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Skyward::ORM::Connection::Database
3
+ class Skyward::ORM::Database
4
4
  Held = Data.define(
5
5
  :connection, #: Skyward::ORM::_Connection
6
6
  :fiber, #: Fiber
@@ -158,9 +158,9 @@ class Skyward::ORM::Connection::Database
158
158
  # @rbs return: Array[untyped]
159
159
  def rows(row, sql, values)
160
160
  columns, fetched = run(sql, values)
161
- types = @column_types.columns(row).to_h { |member| [member.name, member.type] }
162
- unmatched(row, columns, types.keys)
163
- fetched.map(&compile(row, columns, types.fetch_values(*columns)))
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)))
164
164
  end
165
165
 
166
166
  # @rbs migrations: Array[Skyward::ORM::Migrations::Runner::Migration]
@@ -305,22 +305,22 @@ class Skyward::ORM::Connection::Database
305
305
  def build(table, row, expressed)
306
306
  prepare(table)
307
307
  # An expression reads no column, so without a type of its own its value passes through.
308
- columns = @column_types.columns(table).to_h { |column| [column.name, column.type] }.except(*expressed)
309
- members = @column_types.columns(row)
310
- types = members.map { |member| member.type == :untyped ? columns[member.name] || :untyped : member.type }
311
- compile(row, members.map(&:name), types)
308
+ columns = Skyward::ORM::Statements.by_name(@column_types, table).except(*expressed)
309
+ members = @column_types.columns(row).map do |member|
310
+ member.type == :untyped ? columns.fetch(member.name, member) : member
311
+ end
312
+ compile(row, members)
312
313
  end
313
314
 
314
315
  # @rbs row: Class
315
- # @rbs members: Array[Symbol]
316
- # @rbs types: Array[Skyward::ORM::Tables::column_type]
316
+ # @rbs columns: Array[Skyward::ORM::Tables::Column]
317
317
  # @rbs return: ^(Array[untyped]) -> untyped
318
- def compile(row, members, types)
318
+ def compile(row, columns)
319
319
  lambda do |values|
320
320
  row.new(
321
- **members.each_with_index.to_h do |member, index|
321
+ **columns.each_with_index.to_h do |column, index|
322
322
  value = values.fetch(index)
323
- [member, value.nil? ? nil : @adapter.decode(types.fetch(index), value)]
323
+ [column.name, value.nil? ? nil : @adapter.decode(column, value)]
324
324
  end
325
325
  )
326
326
  end
@@ -331,9 +331,9 @@ class Skyward::ORM::Connection::Database
331
331
  # @rbs values: Array[untyped]
332
332
  # @rbs return: [Array[Symbol], Array[Array[untyped]]]
333
333
  def run(sql, values)
334
- statement = Skyward::ORM::Placeholders.replace(sql, values.size) { |position| @adapter.placeholder(position) }
334
+ statement = Skyward::ORM::Statements::Placeholders.replace(sql, values.size) { |position| @adapter.placeholder(position) }
335
335
  binds = values.map { |value| @adapter.encode(value) }
336
- Skyward::ORM::Placeholders.require_scalars(binds, "Skyward.query")
336
+ Skyward::ORM::Statements::Placeholders.require_scalars(binds, "Skyward.query")
337
337
  result = connection { |checked_out| checked_out.execute2(statement, *binds) }
338
338
  [result.fetch(0).map(&:to_sym), result.drop(1)]
339
339
  rescue StandardError => e
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skyward::ORM
4
+ Execution = Data.define(
5
+ :sql, #: String
6
+ :binds, #: Array[untyped]
7
+ :adapter #: Symbol
8
+ )
9
+ end
@@ -2,11 +2,15 @@
2
2
 
3
3
  module Skyward::ORM::Migrations
4
4
  # A table that differs from its class: the class's columns, the table as the
5
- # database has it, and each difference, in the order the migration makes them.
5
+ # database has it, each difference, in the order the migration makes them,
6
+ # and the indexes and constraints the table keeps because the class can't
7
+ # declare them.
6
8
  AlterTable = Data.define(
7
9
  :klass, #: singleton(Skyward::ORM::Record)
8
10
  :columns, #: Array[Skyward::ORM::Tables::Column]
9
11
  :table, #: Skyward::ORM::Catalog::Table
10
- :edits #: Array[edit]
12
+ :edits, #: Array[edit]
13
+ :kept_indexes, #: Array[Skyward::ORM::Catalog::Index]
14
+ :kept_constraints #: Array[Skyward::ORM::Catalog::Constraint]
11
15
  )
12
16
  end
@@ -55,17 +55,20 @@ module Skyward::ORM::Migrations::Definitions
55
55
 
56
56
  # The SQL that follows the column's DEFAULT, or nil when it has none.
57
57
  # @rbs klass: singleton(Skyward::ORM::Record)
58
- # @rbs column: Symbol
58
+ # @rbs column: Skyward::ORM::Tables::Column
59
59
  # @rbs adapter: Skyward::ORM::_Adapter
60
60
  # @rbs return: String?
61
61
  def default(klass, column, adapter)
62
62
  defaults = klass.schema.defaults
63
- return unless defaults.key?(column)
63
+ return unless defaults.key?(column.name)
64
64
 
65
- value = defaults.fetch(column)
65
+ value = defaults.fetch(column.name)
66
66
  case value
67
67
  when Skyward::ORM::Tables::SQL then adapter.default_expression(value.expression)
68
- else literal(adapter.encode(value))
68
+ else
69
+ encoded = Skyward::ORM::Statements.encode(adapter, column, value)
70
+ # A finite decimal's text is a number, which both catalogs read back unquoted.
71
+ column.type == :decimal && !value.nil? && value.finite? ? encoded : literal(encoded)
69
72
  end
70
73
  end
71
74
 
@@ -7,6 +7,28 @@ module Skyward::ORM::Migrations::Diff
7
7
 
8
8
  NULL = "NULL" #: String
9
9
 
10
+ RENAMABLE = %i[foreign_key unique].freeze #: Array[Skyward::ORM::Catalog::constraint_kind]
11
+
12
+ # A name as SQLite or PostgreSQL writes it, quoted or not.
13
+ NAME = /"(?:[^"]|"")*"|`(?:[^`]|``)*`|\[[^\]]*\]|[\p{L}_][\p{L}\p{N}_$]*/ #: Regexp
14
+ # A name with its schema, if it has one.
15
+ QUALIFIED = /(?:#{NAME}\s*\.\s*)?#{NAME}/ #: Regexp
16
+
17
+ # An index a class can declare: on columns alone, by PostgreSQL's btree,
18
+ # with no WHERE or INCLUDE, and not unique, since a class makes that only as
19
+ # a unique constraint.
20
+ PLAIN_INDEX = /
21
+ \A\s*CREATE\s+INDEX\s+(?:IF\s+NOT\s+EXISTS\s+)?#{QUALIFIED}\s+ON\s+#{QUALIFIED}\s*(?:USING\s+btree\s*)?
22
+ \(\s*#{NAME}(?:\s*,\s*#{NAME})*\s*\)\s*\z
23
+ /ix #: Regexp
24
+
25
+ # The columns a foreign key names after its table, if it names any.
26
+ REFERENCED = /\bREFERENCES\s+#{QUALIFIED}\s*(?:\((?<columns>[^)]*)\))?/i #: Regexp
27
+
28
+ QUOTED = /\A'((?:[^']|'')*)'\z/ #: Regexp
29
+
30
+ BY_VALUE = %i[json array].freeze #: Array[Skyward::ORM::Tables::column_type]
31
+
10
32
  # New tables come first, each after the tables it references, so a changed
11
33
  # table can reference a new table too. A table with no class is left alone:
12
34
  # a class that isn't loaded looks the same.
@@ -23,11 +45,29 @@ module Skyward::ORM::Migrations::Diff
23
45
  end
24
46
  altered = sorted.filter_map do |klass|
25
47
  table = existing[klass.table_name]
26
- altered(klass, column_types.columns(klass), table, adapter, rename) if table
48
+ altered(klass, column_types.columns(klass), table, tables, adapter, rename) if table
27
49
  end
28
50
  [*parents_first(created), *altered]
29
51
  end
30
52
 
53
+ # The indexes and constraints the table has that the class can't declare,
54
+ # which the diff neither drops nor adds. PostgreSQL puts a check on every
55
+ # column it mentions, so one the class declares is matched by its name first.
56
+ # @rbs klass: singleton(Skyward::ORM::Record)
57
+ # @rbs columns: Array[Skyward::ORM::Tables::Column]
58
+ # @rbs table: Skyward::ORM::Catalog::Table
59
+ # @rbs tables: Array[Skyward::ORM::Catalog::Table]
60
+ # @rbs return: [Array[Skyward::ORM::Catalog::Index], Array[Skyward::ORM::Catalog::Constraint]]
61
+ def kept(klass, columns, table, tables)
62
+ wanted = Skyward::ORM::Migrations::Definitions.constraints(klass, columns)
63
+ [
64
+ table.indexes.reject { |index| index.definition.match?(PLAIN_INDEX) },
65
+ table.constraints.reject do |constraint|
66
+ declarable?(constraint, tables) || wanted.any? { |want| same?(constraint, want) }
67
+ end
68
+ ]
69
+ end
70
+
31
71
  private
32
72
 
33
73
  # @rbs classes: Array[singleton(Skyward::ORM::Record)]
@@ -37,31 +77,41 @@ module Skyward::ORM::Migrations::Diff
37
77
  return if shared.empty?
38
78
 
39
79
  problems = shared.map do |table, owners|
40
- "#{owners.map(&:to_s).sort.join(" and ")} each declare table :#{table}. Give each table one class."
80
+ sorted = owners.sort_by(&:to_s)
81
+ Skyward::ORM::Tables::Sources.explain(
82
+ "#{sorted.join(" and ")} each declare table :#{table}. Give each table one class.",
83
+ sorted.map { |owner| Skyward::ORM::Tables::Sources.declaration(owner, :table, table) }
84
+ )
41
85
  end
42
- raise Skyward::Core::BootError, problems.join("\n")
86
+ raise Skyward::Core::BootError, Skyward::ORM::Tables::Sources.join(problems)
43
87
  end
44
88
 
45
89
  # Indexes and constraints go before the columns they're on, and come back
46
90
  # after the columns they need.
47
91
  # @rbs (singleton(Skyward::ORM::Record) klass, Array[Skyward::ORM::Tables::Column] columns,
48
- # Skyward::ORM::Catalog::Table table, Skyward::ORM::_Adapter adapter,
92
+ # Skyward::ORM::Catalog::Table table, Array[Skyward::ORM::Catalog::Table] tables, Skyward::ORM::_Adapter adapter,
49
93
  # ^(Symbol table, Symbol from, Symbol to) -> bool rename) -> Skyward::ORM::Migrations::AlterTable?
50
- def altered(klass, columns, table, adapter, rename)
94
+ def altered(klass, columns, table, tables, adapter, rename)
51
95
  names = columns.map(&:name)
52
96
  had = table.columns.map(&:name)
53
97
  added = columns.reject { |column| had.include?(column.name) }
54
- removed = table.columns.reject { |column| names.include?(column.name) }
98
+ # A generated column is made and changed by a hand-written migration, so one
99
+ # the class doesn't declare is never dropped.
100
+ removed = table.columns.reject { |column| column.generated || names.include?(column.name) }
55
101
  renamed = renames(klass.table_name, removed, added, adapter, rename)
56
102
  new_columns = added.reject { |column| renamed.value?(column.name) }
57
- dropped_indexes, added_indexes = indexes(klass, table, renamed)
58
- dropped_constraints, added_constraints = constraints(klass, columns, table, renamed, new_columns)
103
+ kept_indexes, kept_constraints = kept(klass, columns, table, tables)
104
+ compared = table.with(indexes: table.indexes - kept_indexes, constraints: table.constraints - kept_constraints)
105
+ dropped_indexes, added_indexes = indexes(klass, compared, renamed)
106
+ dropped_constraints, renamed_constraints, added_constraints =
107
+ constraints(klass, columns, compared, renamed, new_columns)
59
108
  dropped_columns = removed.reject { |column| renamed.key?(column.name) }
60
109
  edits = [
61
110
  *dropped_indexes,
62
111
  *dropped_constraints,
63
112
  *dropped_columns.map { |column| Skyward::ORM::Migrations::DropColumn.new(column:) },
64
113
  *renamed.map { |from, to| Skyward::ORM::Migrations::RenameColumn.new(from:, to:) },
114
+ *renamed_constraints,
65
115
  *new_columns.map { |column| Skyward::ORM::Migrations::AddColumn.new(column:) },
66
116
  *columns.flat_map do |column|
67
117
  found = table.columns.find { |kept| renamed.fetch(kept.name, kept.name) == column.name }
@@ -70,7 +120,9 @@ module Skyward::ORM::Migrations::Diff
70
120
  *added_constraints,
71
121
  *added_indexes
72
122
  ] #: Array[Skyward::ORM::Migrations::edit]
73
- Skyward::ORM::Migrations::AlterTable.new(klass:, columns:, table:, edits:) unless edits.empty?
123
+ return if edits.empty?
124
+
125
+ Skyward::ORM::Migrations::AlterTable.new(klass:, columns:, table:, edits:, kept_indexes:, kept_constraints:)
74
126
  end
75
127
 
76
128
  # The indexes to drop and to add. One is compared by the columns it will
@@ -93,23 +145,33 @@ module Skyward::ORM::Migrations::Diff
93
145
  ]
94
146
  end
95
147
 
96
- # The constraints to drop and to add, compared as indexes are. A new
97
- # column's own constraints are part of its definition.
148
+ # The constraints to drop, to rename and to add, compared as indexes are. A
149
+ # foreign key or unique constraint the class declares that the table has
150
+ # under another name is renamed, from the first one if it has two, and the
151
+ # other is dropped. A new column's own constraints are part of its definition.
98
152
  # @rbs (singleton(Skyward::ORM::Record) klass, Array[Skyward::ORM::Tables::Column] columns,
99
153
  # Skyward::ORM::Catalog::Table table, Hash[Symbol, Symbol] renamed, Array[Skyward::ORM::Tables::Column] new_columns)
100
- # -> [Array[Skyward::ORM::Migrations::edit], Array[Skyward::ORM::Migrations::edit]]
154
+ # -> [Array[Skyward::ORM::Migrations::edit], Array[Skyward::ORM::Migrations::edit],
155
+ # Array[Skyward::ORM::Migrations::edit]]
101
156
  def constraints(klass, columns, table, renamed, new_columns)
102
157
  wanted = Skyward::ORM::Migrations::Definitions.constraints(klass, columns)
103
158
  pairs = table.constraints.map do |constraint|
104
159
  [constraint, constraint.with(columns: moved(constraint.columns, renamed))]
105
160
  end #: Array[[Skyward::ORM::Catalog::Constraint, Skyward::ORM::Catalog::Constraint]]
106
- moved = pairs.map(&:last)
107
- dropped = pairs.reject { |_, constraint| wanted.any? { |want| same?(constraint, want) } }
108
- added = wanted.reject do |want|
109
- moved.any? { |constraint| same?(constraint, want) } || inline?(want, new_columns)
161
+ unmatched = pairs.reject { |_, constraint| wanted.any? { |want| same?(constraint, want) } }
162
+ missing = wanted.reject { |want| pairs.any? { |_, constraint| same?(constraint, want) } }
163
+ renames = missing.filter_map do |want|
164
+ from = unmatched.find { |_, constraint| renamed?(constraint, want) }&.first&.name
165
+ to = want.name
166
+ Skyward::ORM::Migrations::RenameConstraint.new(from:, to:) if from && to
167
+ end
168
+ dropped = unmatched.reject { |constraint, _| renames.any? { |rename| rename.from == constraint.name } }
169
+ added = missing.reject do |want|
170
+ renames.any? { |rename| rename.to == want.name } || inline?(want, new_columns)
110
171
  end
111
172
  [
112
173
  dropped.map { |constraint, _| Skyward::ORM::Migrations::DropConstraint.new(constraint:) },
174
+ renames,
113
175
  added.map { |constraint| Skyward::ORM::Migrations::AddConstraint.new(constraint:) }
114
176
  ]
115
177
  end
@@ -122,14 +184,15 @@ module Skyward::ORM::Migrations::Diff
122
184
  none = {} #: Hash[Symbol, Symbol]
123
185
  removed.reduce(none) do |renamed, gone|
124
186
  to = added.find do |column|
125
- !renamed.value?(column.name) && adapter.compatible?(column.type, gone.type) &&
187
+ !renamed.value?(column.name) && adapter.compatible?(column, gone.type) &&
126
188
  rename.call(table, gone.name, column.name)
127
189
  end
128
190
  to ? renamed.merge(gone.name => to.name) : renamed
129
191
  end
130
192
  end
131
193
 
132
- # An untyped column has no type or nullability to compare.
194
+ # An untyped column has no type or nullability to compare, and a generated
195
+ # one no default.
133
196
  # @rbs klass: singleton(Skyward::ORM::Record)
134
197
  # @rbs column: Skyward::ORM::Tables::Column
135
198
  # @rbs had: Skyward::ORM::Catalog::Column
@@ -137,11 +200,12 @@ module Skyward::ORM::Migrations::Diff
137
200
  # @rbs return: Array[Skyward::ORM::Migrations::edit]
138
201
  def changed(klass, column, had, adapter)
139
202
  name = column.name
140
- sql_type = adapter.sql_type(column.type)
141
- retyped = sql_type unless adapter.compatible?(column.type, had.type)
142
- default = Skyward::ORM::Migrations::Definitions.default(klass, name, adapter)
203
+ sql_type = adapter.sql_type(column)
204
+ retyped = sql_type unless adapter.compatible?(column, had.type)
205
+ default = Skyward::ORM::Migrations::Definitions.default(klass, column, adapter)
143
206
  # PostgreSQL keeps no default for DEFAULT NULL, which is the same as none.
144
- redefaulted = (had.default || NULL) != (default || NULL)
207
+ wanted = adapter.catalog_default(default || NULL)
208
+ redefaulted = !had.generated && !same_default?(column, had.default || NULL, wanted, adapter)
145
209
  renulled = sql_type && had.nullable != column.nullable
146
210
  [
147
211
  (Skyward::ORM::Migrations::ChangeType.new(column: name, from: had.type, to: retyped) if retyped),
@@ -150,19 +214,54 @@ module Skyward::ORM::Migrations::Diff
150
214
  ].compact
151
215
  end
152
216
 
217
+ # PostgreSQL respells a jsonb default, as '{"a": 1}' for '{"a":1}', and a
218
+ # float array's, as '{1e+20}' for '{1.0e+20}', so a JSON or Array column's
219
+ # default is compared by value.
220
+ # @rbs (Skyward::ORM::Tables::Column column, String had, String wanted, Skyward::ORM::_Adapter adapter) -> bool
221
+ def same_default?(column, had, wanted, adapter)
222
+ return true if had == wanted
223
+ return false unless BY_VALUE.include?(column.type)
224
+
225
+ decoded = value(column, had, adapter)
226
+ !decoded.nil? && decoded == value(column, wanted, adapter)
227
+ end
228
+
229
+ # The value a string literal holds as the column reads it, in an Array so
230
+ # that JSON's null isn't taken for no value, or nil when it's an expression
231
+ # or the column can't read it.
232
+ # @rbs (Skyward::ORM::Tables::Column column, String default, Skyward::ORM::_Adapter adapter) -> [untyped]?
233
+ def value(column, default, adapter)
234
+ text = default[QUOTED, 1]
235
+ [adapter.decode(column, text.gsub("''", "'"))] if text
236
+ # Other SQL than a migration's may have written a default the column can't
237
+ # read, and each type's parser raises its own error for one.
238
+ rescue JSON::ParserError, ArgumentError, TypeError
239
+ nil
240
+ end
241
+
153
242
  # Constraints are matched by name. SQLite can hold one with no name, which
154
- # matches the one its class declares on the same columns. PostgreSQL rewrites
155
- # a check's expression, and puts it on every column it mentions, so a check
156
- # is matched by its name alone. A reference is also matched by what it does
157
- # ON DELETE.
243
+ # matches the one its class declares that does what it does. PostgreSQL
244
+ # rewrites a check's expression, and puts it on every column it mentions, so
245
+ # a check is matched by its name alone.
158
246
  # @rbs had: Skyward::ORM::Catalog::Constraint
159
247
  # @rbs wanted: Skyward::ORM::Catalog::Constraint
160
248
  # @rbs return: bool
161
249
  def same?(had, wanted)
162
250
  name = had.name
163
- same_columns = had.columns == wanted.columns && had.references == wanted.references &&
164
- on_delete(had) == on_delete(wanted)
165
- had.kind == wanted.kind && (name ? name == wanted.name && (had.kind == :check || same_columns) : same_columns)
251
+ had.kind == wanted.kind &&
252
+ (name ? name == wanted.name && (had.kind == :check || alike?(had, wanted)) : alike?(had, wanted))
253
+ end
254
+
255
+ # @rbs had: Skyward::ORM::Catalog::Constraint
256
+ # @rbs wanted: Skyward::ORM::Catalog::Constraint
257
+ # @rbs return: bool
258
+ def renamed?(had, wanted) = RENAMABLE.include?(had.kind) && had.kind == wanted.kind && alike?(had, wanted)
259
+
260
+ # @rbs had: Skyward::ORM::Catalog::Constraint
261
+ # @rbs wanted: Skyward::ORM::Catalog::Constraint
262
+ # @rbs return: bool
263
+ def alike?(had, wanted)
264
+ had.columns == wanted.columns && had.references == wanted.references && on_delete(had) == on_delete(wanted)
166
265
  end
167
266
 
168
267
  # @rbs constraint: Skyward::ORM::Catalog::Constraint
@@ -177,7 +276,32 @@ module Skyward::ORM::Migrations::Diff
177
276
  # @rbs had: Skyward::ORM::Catalog::Index
178
277
  # @rbs wanted: Skyward::ORM::Catalog::Index
179
278
  # @rbs return: bool
180
- def same_index?(had, wanted) = !had.unique && had.columns == wanted.columns
279
+ def same_index?(had, wanted) = had.columns == wanted.columns
280
+
281
+ # @rbs constraint: Skyward::ORM::Catalog::Constraint
282
+ # @rbs tables: Array[Skyward::ORM::Catalog::Table]
283
+ # @rbs return: bool
284
+ def declarable?(constraint, tables)
285
+ case constraint.kind
286
+ when :primary_key, :unique then true
287
+ when :check then constraint.columns.one?
288
+ else constraint.columns.one? && to_primary_key?(constraint, tables)
289
+ end
290
+ end
291
+
292
+ # A reference that names no columns is to its table's primary key.
293
+ # @rbs constraint: Skyward::ORM::Catalog::Constraint
294
+ # @rbs tables: Array[Skyward::ORM::Catalog::Table]
295
+ # @rbs return: bool
296
+ def to_primary_key?(constraint, tables)
297
+ named = constraint.definition[REFERENCED, :columns]
298
+ return true unless named
299
+
300
+ parent = tables.find { |table| table.name == constraint.references }
301
+ key = parent&.constraints&.find { |candidate| candidate.kind == :primary_key }
302
+ names = named.scan(NAME) #: Array[String]
303
+ names.map { |name| name.delete("\"`[]").to_sym } == key&.columns
304
+ end
181
305
 
182
306
  # @rbs constraint: Skyward::ORM::Catalog::Constraint
183
307
  # @rbs added: Array[Skyward::ORM::Tables::Column]
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Skyward::ORM::Migrations
4
+ RenameConstraint = Data.define(
5
+ :from, #: String
6
+ :to #: String
7
+ )
8
+ end
@@ -14,6 +14,10 @@ module Skyward::ORM::Migrations::Runner
14
14
 
15
15
  DOWN = /^[ \t]*-- down[ \t]*$/ #: Regexp
16
16
 
17
+ RAN_AT = Skyward::ORM::Tables::Column.new(
18
+ name: :ran_at, type: :time, nullable: false, symbols: [], element: nil
19
+ ) #: Skyward::ORM::Tables::Column
20
+
17
21
  # @rbs path: String
18
22
  # @rbs return: Array[Migration]
19
23
  def read(path)
@@ -150,7 +154,7 @@ module Skyward::ORM::Migrations::Runner
150
154
  begin
151
155
  connection.execute(
152
156
  "CREATE TABLE IF NOT EXISTS #{TABLE} (version TEXT PRIMARY KEY, checksum TEXT NOT NULL, " \
153
- "ran_at #{adapter.sql_type(:time)} NOT NULL DEFAULT CURRENT_TIMESTAMP)"
157
+ "ran_at #{adapter.sql_type(RAN_AT)} NOT NULL DEFAULT CURRENT_TIMESTAMP)"
154
158
  )
155
159
  connection.execute("COMMIT")
156
160
  settled = true
@@ -159,7 +163,7 @@ module Skyward::ORM::Migrations::Runner
159
163
  end
160
164
  nil
161
165
  ensure
162
- connection.execute_batch(adapter.end_migration)
166
+ end_migration(adapter, connection)
163
167
  end
164
168
 
165
169
  # @rbs (Skyward::ORM::_Adapter adapter, Skyward::ORM::_Connection connection, Migration migration)
@@ -173,7 +177,7 @@ module Skyward::ORM::Migrations::Runner
173
177
  else
174
178
  run_locked(connection, migration, &)
175
179
  ensure
176
- connection.execute_batch(adapter.end_migration)
180
+ end_migration(adapter, connection)
177
181
  end
178
182
 
179
183
  # Rolled back in ensure rather than rescue, so a migration left by throw or a
@@ -193,6 +197,12 @@ module Skyward::ORM::Migrations::Runner
193
197
  end
194
198
  end
195
199
 
200
+ # @rbs (Skyward::ORM::_Adapter adapter, Skyward::ORM::_Connection connection) -> void
201
+ def end_migration(adapter, connection)
202
+ sql = adapter.end_migration
203
+ connection.execute_batch(sql) if sql
204
+ end
205
+
196
206
  # SQLite ends the transaction itself on some failures, such as ON CONFLICT
197
207
  # ROLLBACK, and then ROLLBACK raises, though the outcome is already decided.
198
208
  # @rbs (Skyward::ORM::_Connection connection) -> void
@@ -3,10 +3,14 @@
3
3
  module Skyward::ORM::Migrations::Script
4
4
  extend self
5
5
 
6
+ # The column types of a primary key the database numbers.
7
+ NUMBERED = %i[integer int32 int16].freeze #: Array[Skyward::ORM::Tables::column_type]
8
+
6
9
  # @rbs changes: Array[Skyward::ORM::Migrations::change]
7
10
  # @rbs adapter: Skyward::ORM::_Adapter
8
11
  # @rbs return: String
9
12
  def write(changes, adapter)
13
+ refuse_rebuilding_generated(changes, adapter)
10
14
  up = changes.map { |change| up(change, adapter) }.join("\n")
11
15
  down = changes.reverse.map { |change| down(change, adapter) }.join
12
16
  "#{up}\n-- down\n#{down}"
@@ -14,6 +18,27 @@ module Skyward::ORM::Migrations::Script
14
18
 
15
19
  private
16
20
 
21
+ # A rebuild makes the table from its class, which can't say how the database
22
+ # generates a column, and the catalog keeps only a generated column's
23
+ # expression, not whether it's STORED, so the rebuild can't carry one over.
24
+ # @rbs changes: Array[Skyward::ORM::Migrations::change]
25
+ # @rbs adapter: Skyward::ORM::_Adapter
26
+ # @rbs return: void
27
+ def refuse_rebuilding_generated(changes, adapter)
28
+ problems = changes.filter_map do |change|
29
+ case change
30
+ when Skyward::ORM::Migrations::AlterTable
31
+ generated = change.table.columns.select(&:generated).map(&:name)
32
+ next if generated.empty? || in_place?(change, adapter)
33
+
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`."
37
+ end
38
+ end
39
+ raise Skyward::Core::BootError, problems.join("\n") unless problems.empty?
40
+ end
41
+
17
42
  # @rbs change: Skyward::ORM::Migrations::change
18
43
  # @rbs adapter: Skyward::ORM::_Adapter
19
44
  # @rbs return: String
@@ -21,7 +46,7 @@ module Skyward::ORM::Migrations::Script
21
46
  case change
22
47
  when Skyward::ORM::Migrations::CreateTable
23
48
  klass = change.klass
24
- "#{create_table(klass, change.columns, adapter, klass.table_name, {})}#{indexes(klass)}"
49
+ "#{create_table(klass, change.columns, adapter, klass.table_name, {}, [])}#{indexes(klass)}"
25
50
  else
26
51
  return rebuild(change, adapter) unless in_place?(change, adapter)
27
52
 
@@ -81,6 +106,7 @@ module Skyward::ORM::Migrations::Script
81
106
  when Skyward::ORM::Migrations::ChangeDefault then default(table, edit.column, edit.to)
82
107
  when Skyward::ORM::Migrations::AddConstraint then add_constraint(table, edit.constraint)
83
108
  when Skyward::ORM::Migrations::DropConstraint then drop_constraint(table, edit.constraint)
109
+ when Skyward::ORM::Migrations::RenameConstraint then rename_constraint(table, edit.from, edit.to)
84
110
  when Skyward::ORM::Migrations::AddIndex then "#{edit.index.definition};\n"
85
111
  else "DROP INDEX #{edit.index.name};\n"
86
112
  end
@@ -101,16 +127,18 @@ module Skyward::ORM::Migrations::Script
101
127
  when Skyward::ORM::Migrations::ChangeDefault then default(table, edit.column, edit.from)
102
128
  when Skyward::ORM::Migrations::AddConstraint then drop_constraint(table, edit.constraint)
103
129
  when Skyward::ORM::Migrations::DropConstraint then add_constraint(table, edit.constraint)
130
+ when Skyward::ORM::Migrations::RenameConstraint then rename_constraint(table, edit.to, edit.from)
104
131
  when Skyward::ORM::Migrations::AddIndex then "DROP INDEX #{edit.index.name};\n"
105
132
  else "#{edit.index.definition};\n"
106
133
  end
107
134
  end
108
135
 
109
- # SQLite rebuilds the table as its class has it: create the new one, copy
110
- # the rows of each column it keeps, drop the old one, rename the new one and
111
- # recreate its indexes. Dropping a table another one references would run
112
- # that one's ON DELETE, so `skyward migrate` turns SQLite's foreign keys off
113
- # around each migration, and checks them before it commits.
136
+ # SQLite rebuilds the table as its class has it, with what it keeps that the
137
+ # class can't declare: create the new one, copy the rows of each column it
138
+ # keeps, drop the old one, rename the new one and recreate its indexes.
139
+ # Dropping a table another one references would run that one's ON DELETE,
140
+ # so `skyward migrate` turns SQLite's foreign keys off around each
141
+ # migration, and checks them before it commits.
114
142
  # @rbs change: Skyward::ORM::Migrations::AlterTable
115
143
  # @rbs adapter: Skyward::ORM::_Adapter
116
144
  # @rbs return: String
@@ -128,9 +156,10 @@ module Skyward::ORM::Migrations::Script
128
156
  end
129
157
  [
130
158
  *todos,
131
- create_table(klass, change.columns, adapter, :"#{table}_new", kept),
159
+ create_table(klass, change.columns, adapter, :"#{table}_new", kept, change.kept_constraints),
132
160
  copy(table, copied.map(&:name), copied.map { |column| kept.fetch(column.name).name }),
133
- indexes(klass)
161
+ indexes(klass),
162
+ *change.kept_indexes.map { |index| "#{index.definition};\n" }
134
163
  ].join
135
164
  end
136
165
 
@@ -152,9 +181,7 @@ module Skyward::ORM::Migrations::Script
152
181
  *had.columns.map do |column|
153
182
  "#{column.name.name.ljust(name_width)} #{column.type.ljust(type_width)} #{clauses(column)}".rstrip
154
183
  end,
155
- *had.constraints.map do |constraint|
156
- "#{"CONSTRAINT #{constraint.name} " if constraint.name}#{constraint.definition}"
157
- end
184
+ *had.constraints.map { |constraint| table_constraint(constraint) }
158
185
  ]
159
186
  [
160
187
  *restored,
@@ -187,16 +214,17 @@ module Skyward::ORM::Migrations::Script
187
214
 
188
215
  # Columns line up as a person would write them. A generated key's type runs
189
216
  # on into its identity clause, so it doesn't set the width. A rebuild keeps
190
- # the type an untyped column has.
217
+ # the type an untyped column has, and the constraints the class can't declare.
191
218
  # @rbs (singleton(Skyward::ORM::Record) klass, Array[Skyward::ORM::Tables::Column] columns,
192
- # Skyward::ORM::_Adapter adapter, Symbol name, Hash[Symbol, Skyward::ORM::Catalog::Column] kept) -> String
193
- def create_table(klass, columns, adapter, name, kept)
219
+ # Skyward::ORM::_Adapter adapter, Symbol name, Hash[Symbol, Skyward::ORM::Catalog::Column] kept,
220
+ # Array[Skyward::ORM::Catalog::Constraint] undeclared) -> String
221
+ def create_table(klass, columns, adapter, name, kept, undeclared)
194
222
  schema = klass.schema
195
223
  constraints = Skyward::ORM::Migrations::Definitions.constraints(klass, columns)
196
224
  name_width = columns.map { |column| column.name.length }.max || 0
197
225
  identity = adapter.identity
198
226
  type_width = columns.reject { |column| identity && generated?(schema, column) }
199
- .filter_map { |column| (adapter.sql_type(column.type) || kept[column.name]&.type)&.length }
227
+ .filter_map { |column| (adapter.sql_type(column) || kept[column.name]&.type)&.length }
200
228
  .max || 0
201
229
  lines = columns.map do |column|
202
230
  found = definition(klass, column, adapter, constraints, kept[column.name])
@@ -205,8 +233,9 @@ module Skyward::ORM::Migrations::Script
205
233
  spec, clauses = found
206
234
  [true, "#{column.name.name.ljust(name_width)} #{spec.ljust(type_width)} #{clauses}".rstrip]
207
235
  end #: Array[[bool, String]]
208
- table_constraints = constraints.reject { |constraint| constraint.columns.one? }.map do |constraint|
209
- [true, "CONSTRAINT #{constraint.name} #{constraint.definition}"]
236
+ multi_column = constraints.reject { |constraint| constraint.columns.one? }
237
+ table_constraints = [*multi_column, *undeclared].map do |constraint|
238
+ [true, table_constraint(constraint)]
210
239
  end #: Array[[bool, String]]
211
240
  entries = [*lines, *table_constraints]
212
241
  last = entries.rindex { |definition, _| definition }
@@ -216,6 +245,11 @@ module Skyward::ORM::Migrations::Script
216
245
  ["CREATE TABLE #{name} (\n", *body, ");\n"].join
217
246
  end
218
247
 
248
+ # SQLite can hold a constraint with no name.
249
+ # @rbs constraint: Skyward::ORM::Catalog::Constraint
250
+ # @rbs return: String
251
+ def table_constraint(constraint) = "#{"CONSTRAINT #{constraint.name} " if constraint.name}#{constraint.definition}"
252
+
219
253
  # @rbs klass: singleton(Skyward::ORM::Record)
220
254
  # @rbs return: String
221
255
  def indexes(klass)
@@ -239,13 +273,13 @@ module Skyward::ORM::Migrations::Script
239
273
  # @rbs (singleton(Skyward::ORM::Record) klass, Skyward::ORM::Tables::Column column, Skyward::ORM::_Adapter adapter,
240
274
  # Array[Skyward::ORM::Catalog::Constraint] constraints, Skyward::ORM::Catalog::Column? kept) -> [String, String]?
241
275
  def definition(klass, column, adapter, constraints, kept)
242
- typed = adapter.sql_type(column.type)
276
+ typed = adapter.sql_type(column)
243
277
  sql_type = typed || kept&.type
244
278
  return unless sql_type
245
279
 
246
280
  name = column.name
247
281
  generated = generated?(klass.schema, column)
248
- default = Skyward::ORM::Migrations::Definitions.default(klass, name, adapter)
282
+ default = Skyward::ORM::Migrations::Definitions.default(klass, column, adapter)
249
283
  clauses = [
250
284
  ("NOT NULL" unless (typed ? column.nullable : kept&.nullable) || generated),
251
285
  ("DEFAULT #{default}" if default),
@@ -258,7 +292,7 @@ module Skyward::ORM::Migrations::Script
258
292
  # @rbs schema: Skyward::ORM::Tables::Schema
259
293
  # @rbs column: Skyward::ORM::Tables::Column
260
294
  # @rbs return: bool
261
- def generated?(schema, column) = schema.primary_key == column.name && column.type == :integer
295
+ def generated?(schema, column) = schema.primary_key == column.name && NUMBERED.include?(column.type)
262
296
 
263
297
  # A column as the database has it.
264
298
  # @rbs column: Skyward::ORM::Catalog::Column
@@ -315,6 +349,12 @@ module Skyward::ORM::Migrations::Script
315
349
  # @rbs return: String
316
350
  def drop_constraint(table, constraint) = "ALTER TABLE #{table} DROP CONSTRAINT #{constraint.name};\n"
317
351
 
352
+ # @rbs table: Symbol
353
+ # @rbs from: String
354
+ # @rbs to: String
355
+ # @rbs return: String
356
+ def rename_constraint(table, from, to) = "ALTER TABLE #{table} RENAME CONSTRAINT #{from} TO #{to};\n"
357
+
318
358
  # The rows a table already has need a value for a new required column.
319
359
  # @rbs klass: singleton(Skyward::ORM::Record)
320
360
  # @rbs column: Skyward::ORM::Tables::Column