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 5b5a504
Browse filesBrowse the repository at this point in the historyBrowse files
Keep the instant of MySQL Time parameters whatever their UTC offset (#59)
## Summary
**MySQL `Time` parameters no longer change their instant.** `quote`
(bound parameters) and `csv_line` (`:copyfrom`) used to write a `Time` /
`DateTime` with `strftime`, which keeps the value's own wall-clock time
and drops its UTC offset. mysql2 reads `DATETIME` / `TIMESTAMP` back as
wall-clock times in its `database_timezone`: the process zone, or UTC
for a client created with `database_timezone: :utc`. So any value with a
different offset came back as a different instant. For example,
`Time.now.utc` with `TZ=America/New_York` came back 5 hours off, and
nothing raised. Both paths now go through a new helper,
`mysql_time(client, value)`, which first converts the value to the zone
mysql2 reads in. `csv_line` now takes the client, and it formats
`DateTime` like `Time`.
This reverses the earlier
`test_time_values_match_mysql2_prepared_statement_storage`. mysql2's own
prepared statements store the wall-clock time too, so they show the same
shift. That test is replaced by:
- `test_time_values_keep_their_instant_whatever_their_offset`: runs with
`ENV['TZ'] = 'XYZ-3'` and passes +05:30, -07:00 (`DateTime`), UTC and
local values, both through a bound `:exec` and through the new
`CopyMysqlBindingTimes` `:copyfrom`.
- `test_time_values_follow_a_utc_database_timezone`: does the same with
a client created with `database_timezone: :utc`.
Both tests fail on `main` and pass with this change. The wall-clock
assertions in `test_client_side_quoting_round_trips_supported_values`
now compare instants.
Smaller fixes from the pre-release sweep:
- `timeout_exits_with_throw?` (generated for `rubyVersion` < 3.3)
compares `<=>` with `== -1` instead of calling `.negative?` on an
`Integer?`. Steep reported that line on 3.1 / 3.2 output.
- `moduleName` can no longer reopen `Kernel`, `Object` or `BasicObject`,
which would put the generated constants on every object. Covered by a
unit test.
- README documents the MySQL time zone behaviour, including that the
server also converts `TIMESTAMP` columns with the session `time_zone`.
It also notes that sqlc before 1.30 (including the pinned 1.27) reports
quoted SQLite table names with their quotes, so `"Items"` gives the
model `Items` there and `Item` from 1.30 on. Use `rename` or
`exactTableNames` to keep the name stable.
Regenerated: `examples/mysql2`, `test/fixtures/mysql2.bin` (new query),
the golden files, `steep_expectations.yml` (line numbers only) and
`docs/Coverage.md`.
Local runs: full E2E for sqlite3, pg and mysql2 on Ruby 3.1 / 3.2 / 3.3
/ 3.4 / 4.0, options E2E on 3.1 / 4.0, unit tests, `rubocop examples`,
`steep check --with-expectations` and `sqlc diff`.
## 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>
| Enums, constants (also for keyword / invalid names), database labels and MySQL `SET`|[`test_enum_column`](../tests/end2end_pg.rb#test_enum_column), [`test_enum_values_named_like_ruby_keywords_get_valid_constants`](../tests/end2end_pg.rb#test_enum_values_named_like_ruby_keywords_get_valid_constants)|[`test_enum_and_set_columns`](../tests/end2end_mysql2.rb#test_enum_and_set_columns), [`test_enum_values_named_like_ruby_keywords_get_valid_constants`](../tests/end2end_mysql2.rb#test_enum_values_named_like_ruby_keywords_get_valid_constants)| not supported by the SQLite schema |[`test_models_cover_tables_and_enums`](../test/features_test.rb#test_models_cover_tables_and_enums)|
20
20
| PostgreSQL arrays, JSON/JSONB, type round trips and RBS |[`test_array_parameters`](../tests/end2end_pg.rb#test_array_parameters), [`test_remaining_postgresql_types_round_trip`](../tests/end2end_pg.rb#test_remaining_postgresql_types_round_trip), [`test_json_and_jsonb_parameters_round_trip_all_json_value_types`](../tests/end2end_pg.rb#test_json_and_jsonb_parameters_round_trip_all_json_value_types)| not PostgreSQL types | not PostgreSQL types |[`test_multi_dimensional_arrays`](../test/features_test.rb#test_multi_dimensional_arrays), [`test_more_postgres_types_composites_and_domains`](../test/features_test.rb#test_more_postgres_types_composites_and_domains)|
21
-
| MySQL unsigned integers, BIT, YEAR, DATETIME/TIME, JSON, enums/SET and client quoting | not MySQL |[`test_client_side_quoting_round_trips_supported_values`](../tests/end2end_mysql2.rb#test_client_side_quoting_round_trips_supported_values), [`test_time_values_match_mysql2_prepared_statement_storage`](../tests/end2end_mysql2.rb#test_time_values_match_mysql2_prepared_statement_storage), [`test_declared_types_match_runtime_values`](../tests/end2end_mysql2.rb#test_declared_types_match_runtime_values)| not MySQL |[`test_mysql_uses_client_side_binding_and_sqlite_closes_prepared_statements`](../test/features_test.rb#test_mysql_uses_client_side_binding_and_sqlite_closes_prepared_statements)|
21
+
| MySQL unsigned integers, BIT, YEAR, DATETIME/TIME (a `Time` keeps its instant in any zone, also in `:copyfrom`), JSON, enums/SET and client quoting | not MySQL |[`test_client_side_quoting_round_trips_supported_values`](../tests/end2end_mysql2.rb#test_client_side_quoting_round_trips_supported_values), [`test_time_values_keep_their_instant_whatever_their_offset`](../tests/end2end_mysql2.rb#test_time_values_keep_their_instant_whatever_their_offset), [`test_time_values_follow_a_utc_database_timezone`](../tests/end2end_mysql2.rb#test_time_values_follow_a_utc_database_timezone), [`test_declared_types_match_runtime_values`](../tests/end2end_mysql2.rb#test_declared_types_match_runtime_values)| not MySQL |[`test_mysql_uses_client_side_binding_and_sqlite_closes_prepared_statements`](../test/features_test.rb#test_mysql_uses_client_side_binding_and_sqlite_closes_prepared_statements)|
22
22
| SQLite affinity, JSON/UUID, and NUMERIC integer/float results | not SQLite | not SQLite |[`test_declared_types_match_runtime_values`](../tests/end2end_sqlite3.rb#test_declared_types_match_runtime_values), [`test_declared_types_with_parameters`](../tests/end2end_sqlite3.rb#test_declared_types_with_parameters)|[`test_sqlite_declared_types_follow_type_affinity`](../test/features_test.rb#test_sqlite_declared_types_follow_type_affinity), [`test_sqlite_json_and_uuid_declarations_are_strings`](../test/features_test.rb#test_sqlite_json_and_uuid_declarations_are_strings)|
23
23
| SQLite `busy_handler_timeout=` waits for another connection without blocking other threads | not SQLite | not SQLite |[`test_busy_handler_timeout_waits_for_another_connection_without_blocking_threads`](../tests/end2end_sqlite3.rb#test_busy_handler_timeout_waits_for_another_connection_without_blocking_threads)||
load_data(client,"LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `authors` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`name`, `bio`)")iffile.pos.positive?
load_data(client,"LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`order`, `note`)")iffile.pos.positive?
load_data(client,"LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `regressions` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`id`, `order`, `note`, `payload`)")iffile.pos.positive?
load_data(client,"LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `copy_types` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`id`, `code`, @sqlc_v2, `flags`, `amount`) SET `doc` = CONVERT(@sqlc_v2 USING utf8mb4)")iffile.pos.positive?
load_data(client,"LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' IGNORE INTO TABLE `copy_types` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`id`, `code`)",strict: false)iffile.pos.positive?
load_data(client,"LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' REPLACE INTO TABLE `copy_types` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`id`, `code`)")iffile.pos.positive?
load_data(client,"LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `mysql_binding_values` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`id`, `datetime_value`, `datetime_2`)")iffile.pos.positive?
load_data(client,"LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `MixedCaseCopies` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`Id`, `SomeValue`)")iffile.pos.positive?
load_data(client,"LOAD DATA LOCAL INFILE '#{client.escape(file.path)}' INTO TABLE `MixedCaseCopies` CHARACTER SET binary FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' (`SomeValue`, `Id`)")iffile.pos.positive?
@@ -1451,7 +1469,7 @@ def quote(client, value)
1451
1469
1452
1470
value.to_s('F')
1453
1471
when ::DateTime, ::Time
1454
-
"'#{value.strftime('%Y-%m-%d %H:%M:%S.%6N')}'"
1472
+
"'#{mysql_time(client,value)}'"
1455
1473
when ::Date
1456
1474
"'#{value.strftime('%Y-%m-%d')}'"
1457
1475
when ::String
@@ -1465,6 +1483,12 @@ def quote(client, value)
1465
1483
end
1466
1484
end
1467
1485
1486
+
defmysql_time(client,value)
1487
+
time=value.is_a?(::DateTime) ? value.to_time : value
0 commit comments