You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8d1fc8b
Browse filesBrowse the repository at this point in the historyBrowse files
Fix MySQL :copyfrom into mixed-case tables; ArgumentError for unknown isolation:; doc gaps (#57)
## Summary
**MySQL `:copyfrom` into a table whose name has uppercase letters
failed.** sqlc lowercases MySQL identifiers, so for ``INSERT INTO
`MixedCaseCopies` …`` the generated `LOAD DATA` named ``
`mixedcasecopies` ``. On Linux MySQL, with the default
`lower_case_table_names=0`, table names are case-sensitive, so the call
raised `Mysql2::Error: Table '…mixedcasecopies' doesn't exist`. Other
queries on the same table already worked, because they use the SQL text
as written.
* **Fix:** `:copyfrom` gets its table and column names from a new driver
hook, `copy_target`. The default in `drivers/base.rb` quotes sqlc's
names, as before. `drivers/mysql2.rb` overrides it to take the target
table (optionally `schema.table`) and the column list as the `INSERT`
writes them, quoted or unquoted. It falls back to the old behavior if
the text cannot be matched.
* **SQLite fix found by the same test:** sqlc 1.27 (the pinned version)
reports a quoted SQLite table name with its quotes, so the generated
`INSERT` named `"""MixedCaseCopies"""`. `drivers/sqlite3.rb` overrides
`copy_target` to strip the quotes from sqlc's names (`"…"`, `` `…` ``,
`[…]`) and then quote each name once.
* **Test:** `test_copyfrom_keeps_the_case_of_table_and_column_names`
uses a new `MixedCaseCopies` table in the example schemas of all three
engines. The MySQL test covers both backtick-quoted names and unquoted
names in a different column order. On `main` the MySQL test fails with
the error above, and the SQLite test fails with `no such table` when the
code is generated with sqlc 1.27.
Smaller items from the same pre-release check:
* **Unknown `isolation:` values** (`pg`, `mysql2`) now raise
`ArgumentError("unknown isolation level: …")` before a connection is
checked out. Before, they raised `KeyError` from
`ISOLATION_LEVELS.fetch`. SQLite's `mode:` already raised
`ArgumentError`. The existing isolation E2E tests now expect
`ArgumentError`; on `main` they fail with `KeyError`.
* **README:**
* Steep needs `library "timeout"` for code generated with a
`rubyVersion` below 3.3.
* `Struct` rows (`rubyVersion` below 3.2) rename more members than
`Data` rows, for example `count` → `count_`.
* SQLite's 32,766-parameter limit applies to `sqlc.slice`.
* MySQL `LOAD DATA` keeps the table and column names as written.
* `lib/sqlc_gen_ruby/code.rb`: removed a duplicated range in a character
class, which `ruby -w` warned about.
* Regenerated `examples/`, `test/golden`, `test/fixtures/*.bin` and
`steep_expectations.yml`. `docs/Coverage.md` lists the new test.
## Checklist
- [x] `examples/` is regenerated (`rake sqlc_generate_process`) and
committed
- [x] Tests added or updated (`rake test`, and `rake end2end_*` when the
generated code changes)
- [x] The generated code stays RuboCop-clean (`rake rubocop`) and
Steep-clean (`rake steep`)
- [x] Labelled (`bug`, `enhancement`, `documentation`, or `breaking`) so
the release notes are grouped correctly
---------
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Doron Eli Rachman <doron050@gmail.com>
`ON DUPLICATE KEY UPDATE`on MySQL) are generation errors; use `:batchexec` for those.
169
+
* MySQL: `LOAD DATA` names the table and columns as the `INSERT` writes them (`` `MixedCase` ``): sqlc reports MySQL
170
+
names in lowercase, but table names are case-sensitive on Linux.
169
171
* MySQL: `LOAD DATA` commits an open transaction that was started with raw SQL (`BEGIN`); call `:copyfrom` inside
170
172
`transaction`instead.
171
173
@@ -195,14 +197,17 @@ server; duplicate keys and invalid values raise `Mysql2::Error` instead of being
195
197
* SQLite follows its [type affinity rules](https://www.sqlite.org/datatype3.html#determination_of_column_affinity)
196
198
(`VARCHAR(255)` → `String`, `DECIMAL(10,5)` → `Float`, `UNSIGNED BIG INT` → `Integer`); `JSON` and `UUID` columns are `String`s,
197
199
also when SQLite stored numeric-looking text as a number (`'42'` → `"42"`, in SQLite's spelling: `'1.50'`→ `"1.5"`).
198
-
The generated SQLite code uses `Monitor`: if you run Steep, add `library "monitor"` to your `Steepfile`.
200
+
The generated SQLite code uses `Monitor`: if you run Steep, add `library "monitor"` to your `Steepfile`. Code
201
+
generated for a `rubyVersion` below 3.3 also uses `Timeout` (see [Transactions](#transactions)): add `library "timeout"` too.
199
202
200
203
### Names
201
204
* Query methods and keyword arguments are snake_cased, with acronyms kept together (`MIN_ID` → `min_id`,
202
205
`HTTPServer`→ `http_server`); Ruby keywords get a trailing `_` (`end_:`). Row members use the column spelling
203
206
reported by sqlc for the engine, except names that conflict with row methods, including the private ones `Data` / `Struct` call while building and copying rows (`class_`, `hash_`, `initialize_`):
204
207
PostgreSQL and SQLite keep `"camelCase"` as `:camelCase`, while MySQL reports it as `:camelcase`. A column named
205
-
`end`is the row member `:end` but the argument `end_:`.
208
+
`end`is the row member `:end` but the argument `end_:`. `Struct` rows (`rubyVersion` below 3.2) have more methods
209
+
than `Data` rows, so more names get the `_`: a column `count` is `:count` from 3.2 but `:count_` below
210
+
(`Struct#count` would otherwise be `Enumerable#count`); check the generated `.rbs` when changing `rubyVersion`.
206
211
* A parameter used twice is one keyword when named with `sqlc.arg`; repeated `?` / `$n` of one column become `id:`, `id_2:`.
207
212
* Models may be named like Ruby or driver constants (tables `exceptions`, `data`, `threads`, `sets`, ...): the
208
213
generated code and `.rbs` refer to those as `::Exception`, `::Data`, `::Thread`, ... so the models cannot shadow them.
@@ -212,6 +217,9 @@ server; duplicate keys and invalid values raise `Mysql2::Error` instead of being
212
217
### Limitations
213
218
* Two result columns with the same name (e.g. `a.id, b.id`) are an error: alias one (`b.id AS book_id`) or use `sqlc.embed`.
214
219
* SQLite's `NUMERIC` affinity can return an `Integer` where `Float` is declared.
220
+
* SQLite: a statement takes at most 32,766 parameters (`SQLITE_MAX_VARIABLE_NUMBER`), so a `sqlc.slice` with more
221
+
elements raises `SQLite3::SQLException: too many SQL variables`; on MySQL the limit is the server's
222
+
`max_allowed_packet`. Load larger sets into a table (`:copyfrom`) and join it instead.
215
223
* `:batchexec` / `:batchone` / `:batchmany` are not atomic on their own: wrap them in `transaction` for all-or-nothing.
216
224
* SQLite: sqlc does not see parameters in `ON CONFLICT ... DO UPDATE SET` (it leaves `sqlc.arg(...)` in the SQL or
217
225
reports fewer parameters than there are `?`), so such queries are generation errors. Use `excluded.<column>`
@@ -43,6 +43,7 @@ CI runs `test/coverage_map_test.rb`, which checks every linked test method exist
43
43
| Interrupts at `BEGIN` / `COMMIT` (`Thread#raise`, raised by the statement) roll back and leave no open transaction |[`test_interrupts_around_begin_and_commit_never_leave_the_connection_in_a_transaction`](../tests/end2end_pg.rb#test_interrupts_around_begin_and_commit_never_leave_the_connection_in_a_transaction)|[`test_interrupts_around_begin_and_commit_never_leave_the_connection_in_a_transaction`](../tests/end2end_mysql2.rb#test_interrupts_around_begin_and_commit_never_leave_the_connection_in_a_transaction)|[`test_interrupts_around_begin_and_commit_never_leave_the_database_in_a_transaction`](../tests/end2end_sqlite3.rb#test_interrupts_around_begin_and_commit_never_leave_the_database_in_a_transaction)||
44
44
| PostgreSQL statements dropped by `DEALLOCATE ALL` / `DISCARD ALL` are prepared again |[`test_statements_are_prepared_again_after_deallocate_all`](../tests/end2end_pg.rb#test_statements_are_prepared_again_after_deallocate_all)| not MySQL (no server-side prepared statements) | not SQLite |[`test_statements_are_named_after_the_module_and_query_class`](../test/features_test.rb#test_statements_are_named_after_the_module_and_query_class)|
45
45
|`:execresult` with `RETURNING` runs the statement | covered by `:execresult` semantics | covered by `:execresult` semantics |[`test_execresult_with_returning_runs_the_statement`](../tests/end2end_sqlite3.rb#test_execresult_with_returning_runs_the_statement)||
46
+
|`:copyfrom` keeps the case of quoted / mixed-case table and column names (MySQL `LOAD DATA`) |[`test_copyfrom_keeps_the_case_of_table_and_column_names`](../tests/end2end_pg.rb#test_copyfrom_keeps_the_case_of_table_and_column_names)|[`test_copyfrom_keeps_the_case_of_table_and_column_names`](../tests/end2end_mysql2.rb#test_copyfrom_keeps_the_case_of_table_and_column_names)|[`test_copyfrom_keeps_the_case_of_table_and_column_names`](../tests/end2end_sqlite3.rb#test_copyfrom_keeps_the_case_of_table_and_column_names)||
46
47
|`:copyfrom` takes any `Enumerable` (empty, lazy, `Enumerator`) |[`test_copyfrom_takes_any_enumerable`](../tests/end2end_pg.rb#test_copyfrom_takes_any_enumerable)|[`test_copyfrom_takes_any_enumerable`](../tests/end2end_mysql2.rb#test_copyfrom_takes_any_enumerable)|[`test_copyfrom_takes_any_enumerable`](../tests/end2end_sqlite3.rb#test_copyfrom_takes_any_enumerable)||
47
48
| Query names like `HasAuthors` / `IsActive` keep their name and stay RuboCop-clean |[`test_predicate_like_query_names_are_kept`](../tests/end2end_pg.rb#test_predicate_like_query_names_are_kept)|[`test_predicate_like_query_names_are_kept`](../tests/end2end_mysql2.rb#test_predicate_like_query_names_are_kept)|[`test_predicate_like_query_names_are_kept`](../tests/end2end_sqlite3.rb#test_predicate_like_query_names_are_kept)|[`test_predicate_like_query_names_are_rubocop_clean`](../test/features_test.rb#test_predicate_like_query_names_are_rubocop_clean)|
48
49
| SQLite `JSON` / `UUID` columns are `String`s, also numeric-looking text | not PostgreSQL | not MySQL |[`test_json_and_uuid_columns_return_strings_that_look_numeric`](../tests/end2end_sqlite3.rb#test_json_and_uuid_columns_return_strings_that_look_numeric)||
0 commit comments