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.
- checksums.yaml +4 -4
- data/README.md +200 -31
- data/lib/skyward/orm/adapters/postgresql.rb +140 -38
- data/lib/skyward/orm/adapters/sqlite/constraints.rb +18 -0
- data/lib/skyward/orm/adapters/sqlite.rb +130 -21
- data/lib/skyward/orm/adapters.rb +12 -2
- data/lib/skyward/orm/catalog/column.rb +2 -1
- data/lib/skyward/orm/cli/boot_file.rb +2 -2
- data/lib/skyward/orm/cli/migrate.rb +1 -1
- data/lib/skyward/orm/cli/migration.rb +1 -1
- data/lib/skyward/orm/cli/scaffold/source.rb +304 -0
- data/lib/skyward/orm/cli/scaffold.rb +70 -0
- data/lib/skyward/orm/cli/status.rb +5 -3
- data/lib/skyward/orm/commands.rb +12 -0
- data/lib/skyward/orm/connection/instrumented.rb +69 -0
- data/lib/skyward/orm/connection/session.rb +74 -5
- data/lib/skyward/orm/{connection/database → database}/schema_check.rb +20 -8
- data/lib/skyward/orm/{connection/database.rb → database.rb} +15 -15
- data/lib/skyward/orm/execution.rb +9 -0
- data/lib/skyward/orm/migrations/alter_table.rb +6 -2
- data/lib/skyward/orm/migrations/definitions.rb +7 -4
- data/lib/skyward/orm/migrations/diff.rb +154 -30
- data/lib/skyward/orm/migrations/rename_constraint.rb +8 -0
- data/lib/skyward/orm/migrations/runner.rb +13 -3
- data/lib/skyward/orm/migrations/script.rb +60 -20
- data/lib/skyward/orm/migrations/status.rb +32 -3
- data/lib/skyward/orm/migrations/summary.rb +3 -6
- data/lib/skyward/orm/migrations/versions.rb +1 -1
- data/lib/skyward/orm/migrations.rb +1 -1
- data/lib/skyward/orm/projection.rb +23 -4
- data/lib/skyward/orm/queries/clauses.rb +117 -32
- data/lib/skyward/orm/record.rb +23 -1
- data/lib/skyward/orm/{placeholders.rb → statements/placeholders.rb} +1 -1
- data/lib/skyward/orm/statements.rb +171 -30
- data/lib/skyward/orm/tables/column.rb +2 -1
- data/lib/skyward/orm/tables/column_types.rb +62 -15
- data/lib/skyward/orm/tables/declarations.rb +38 -10
- data/lib/skyward/orm/{members.rb → tables/members.rb} +1 -1
- data/lib/skyward/orm/tables/sources.rb +63 -0
- data/lib/skyward/orm/tables/validation.rb +12 -3
- data/lib/skyward/orm/tables/values.rb +115 -5
- data/lib/skyward/orm/tables.rb +2 -1
- data/lib/skyward/orm/transactional.rb +2 -2
- data/lib/skyward/orm/version.rb +1 -1
- data/lib/skyward/orm.rb +44 -10
- data/sig/generated/skyward/orm/adapters/postgresql.rbs +53 -14
- data/sig/generated/skyward/orm/adapters/sqlite/constraints.rbs +6 -0
- data/sig/generated/skyward/orm/adapters/sqlite.rbs +67 -7
- data/sig/generated/skyward/orm/adapters.rbs +3 -0
- data/sig/generated/skyward/orm/catalog/column.rbs +6 -4
- data/sig/generated/skyward/orm/cli/boot_file.rbs +4 -4
- data/sig/generated/skyward/orm/cli/migrate.rbs +2 -2
- data/sig/generated/skyward/orm/cli/migration.rbs +2 -2
- data/sig/generated/skyward/orm/cli/scaffold/source.rbs +147 -0
- data/sig/generated/skyward/orm/cli/scaffold.rbs +28 -0
- data/sig/generated/skyward/orm/cli/status.rbs +2 -2
- data/sig/generated/skyward/orm/connection/instrumented.rbs +45 -0
- data/sig/generated/skyward/orm/connection/session.rbs +40 -6
- data/sig/generated/skyward/orm/{connection/database → database}/schema_check.rbs +11 -4
- data/sig/generated/skyward/orm/{connection/database.rbs → database.rbs} +4 -5
- data/sig/generated/skyward/orm/execution.rbs +18 -0
- data/sig/generated/skyward/orm/migrations/alter_table.rbs +11 -5
- data/sig/generated/skyward/orm/migrations/definitions.rbs +2 -2
- data/sig/generated/skyward/orm/migrations/diff.rbs +77 -11
- data/sig/generated/skyward/orm/migrations/rename_constraint.rbs +16 -0
- data/sig/generated/skyward/orm/migrations/runner.rbs +5 -0
- data/sig/generated/skyward/orm/migrations/script.rbs +32 -8
- data/sig/generated/skyward/orm/migrations/status.rbs +15 -2
- data/sig/generated/skyward/orm/migrations/summary.rbs +0 -1
- data/sig/generated/skyward/orm/migrations/versions.rbs +2 -2
- data/sig/generated/skyward/orm/migrations.rbs +1 -1
- data/sig/generated/skyward/orm/projection.rbs +10 -2
- data/sig/generated/skyward/orm/queries/clauses.rbs +60 -24
- data/sig/generated/skyward/orm/record.rbs +8 -0
- data/sig/generated/skyward/orm/{placeholders.rbs → statements/placeholders.rbs} +2 -2
- data/sig/generated/skyward/orm/statements.rbs +83 -5
- data/sig/generated/skyward/orm/tables/column.rbs +6 -4
- data/sig/generated/skyward/orm/tables/column_types.rbs +18 -4
- data/sig/generated/skyward/orm/tables/declarations.rbs +15 -1
- data/sig/generated/skyward/orm/{members.rbs → tables/members.rbs} +2 -2
- data/sig/generated/skyward/orm/tables/sources.rbs +32 -0
- data/sig/generated/skyward/orm/tables/values.rbs +41 -1
- data/sig/generated/skyward/orm/tables.rbs +3 -1
- data/sig/generated/skyward/orm.rbs +6 -6
- data/sig/manual/pg.rbs +12 -0
- data/sig/manual/skyward/orm/cli/scaffold/source.rbs +3 -0
- data/sig/manual/skyward/orm/cli/scaffold.rbs +3 -0
- data/sig/manual/skyward/orm/database/schema_check.rbs +3 -0
- data/sig/manual/skyward/orm/statements/placeholders.rbs +3 -0
- data/sig/manual/skyward/orm/tables/members.rbs +3 -0
- data/sig/manual/skyward/orm/tables/sources.rbs +3 -0
- data/sig/manual/skyward/orm.rbs +32 -5
- metadata +43 -14
- data/sig/manual/skyward/orm/connection/database/schema_check.rbs +0 -3
- data/sig/manual/skyward/orm/members.rbs +0 -3
- data/sig/manual/skyward/orm/placeholders.rbs +0 -3
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# A table class's file, written from its table as the catalog reads it. Each
|
|
4
|
+
# type, default and constraint is one the class's migration would write the
|
|
5
|
+
# same, so `skyward migration` changes only what a comment says it will.
|
|
6
|
+
module Skyward::ORM::CLI::Scaffold::Source
|
|
7
|
+
extend self
|
|
8
|
+
|
|
9
|
+
# Only a trailing s is dropped: ies becomes y, sses becomes ss, and ss stays.
|
|
10
|
+
SINGULAR = /ies\z|sses\z|(?<!s)s\z/ #: Regexp
|
|
11
|
+
SINGULARS = { "ies" => "y", "sses" => "ss" }.freeze #: Hash[String, String]
|
|
12
|
+
|
|
13
|
+
QUOTED = /'((?:[^']|'')*)'/ #: Regexp
|
|
14
|
+
LITERAL = /\A#{QUOTED}\z/ #: Regexp
|
|
15
|
+
ISO_DATE = /\A(\d{4})-(\d{2})-(\d{2})\z/ #: Regexp
|
|
16
|
+
# A Time as SQLite's adapter decodes it, to the microsecond. PostgreSQL reads
|
|
17
|
+
# a default back with a cast, so no Time is written back the same there.
|
|
18
|
+
ISO_TIME = /\A(\d{4}-\d{2}-\d{2}) (?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d{1,6})?\z/ #: Regexp
|
|
19
|
+
|
|
20
|
+
BOOLS = { "true" => true, "false" => false, "1" => true, "0" => false }.freeze #: Hash[String, bool]
|
|
21
|
+
|
|
22
|
+
ENUM = Skyward::ORM::Tables::Column.new(
|
|
23
|
+
name: :enum, type: :enum, nullable: false, symbols: [], element: nil
|
|
24
|
+
) #: Skyward::ORM::Tables::Column
|
|
25
|
+
|
|
26
|
+
PARENTHESIZED = /\A\((.*)\)\z/m #: Regexp
|
|
27
|
+
|
|
28
|
+
CHECK = /\ACHECK\s*\((.*)\)\z/im #: Regexp
|
|
29
|
+
|
|
30
|
+
# What's left of a check that lists a column's values once its strings,
|
|
31
|
+
# casts and punctuation are gone. SQLite keeps `priority IN ('low', 'high')`,
|
|
32
|
+
# and PostgreSQL rewrites it as `priority = ANY (ARRAY['low'::text, 'high'::text])`.
|
|
33
|
+
CAST = /::[a-z ]+(?:\[\])?/ #: Regexp
|
|
34
|
+
PUNCTUATION = /[\s()\[\],"]/ #: Regexp
|
|
35
|
+
LISTS = %w[IN =ANYARRAY].freeze #: Array[String]
|
|
36
|
+
|
|
37
|
+
# A btree index on columns alone, as PostgreSQL writes one back, or as it was written on SQLite.
|
|
38
|
+
PLAIN_INDEX = /\ACREATE\s+(?:UNIQUE\s+)?INDEX\s.*?\sON\s+[^\s(]+\s*(?:USING\s+btree\s+)?\(([^()]*)\)\z/im #: Regexp
|
|
39
|
+
|
|
40
|
+
# The columns a foreign key names after the table it references, if it names any.
|
|
41
|
+
REFERENCED = /\bREFERENCES\s+(?:"(?:[^"]|"")*"|[^\s(]+)\s*(?:\(([^)]*)\))?/i #: Regexp
|
|
42
|
+
|
|
43
|
+
DROPPED = " # `skyward migration` will drop these, since a table class can't declare them:\n" #: String
|
|
44
|
+
REWRITTEN = "`skyward migration` will rewrite these defaults once, as the values below, since SQLite keeps each " \
|
|
45
|
+
"as it was written: " #: String
|
|
46
|
+
|
|
47
|
+
# @rbs (Skyward::ORM::Catalog::Table table, Array[Skyward::ORM::Catalog::Table] tables,
|
|
48
|
+
# Skyward::ORM::_Adapter adapter, Array[singleton(Skyward::ORM::Record)] classes) -> String
|
|
49
|
+
def text(table, tables, adapter, classes)
|
|
50
|
+
checks = table.constraints.select { |constraint| constraint.kind == :check }
|
|
51
|
+
unions = table.columns.to_h { |column| [column.name, union(column, checks, adapter)] }.compact
|
|
52
|
+
types = table.columns.to_h do |column|
|
|
53
|
+
[column.name, unions.key?(column.name) ? :enum : adapter.column_type(column.type)]
|
|
54
|
+
end #: Hash[Symbol, Skyward::ORM::Tables::column_type?]
|
|
55
|
+
refined = types.values.intersect?(Skyward::ORM::Tables::ColumnTypes::REFINEMENTS.values)
|
|
56
|
+
[
|
|
57
|
+
"# rbs_inline: enabled\n",
|
|
58
|
+
("# @rbs use Skyward::ORM::*\n" if refined),
|
|
59
|
+
"\nclass #{class_name(table.name)} < Skyward::ORM::Record\n",
|
|
60
|
+
" table #{table.name.inspect}\n",
|
|
61
|
+
*declarations(table, tables, adapter, classes, checks - unions.values.map(&:first), types),
|
|
62
|
+
"\n # @dynamic #{table.columns.map(&:name).join(", ")}\n",
|
|
63
|
+
*readers(table.columns, unions, types),
|
|
64
|
+
"end\n"
|
|
65
|
+
].compact.join
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# @rbs table: Symbol
|
|
69
|
+
# @rbs return: String
|
|
70
|
+
def class_name(table)
|
|
71
|
+
table.name.split("_").map(&:capitalize).join.sub(SINGULAR) { |ending| SINGULARS.fetch(ending, "") }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# @rbs table: Skyward::ORM::Catalog::Table?
|
|
75
|
+
# @rbs return: Array[Symbol]
|
|
76
|
+
def primary_key(table) = table&.constraints&.find { |constraint| constraint.kind == :primary_key }&.columns || []
|
|
77
|
+
|
|
78
|
+
private
|
|
79
|
+
|
|
80
|
+
# Each line below `table`, in the README's order, with what the class can't
|
|
81
|
+
# declare first, as a comment.
|
|
82
|
+
# @rbs (Skyward::ORM::Catalog::Table table, Array[Skyward::ORM::Catalog::Table] tables,
|
|
83
|
+
# Skyward::ORM::_Adapter adapter, Array[singleton(Skyward::ORM::Record)] classes,
|
|
84
|
+
# Array[Skyward::ORM::Catalog::Constraint] checks, Hash[Symbol, Skyward::ORM::Tables::column_type?] types)
|
|
85
|
+
# -> Array[String]
|
|
86
|
+
def declarations(table, tables, adapter, classes, checks, types)
|
|
87
|
+
key = primary_key(table).fetch(0)
|
|
88
|
+
references, unreferenced = table.constraints.select { |constraint| constraint.kind == :foreign_key }
|
|
89
|
+
.partition { |constraint| reference?(constraint, tables) }
|
|
90
|
+
column_checks, unchecked = checks.partition { |check| check.columns.one? }
|
|
91
|
+
plain, unindexed = table.indexes.partition { |index| plain?(index) }
|
|
92
|
+
unique_indexes, indexes = plain.partition(&:unique)
|
|
93
|
+
defaults = table.columns.filter_map { |column| default(column, types[column.name], adapter) }
|
|
94
|
+
declared, unread = defaults.partition { |_, value| value }
|
|
95
|
+
rewritten = declared.reject(&:last).map { |column, _| column.name }
|
|
96
|
+
undeclared = [*unread.map(&:first), *unreferenced, *unchecked, *unindexed].map do |undeclarable|
|
|
97
|
+
comment(definition(undeclarable), " # ")
|
|
98
|
+
end
|
|
99
|
+
[
|
|
100
|
+
(DROPPED unless undeclared.empty?),
|
|
101
|
+
*undeclared,
|
|
102
|
+
(" primary_key #{key.inspect}\n" unless key == :id),
|
|
103
|
+
*references.flat_map { |reference| reference(table.name, reference, classes) },
|
|
104
|
+
(comment("#{REWRITTEN}#{rewritten.join(", ")}") unless rewritten.empty?),
|
|
105
|
+
(" default #{declared.map { |column, value| "#{column.name}: #{value}" }.join(", ")}\n" unless declared.empty?),
|
|
106
|
+
*table.constraints.select { |constraint| constraint.kind == :unique }.map { |unique| columns("unique", unique) },
|
|
107
|
+
*unique_indexes.flat_map do |index|
|
|
108
|
+
[
|
|
109
|
+
comment(
|
|
110
|
+
"`skyward migration` will replace the unique index #{index.name} with a constraint on the same " \
|
|
111
|
+
"columns."
|
|
112
|
+
),
|
|
113
|
+
columns("unique", index)
|
|
114
|
+
]
|
|
115
|
+
end,
|
|
116
|
+
*column_checks.map do |check|
|
|
117
|
+
" check #{check.columns.fetch(0).inspect}, Skyward.sql(#{rule(check).inspect})\n"
|
|
118
|
+
end,
|
|
119
|
+
*indexes.map { |index| columns("index", index) }
|
|
120
|
+
].compact
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# @rbs columns: Array[Skyward::ORM::Catalog::Column]
|
|
124
|
+
# @rbs unions: Hash[Symbol, [Skyward::ORM::Catalog::Constraint, Array[Symbol]]]
|
|
125
|
+
# @rbs types: Hash[Symbol, Skyward::ORM::Tables::column_type?]
|
|
126
|
+
# @rbs return: Array[String]
|
|
127
|
+
def readers(columns, unions, types)
|
|
128
|
+
width = columns.map { |column| column.name.length }.max.to_i
|
|
129
|
+
columns.flat_map do |column|
|
|
130
|
+
name = column.name
|
|
131
|
+
column_type = types[name]
|
|
132
|
+
symbols = unions[name]&.last
|
|
133
|
+
type = if symbols then symbols.map(&:inspect).join(" | ")
|
|
134
|
+
elsif column_type then Skyward::ORM::Tables::Values::WRITTEN.fetch(column_type)
|
|
135
|
+
else "untyped"
|
|
136
|
+
end
|
|
137
|
+
rbs = if column_type.nil? || !column.nullable then type
|
|
138
|
+
elsif symbols then "(#{type})?"
|
|
139
|
+
else "#{type}?"
|
|
140
|
+
end
|
|
141
|
+
untyped = if column.type.empty? then "#{name} has no SQL type, so it's untyped."
|
|
142
|
+
else "#{name} is #{column.type}, which has no column type, so it's untyped."
|
|
143
|
+
end
|
|
144
|
+
[
|
|
145
|
+
(comment(untyped) unless column_type),
|
|
146
|
+
" attr_reader :#{name.name.ljust(width)} #: #{rbs}\n"
|
|
147
|
+
].compact
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# A text column whose check lists only strings is a symbol union, and its
|
|
152
|
+
# type makes that check.
|
|
153
|
+
# @rbs (Skyward::ORM::Catalog::Column column, Array[Skyward::ORM::Catalog::Constraint] checks,
|
|
154
|
+
# Skyward::ORM::_Adapter adapter) -> [Skyward::ORM::Catalog::Constraint, Array[Symbol]]?
|
|
155
|
+
def union(column, checks, adapter)
|
|
156
|
+
return unless adapter.column_type(column.type) == :string && adapter.compatible?(ENUM, column.type)
|
|
157
|
+
|
|
158
|
+
found = checks.find do |check|
|
|
159
|
+
rule = rule(check)
|
|
160
|
+
residue = rule.gsub(QUOTED, "").gsub(CAST, "").gsub(PUNCTUATION, "").upcase
|
|
161
|
+
check.columns == [column.name] && rule.match?(QUOTED) &&
|
|
162
|
+
LISTS.any? { |list| residue == "#{column.name.upcase}#{list}" }
|
|
163
|
+
end
|
|
164
|
+
return unless found
|
|
165
|
+
|
|
166
|
+
symbols = rule(found).scan(QUOTED).map { |(value)| value.to_s.gsub("''", "'").to_sym }
|
|
167
|
+
[found, symbols]
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# A literal the column's type reads is that value, if it's written back the
|
|
171
|
+
# same. Anything else is SQL, without the parentheses SQLite's adapter puts
|
|
172
|
+
# back, since its default_expression adds them again. The bool says whether
|
|
173
|
+
# it's written back the same. SQLite keeps a literal as it was written, even
|
|
174
|
+
# in parentheses, so one no value writes back the same, such as 1.50, is the
|
|
175
|
+
# value it reads as, which a migration rewrites it as once, or nil when it
|
|
176
|
+
# reads as none.
|
|
177
|
+
# @rbs (Skyward::ORM::Catalog::Column column, Skyward::ORM::Tables::column_type? type,
|
|
178
|
+
# Skyward::ORM::_Adapter adapter) -> [Skyward::ORM::Catalog::Column, String?, bool]?
|
|
179
|
+
def default(column, type, adapter)
|
|
180
|
+
sql = column.default
|
|
181
|
+
return if sql.nil? || sql == Skyward::ORM::Migrations::Diff::NULL
|
|
182
|
+
|
|
183
|
+
read = values(sql, type)
|
|
184
|
+
literal = read.filter_map do |value|
|
|
185
|
+
ruby(value) if Skyward::ORM::Migrations::Definitions.literal(adapter.encode(value)) == sql
|
|
186
|
+
end.first
|
|
187
|
+
return [column, literal, true] if literal
|
|
188
|
+
|
|
189
|
+
expression = [sql, sql[PARENTHESIZED, 1]].compact.find { |candidate| adapter.default_expression(candidate) == sql }
|
|
190
|
+
return [column, "Skyward.sql(#{expression.inspect})", true] if expression
|
|
191
|
+
|
|
192
|
+
[column, (ruby(read.first) unless read.empty?), false]
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
# @rbs sql: String
|
|
196
|
+
# @rbs type: Skyward::ORM::Tables::column_type?
|
|
197
|
+
# @rbs return: Array[untyped]
|
|
198
|
+
def values(sql, type)
|
|
199
|
+
string = sql[LITERAL, 1]&.gsub("''", "'")
|
|
200
|
+
# SQLite reads 010 as 10, where Ruby's Integer would read it as octal.
|
|
201
|
+
integer = Integer(sql, 10, exception: false)
|
|
202
|
+
float = Float(sql, exception: false)
|
|
203
|
+
case type
|
|
204
|
+
when :integer, :int32, :int16 then [integer]
|
|
205
|
+
when :float then [integer, float]
|
|
206
|
+
when :bool then [BOOLS[sql.downcase]]
|
|
207
|
+
when :string, :uuid then [string]
|
|
208
|
+
when :enum then [string&.to_sym]
|
|
209
|
+
when :date then [date(string)]
|
|
210
|
+
when :time then [time(string)]
|
|
211
|
+
when nil then [integer, float, string]
|
|
212
|
+
else [] #: Array[untyped]
|
|
213
|
+
end.compact
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# @rbs string: String?
|
|
217
|
+
# @rbs return: Date?
|
|
218
|
+
def date(string)
|
|
219
|
+
match = string&.match(ISO_DATE)
|
|
220
|
+
return unless match
|
|
221
|
+
|
|
222
|
+
year, month, day = match.captures.map(&:to_i) #: [Integer, Integer, Integer]
|
|
223
|
+
Date.new(year, month, day) if Date.valid_date?(year, month, day)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# @rbs string: String?
|
|
227
|
+
# @rbs return: Time?
|
|
228
|
+
def time(string)
|
|
229
|
+
Time.new(string, in: "UTC") if string && date(string[ISO_TIME, 1])
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# @rbs value: untyped
|
|
233
|
+
# @rbs return: String
|
|
234
|
+
def ruby(value)
|
|
235
|
+
case value
|
|
236
|
+
when Date then "Date.new(#{value.year}, #{value.month}, #{value.day})"
|
|
237
|
+
when Time
|
|
238
|
+
parts = [value.year, value.month, value.day, value.hour, value.min, value.sec,
|
|
239
|
+
(value.usec unless value.usec.zero?)]
|
|
240
|
+
"Time.utc(#{parts.compact.join(", ")})"
|
|
241
|
+
else value.inspect
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
# A foreign key a class declares is on one column, and refers to the
|
|
246
|
+
# primary key of the table it references.
|
|
247
|
+
# @rbs constraint: Skyward::ORM::Catalog::Constraint
|
|
248
|
+
# @rbs tables: Array[Skyward::ORM::Catalog::Table]
|
|
249
|
+
# @rbs return: bool
|
|
250
|
+
def reference?(constraint, tables)
|
|
251
|
+
key = primary_key(tables.find { |table| table.name == constraint.references })
|
|
252
|
+
listed = constraint.definition[REFERENCED, 1]&.split(",")&.map { |column| column.strip.delete('"').to_sym }
|
|
253
|
+
constraint.columns.one? && key.one? && (listed.nil? || listed == key)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# @rbs table: Symbol
|
|
257
|
+
# @rbs constraint: Skyward::ORM::Catalog::Constraint
|
|
258
|
+
# @rbs classes: Array[singleton(Skyward::ORM::Record)]
|
|
259
|
+
# @rbs return: Array[String]
|
|
260
|
+
def reference(table, constraint, classes)
|
|
261
|
+
# reference? found the table it references.
|
|
262
|
+
referenced = constraint.references #: Symbol
|
|
263
|
+
owner = classes.find { |klass| klass.table_name == referenced }
|
|
264
|
+
action = constraint.definition[Skyward::ORM::Migrations::Diff::ON_DELETE, 1].to_s.downcase.split.join("_")
|
|
265
|
+
on_delete = ", on_delete: :#{action}" unless ["", "no_action"].include?(action)
|
|
266
|
+
line = " references #{constraint.columns.fetch(0).inspect}, #{owner || class_name(referenced)}#{on_delete}\n"
|
|
267
|
+
return [line] if owner || referenced == table
|
|
268
|
+
|
|
269
|
+
[comment("#{referenced} has no class yet. Scaffold it too: skyward scaffold #{referenced}"), line]
|
|
270
|
+
end
|
|
271
|
+
|
|
272
|
+
# @rbs index: Skyward::ORM::Catalog::Index
|
|
273
|
+
# @rbs return: bool
|
|
274
|
+
def plain?(index)
|
|
275
|
+
keys = index.definition[PLAIN_INDEX, 1]
|
|
276
|
+
!keys.nil? && keys.split(",").map { |key| key.strip.delete('"') } == index.columns.map(&:name)
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# @rbs check: Skyward::ORM::Catalog::Constraint
|
|
280
|
+
# @rbs return: String
|
|
281
|
+
def rule(check) = check.definition[CHECK, 1].to_s
|
|
282
|
+
|
|
283
|
+
# @rbs declaration: String
|
|
284
|
+
# @rbs key: Skyward::ORM::Catalog::Constraint | Skyward::ORM::Catalog::Index
|
|
285
|
+
# @rbs return: String
|
|
286
|
+
def columns(declaration, key) = " #{declaration} #{key.columns.map(&:inspect).join(", ")}\n"
|
|
287
|
+
|
|
288
|
+
# Every line of the text is commented, since a definition or name the
|
|
289
|
+
# database keeps can hold a newline.
|
|
290
|
+
# @rbs text: String
|
|
291
|
+
# @rbs prefix: String
|
|
292
|
+
# @rbs return: String
|
|
293
|
+
def comment(text, prefix = " # ") = "#{text.gsub(/^/, prefix)}\n"
|
|
294
|
+
|
|
295
|
+
# @rbs undeclarable: Skyward::ORM::Catalog::Column | Skyward::ORM::Catalog::Constraint | Skyward::ORM::Catalog::Index
|
|
296
|
+
# @rbs return: String
|
|
297
|
+
def definition(undeclarable)
|
|
298
|
+
case undeclarable
|
|
299
|
+
when Skyward::ORM::Catalog::Column then "#{undeclarable.name} DEFAULT #{undeclarable.default}"
|
|
300
|
+
when Skyward::ORM::Catalog::Index then undeclarable.definition
|
|
301
|
+
else [("CONSTRAINT #{undeclarable.name}" if undeclarable.name), undeclarable.definition].compact.join(" ")
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Skyward::ORM::CLI::Scaffold
|
|
4
|
+
extend self
|
|
5
|
+
|
|
6
|
+
USAGE = <<~TEXT.chomp #: String
|
|
7
|
+
Usage: skyward scaffold TABLE
|
|
8
|
+
Prints a table class for TABLE, to save where your app keeps its classes:
|
|
9
|
+
skyward scaffold todos > app/todo.rb
|
|
10
|
+
TEXT
|
|
11
|
+
|
|
12
|
+
# A row compiles @name for each column, and has these methods of its own.
|
|
13
|
+
COLUMN = /\A[a-z_][a-z0-9_]*\z/ #: Regexp
|
|
14
|
+
TAKEN = [*Skyward::ORM::Tables::Rows::METHODS, :with, :members].freeze #: Array[Symbol]
|
|
15
|
+
|
|
16
|
+
# @rbs (Array[String] args, directory: String, out: _Writer, kernel: singleton(Kernel),
|
|
17
|
+
# session: Skyward::ORM::Connection::Session, discover: ^() -> Array[singleton(Skyward::ORM::Record)],
|
|
18
|
+
# generate: ^() -> String?) -> void
|
|
19
|
+
def run(args, directory:, out:, kernel:, session:, discover:, generate:)
|
|
20
|
+
name = args.fetch(0) if args.one?
|
|
21
|
+
return kernel.abort(USAGE) if name.nil? || name.start_with?("-")
|
|
22
|
+
|
|
23
|
+
Skyward::ORM::CLI::BootFile.boot(directory, session, kernel, generate) do |database|
|
|
24
|
+
classes = discover.call
|
|
25
|
+
tables = database.tables
|
|
26
|
+
found = find(name, tables, classes)
|
|
27
|
+
case found
|
|
28
|
+
when String then kernel.abort(found)
|
|
29
|
+
else out.write(Skyward::ORM::CLI::Scaffold::Source.text(found, tables, database.adapter, classes))
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
# @rbs name: String
|
|
37
|
+
# @rbs tables: Array[Skyward::ORM::Catalog::Table]
|
|
38
|
+
# @rbs classes: Array[singleton(Skyward::ORM::Record)]
|
|
39
|
+
# @rbs return: Skyward::ORM::Catalog::Table | String
|
|
40
|
+
def find(name, tables, classes)
|
|
41
|
+
table = tables.find { |candidate| candidate.name.name == name }
|
|
42
|
+
return refusal(table, classes) || table if table
|
|
43
|
+
|
|
44
|
+
suggestion = DidYouMean::SpellChecker.new(dictionary: tables.map { |candidate| candidate.name.name })
|
|
45
|
+
.correct(name).first
|
|
46
|
+
"skyward: there's no table #{name}.#{" Did you mean #{suggestion}?" if suggestion}"
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# @rbs table: Skyward::ORM::Catalog::Table
|
|
50
|
+
# @rbs classes: Array[singleton(Skyward::ORM::Record)]
|
|
51
|
+
# @rbs return: String?
|
|
52
|
+
def refusal(table, classes)
|
|
53
|
+
name = table.name
|
|
54
|
+
owner = classes.find { |klass| klass.table_name == name }
|
|
55
|
+
key = Skyward::ORM::CLI::Scaffold::Source.primary_key(table)
|
|
56
|
+
unreadable = table.columns.map(&:name).reject { |column| column.match?(COLUMN) && !TAKEN.include?(column) }
|
|
57
|
+
if name == Skyward::ORM::Migrations::Versions::TABLE
|
|
58
|
+
"skyward: #{name} is Skyward ORM's own table, so it needs no class."
|
|
59
|
+
elsif owner then "skyward: #{owner} already declares table :#{name}."
|
|
60
|
+
elsif !key.one?
|
|
61
|
+
"skyward: #{name} has #{key.empty? ? "no primary key" : "a primary key on (#{key.join(", ")})"}, and a table " \
|
|
62
|
+
"class has a one-column primary key. Give the table one in a `skyward migration --empty` migration, then " \
|
|
63
|
+
"scaffold it."
|
|
64
|
+
elsif !unreadable.empty?
|
|
65
|
+
"skyward: a table class reads each column as a lowercase Ruby method a row doesn't already have, so it can't " \
|
|
66
|
+
"read #{unreadable.map { |column| "#{name}.#{column}" }.join(" or ")}. Rename each in a " \
|
|
67
|
+
"`skyward migration --empty` migration, then scaffold it."
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -17,7 +17,7 @@ module Skyward::ORM::CLI::Status
|
|
|
17
17
|
|
|
18
18
|
private
|
|
19
19
|
|
|
20
|
-
# @rbs (Skyward::ORM::
|
|
20
|
+
# @rbs (Skyward::ORM::Database database, directory: String, out: _Writer, kernel: singleton(Kernel),
|
|
21
21
|
# discover: ^() -> Array[singleton(Skyward::ORM::Record)]) -> void
|
|
22
22
|
def report(database, directory:, out:, kernel:, discover:)
|
|
23
23
|
status = Skyward::ORM::Migrations::Status
|
|
@@ -28,9 +28,11 @@ module Skyward::ORM::CLI::Status
|
|
|
28
28
|
Skyward::ORM::Migrations::Runner.read(File.join(directory, Skyward::ORM::Migrations::Directory::PATH)),
|
|
29
29
|
Skyward::ORM::Migrations::Versions.checksums(database)
|
|
30
30
|
)
|
|
31
|
-
|
|
31
|
+
column_types = database.column_types
|
|
32
|
+
changes = Skyward::ORM::Migrations::Diff.changes(classes, column_types, tables, adapter, UNASKED)
|
|
33
|
+
kept = status.kept(classes, column_types, tables)
|
|
32
34
|
unowned = status.unowned(tables, classes)
|
|
33
|
-
out.write(status.text(migrations, changes, unowned, adapter))
|
|
35
|
+
out.write(status.text(migrations, changes, kept, unowned, adapter))
|
|
34
36
|
kernel.exit(1) unless status.clean?(migrations, changes, unowned)
|
|
35
37
|
end
|
|
36
38
|
end
|
data/lib/skyward/orm/commands.rb
CHANGED
|
@@ -62,3 +62,15 @@ Skyward::Core::CLI.register(
|
|
|
62
62
|
generate:
|
|
63
63
|
)
|
|
64
64
|
end
|
|
65
|
+
|
|
66
|
+
Skyward::Core::CLI.register("scaffold", "Print a table class for a table in the database") do |args|
|
|
67
|
+
Skyward::ORM::CLI::Scaffold.run(
|
|
68
|
+
args,
|
|
69
|
+
directory: Dir.pwd,
|
|
70
|
+
out: $stdout,
|
|
71
|
+
kernel: Kernel,
|
|
72
|
+
session: Skyward::ORM::Connection::SESSION,
|
|
73
|
+
discover:,
|
|
74
|
+
generate:
|
|
75
|
+
)
|
|
76
|
+
end
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class Skyward::ORM::Connection::Instrumented
|
|
4
|
+
ONCE = "Skyward.connect's instrument: must call its block once, to run the statement" #: String
|
|
5
|
+
|
|
6
|
+
# @rbs @connection: Skyward::ORM::_Connection
|
|
7
|
+
# @rbs @instrument: Skyward::ORM::_Instrument
|
|
8
|
+
# @rbs @adapter: Symbol
|
|
9
|
+
# @rbs @clock_ms: ^() -> Float
|
|
10
|
+
|
|
11
|
+
# @rbs (Skyward::ORM::_Connection connection, Skyward::ORM::_Instrument instrument, Symbol adapter,
|
|
12
|
+
# clock_ms: ^() -> Float) -> void
|
|
13
|
+
def initialize(connection, instrument, adapter, clock_ms:)
|
|
14
|
+
@connection = connection
|
|
15
|
+
@instrument = instrument
|
|
16
|
+
@adapter = adapter
|
|
17
|
+
@clock_ms = clock_ms
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @rbs sql: String
|
|
21
|
+
# @rbs binds: Array[untyped]
|
|
22
|
+
# @rbs return: Array[Array[untyped]]
|
|
23
|
+
def execute(sql, binds = []) = instrumented(sql, binds) { @connection.execute(sql, binds) }
|
|
24
|
+
|
|
25
|
+
# @rbs sql: String
|
|
26
|
+
# @rbs *binds: untyped
|
|
27
|
+
# @rbs return: Array[Array[untyped]]
|
|
28
|
+
def execute2(sql, *binds) = instrumented(sql, binds) { @connection.execute2(sql, *binds) }
|
|
29
|
+
|
|
30
|
+
# @rbs sql: String
|
|
31
|
+
# @rbs return: void
|
|
32
|
+
def execute_batch(sql)
|
|
33
|
+
instrumented(sql, []) { @connection.execute_batch(sql) }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @rbs return: Integer
|
|
37
|
+
def changes = @connection.changes
|
|
38
|
+
|
|
39
|
+
# @rbs return: bool
|
|
40
|
+
def transaction_active? = @connection.transaction_active?
|
|
41
|
+
|
|
42
|
+
# @rbs return: void
|
|
43
|
+
def close
|
|
44
|
+
@connection.close
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
# @rbs [T] (String sql, Array[untyped] binds) { () -> T } -> T
|
|
50
|
+
def instrumented(sql, binds)
|
|
51
|
+
calls = 0
|
|
52
|
+
# Steep can't name the method's T inside its body.
|
|
53
|
+
result = nil #: untyped
|
|
54
|
+
# Frozen copies, so the hook can't rewrite the SQL or the list of binds the block sends.
|
|
55
|
+
execution = Skyward::ORM::Execution.new(sql: sql.dup.freeze, binds: binds.dup.freeze, adapter: @adapter)
|
|
56
|
+
@instrument.call(execution) do
|
|
57
|
+
calls += 1
|
|
58
|
+
raise ONCE if calls > 1
|
|
59
|
+
|
|
60
|
+
# Timed around the driver's call alone, so the hook's own work isn't counted.
|
|
61
|
+
started = @clock_ms.call
|
|
62
|
+
result = yield
|
|
63
|
+
@clock_ms.call - started
|
|
64
|
+
end
|
|
65
|
+
raise ONCE if calls.zero?
|
|
66
|
+
|
|
67
|
+
result
|
|
68
|
+
end
|
|
69
|
+
end
|
|
@@ -1,28 +1,69 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class Skyward::ORM::Connection::Session
|
|
4
|
-
# @rbs @database: Skyward::ORM::
|
|
4
|
+
# @rbs @database: Skyward::ORM::Database?
|
|
5
5
|
# @rbs @check: bool
|
|
6
|
+
# @rbs @current: Symbol
|
|
7
|
+
# @rbs @holding: Symbol
|
|
6
8
|
|
|
7
9
|
# @rbs return: void
|
|
8
10
|
def initialize
|
|
9
11
|
@check = true
|
|
12
|
+
# Fiber storage takes only a Symbol key, and each session's switch is its own.
|
|
13
|
+
@current = :"skyward_orm_database_#{object_id}"
|
|
14
|
+
@holding = :"skyward_orm_holding_#{object_id}"
|
|
10
15
|
end
|
|
11
16
|
|
|
12
|
-
# @rbs database: Skyward::ORM::
|
|
13
|
-
# @rbs return: Skyward::ORM::
|
|
17
|
+
# @rbs database: Skyward::ORM::Database
|
|
18
|
+
# @rbs return: Skyward::ORM::Database
|
|
14
19
|
def connect(database)
|
|
15
20
|
@database = database
|
|
16
21
|
end
|
|
17
22
|
|
|
18
|
-
# @rbs return: Skyward::ORM::
|
|
23
|
+
# @rbs return: Skyward::ORM::Database
|
|
19
24
|
def database
|
|
20
|
-
@
|
|
25
|
+
current = Fiber[@current] #: Skyward::ORM::Database?
|
|
26
|
+
current || @database || raise(
|
|
21
27
|
Skyward::Core::BootError,
|
|
22
28
|
"Skyward ORM isn't connected. Call Skyward.connect(url) where your app boots."
|
|
23
29
|
)
|
|
24
30
|
end
|
|
25
31
|
|
|
32
|
+
# Fiber storage is copied into the fibers and threads a fiber starts, so
|
|
33
|
+
# those it starts in the block inherit the switch.
|
|
34
|
+
# @rbs [T] (Skyward::ORM::Database database) { () -> T } -> T
|
|
35
|
+
def using(database)
|
|
36
|
+
outer = Fiber[@current] #: Skyward::ORM::Database?
|
|
37
|
+
Fiber[@current] = database
|
|
38
|
+
begin
|
|
39
|
+
hold_also(database)
|
|
40
|
+
yield
|
|
41
|
+
ensure
|
|
42
|
+
Fiber[@current] = outer
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# A transaction on the current database, for a test, and on each database
|
|
47
|
+
# its fiber switches to, until #release rolls them all back. The holding is
|
|
48
|
+
# kept before the database holds, so #release finds one that took a
|
|
49
|
+
# connection and then raised.
|
|
50
|
+
# @rbs return: void
|
|
51
|
+
def hold
|
|
52
|
+
held = database
|
|
53
|
+
Thread.current[@holding] = [held]
|
|
54
|
+
held.hold
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Nothing is held when #hold raised before it could find a database.
|
|
58
|
+
# @rbs return: void
|
|
59
|
+
def release
|
|
60
|
+
holding = Thread.current[@holding] #: Array[Skyward::ORM::Database]?
|
|
61
|
+
return unless holding
|
|
62
|
+
|
|
63
|
+
Thread.current[@holding] = nil
|
|
64
|
+
release_each(holding)
|
|
65
|
+
end
|
|
66
|
+
|
|
26
67
|
# @rbs return: bool
|
|
27
68
|
def check? = @check
|
|
28
69
|
|
|
@@ -35,4 +76,32 @@ class Skyward::ORM::Connection::Session
|
|
|
35
76
|
ensure
|
|
36
77
|
@check = true
|
|
37
78
|
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
# Only the fiber that holds: the holding is fiber-local rather than in fiber
|
|
83
|
+
# storage, so a fiber or thread it starts doesn't inherit it, since nothing
|
|
84
|
+
# would release what one held.
|
|
85
|
+
# @rbs database: Skyward::ORM::Database
|
|
86
|
+
# @rbs return: void
|
|
87
|
+
def hold_also(database)
|
|
88
|
+
holding = Thread.current[@holding] #: Array[Skyward::ORM::Database]?
|
|
89
|
+
return unless holding && !holding.include?(database)
|
|
90
|
+
|
|
91
|
+
Thread.current[@holding] = [*holding, database]
|
|
92
|
+
database.hold
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Each database is released even when one before it raises.
|
|
96
|
+
# @rbs databases: Array[Skyward::ORM::Database]
|
|
97
|
+
# @rbs return: void
|
|
98
|
+
def release_each(databases)
|
|
99
|
+
return if databases.empty?
|
|
100
|
+
|
|
101
|
+
begin
|
|
102
|
+
databases.fetch(0).release
|
|
103
|
+
ensure
|
|
104
|
+
release_each(databases.drop(1))
|
|
105
|
+
end
|
|
106
|
+
end
|
|
38
107
|
end
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
module Skyward::ORM::
|
|
3
|
+
module Skyward::ORM::Database::SchemaCheck
|
|
4
4
|
extend self
|
|
5
5
|
|
|
6
6
|
# @rbs!
|
|
@@ -16,33 +16,45 @@ module Skyward::ORM::Connection::Database::SchemaCheck
|
|
|
16
16
|
name = klass.table_name
|
|
17
17
|
table = tables.find { |candidate| candidate.name == name }
|
|
18
18
|
problems = if table
|
|
19
|
-
columns.
|
|
19
|
+
undeclared = table.columns.map(&:name) - columns.map(&:name)
|
|
20
|
+
columns.flat_map { |column| column_problems(klass, table, column, undeclared, adapter) }
|
|
20
21
|
else
|
|
21
|
-
|
|
22
|
+
hint = suggest(name, tables.map(&:name))
|
|
23
|
+
[[name.to_s, "The database has no table #{name}.#{hint}", MIGRATE]] #: Array[problem]
|
|
22
24
|
end
|
|
23
25
|
report(klass, name, problems) unless problems.empty?
|
|
24
26
|
end
|
|
25
27
|
|
|
26
28
|
private
|
|
27
29
|
|
|
30
|
+
# A missing column is suggested only one the class doesn't declare, since
|
|
31
|
+
# one added with no migration yet would otherwise be pointed at another.
|
|
28
32
|
# @rbs (singleton(Skyward::ORM::Record) klass, Skyward::ORM::Catalog::Table table,
|
|
29
|
-
# Skyward::ORM::Tables::Column column, Skyward::ORM::_Adapter adapter) -> Array[problem]
|
|
30
|
-
def column_problems(klass, table, column, adapter)
|
|
33
|
+
# Skyward::ORM::Tables::Column column, Array[Symbol] undeclared, Skyward::ORM::_Adapter adapter) -> Array[problem]
|
|
34
|
+
def column_problems(klass, table, column, undeclared, adapter)
|
|
31
35
|
name = column.name
|
|
32
36
|
found = table.columns.find { |candidate| candidate.name == name }
|
|
33
|
-
return [[name.to_s, "#{table.name} has no column #{name}
|
|
37
|
+
return [[name.to_s, "#{table.name} has no column #{name}.#{suggest(name, undeclared)}", MIGRATE]] unless found
|
|
34
38
|
# An untyped column's values pass through as the driver returns them.
|
|
35
39
|
return [] if column.type == :untyped
|
|
36
40
|
|
|
37
41
|
stored = "#{table.name}.#{name}"
|
|
38
42
|
declared = "#{klass}##{name}"
|
|
39
|
-
type = "#{stored} is #{found.type}, but #{declared} needs #{adapter.sql_type(column
|
|
43
|
+
type = "#{stored} is #{found.type}, but #{declared} needs #{adapter.sql_type(column)}."
|
|
40
44
|
[
|
|
41
|
-
(type unless adapter.compatible?(column
|
|
45
|
+
(type unless adapter.compatible?(column, found.type)),
|
|
42
46
|
(nullability(stored, declared, found.nullable) unless found.nullable == column.nullable)
|
|
43
47
|
].compact.map { |problem| [name.to_s, problem, "#{MIGRATE} Or change #{declared}'s type to match."] }
|
|
44
48
|
end
|
|
45
49
|
|
|
50
|
+
# @rbs name: Symbol
|
|
51
|
+
# @rbs names: Array[Symbol]
|
|
52
|
+
# @rbs return: String
|
|
53
|
+
def suggest(name, names)
|
|
54
|
+
suggestion = Skyward::Core::Diagnostics::Suggestion.closest(name.to_s, names.map(&:to_s))
|
|
55
|
+
suggestion ? " Did you mean #{suggestion}?" : ""
|
|
56
|
+
end
|
|
57
|
+
|
|
46
58
|
# @rbs stored: String
|
|
47
59
|
# @rbs declared: String
|
|
48
60
|
# @rbs nullable: bool
|