Repository navigation
ruflet_record: fix six bugs the coverage tests exposed; test the untested public API - #90
Merged
Merged
Conversation
Datetimes are stored as UTC wall-clock text and read back with Time.utc. The adapter formatted a bound Time with its own wall clock, so a local Time (Time.now off UTC, or Time.new(..., "+02:00")) was stored hours off. Column#cast also ignored the offset in a string such as "2026-01-01T10:00:00+02:00". Convert bound Times to UTC, keep cast Times as their UTC instant, and apply a string's Z or +HH:MM offset (fractional seconds are accepted).
Schema's json columns were created as plain TEXT. Assigning a Hash raised "no such bind parameter", since sqlite3 reads a Hash bind as named parameters, and an Array bind was rejected. So a json column could only hold strings the app encoded itself, and read back as strings. Declare json columns as JSON, as Rails' SQLite adapter does, so Column#type reports :json. Bind Hashes and Arrays as JSON text, and parse the column's text back into Ruby data. Without the JSON library (some embedded builds) binding a Hash or Array raises a clear ArgumentError, and reads keep the text.
Table names, foreign key targets and association classes come from the Inflector: - pluralize turned every final y into "ies", so a Survey, Day or Key model looked for "surveies", "daies" or "keies" tables, and references(:survey, foreign_key: true) pointed at "surveies"; - singularize did not undo pluralize's "es" after ss or after status, bus and alias, so has_many :addresses looked for an Addresse class. Keep a y after a vowel, and strip "es" in those cases.
belongs_to defines a writer (post.user = user), but assign_attributes sent every key to write_attribute, which knows only columns. So Post.create!(user: user) and post.update!(user: other) raised UnknownAttributeError. Use the writer for a key that is not a column but has one; other unknown keys still raise.
…defined inherited copied the parent's connection into each subclass when it was defined. A model defined before ApplicationRecord.establish_connection (models first, then the connection) therefore kept nil and raised ConnectionNotEstablished. It also kept an old connection if its parent reconnected. Look the connection up the class chain instead. A model's own establish_connection still takes precedence.
The SQLite adapter passed PRAGMA table_info's notnull flag as Column's null argument, so null? answered the opposite of the schema.
Queries, find_or_*, joins, merge, load, add_where, destroy_all, column metadata and reset, instantiate, validators, attribute accessors, update_columns, touch, schema changes, table definition types, migrations (up, change, down) and Column casting.
This was referenced Nov 17, 2026
The earlier fix declared json columns JSON and parsed every String on assignment and read. JSON gives the column NUMERIC affinity, so values that worked before changed: "007" read back as 7, "true" and true as 1, 1.0 as 1, a big number string as a Float, and an app's own JSON.generate text came back as a Hash, so JSON.parse(value) raised. Tables made before the fix stayed TEXT, so the same app also saw different types on old and new installs. Keep json columns TEXT and do not parse them: every value that worked before reads back unchanged. A Hash or Array is still bound as JSON text instead of failing (CRuby) or storing inspect text (device VM). The device VM's framework code cannot require json at load time, so the previous commit's guard made a Hash raise RecordNotSaved on device even though it worked under CRuby. The adapter now requires json the first time it binds a Hash or Array; on device the VM loads its builtin json. Where no JSON library loads (a bare mruby), the value is bound unchanged, as before.
The datetime fix also converted an assigned Time to UTC in memory, so Setting.new(at: Time.new(2026, 1, 1, 10, 0, 0, "+02:00")).at changed from 10:00 +0200 to 08:00 UTC. Binding already stores the instant as UTC, so the conversion was not needed. Keep the Time as assigned, as before. Also test a fractional-seconds string with an offset and Time#to_s text, which no test covered.
The belongs_to fix sent any key that had a public writer to that writer. That also changed keys no association defines: with attr_accessor :draft, Post.new(draft: true) set it instead of raising UnknownAttributeError, and operator names matched too: "=" and "!" called == and != and dropped the value, and "[]" raised ArgumentError. Record the names belongs_to defines, on the model or a parent, and use the writer only for those. Every other unknown key raises UnknownAttributeError, as before.
… only without one
The connection fix looked every model's connection up the class chain
on each call. That changed models that had a connection when they were
defined: they moved when their parent reconnected, kept the previous
database's cached columns ("table items has no column named extra"),
and a direct Base subclass used Base's connection instead of
RufletRecord.connection after RufletRecord.establish_connection.
Copy the parent's connection at definition again, as before. Only a
model that got none (defined before its abstract parent's
establish_connection) looks it up the chain, and Base's direct
subclasses still fall back to RufletRecord.connection.
establish_connection now also clears the cached columns of the models
that follow the class, so they read the new database's columns.
The inflector fix changed words the old rules handled correctly:
pluralize("soliloquy") became "soliloquys" (qu + y is a consonant y), and
singularize turned abuses, crevasses, impasses and posses into abus,
crevass, impass and poss.
Treat "quy" like a consonant y, keep known "sse" words (crevasse,
impasse, finesse, posse, lacrosse, largesse, demitasse, wrasse), and
strip "es" from "buses" only as a whole word, so abuses stays abuse.
…fore
The adapter JSON-encoded every Hash or Array bind, so raw
connection.execute/select_all and where("...", value) changed too. On
CRuby sqlite3 reads a Hash bind as named parameters and flattens an
Array into positional binds, and both worked before; they now bound
JSON text to the first placeholder and gave silently wrong results.
Encode at the model layer instead: the attribute values that create,
update, update_columns and update_all bind, and the values of Hash
conditions (where, where.not, find_by, IN-list items). Raw SQL binds
and SQL-string fragments go to the driver unchanged, and
bound_attributes still shows the values as given.
The "sses" -> "ss" rule broke every -sse word missing from SSE_PLURALS: mousses became mouss, so has_many :mousses looked for a Mouss class. A diff over /usr/share/dict/words found 47 such words. List the -sse words whose -ss form is not the usual word (mousse, bouillabaisse, coulisse, pelisse, noblesse, politesse, ...). Those that as a suffix would also catch -ss plurals (fosses, tasses, osses, rasses, casses: carcasses, grasses, bosses) match only as whole words. Where the -ss word is the usual one (passes, masses, crosses, princesses), it wins, as for addresses.
This was referenced Nov 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the release-readiness test pass, for
ruflet_record: six bug fixes, each with a regression test, plus tests for the public API that no test called.Behavior changes
Each item is a bug fix. Every other existing call behaves as on
main.Timeis stored as UTC. Before, it was stored by its own wall clock and read back as UTC:Time.new(2026, 1, 1, 10, 0, 0, "+02:00")reloaded as 10:00 UTC, and now reloads as 08:00 UTC. A string'sZor±HH:MMoffset is now applied when cast. It was ignored. An assignedTimeis kept as given in memory, as before.created_at/updated_atwere always UTC and are unaffected.Upgrade notes: rows that earlier versions wrote from a non-UTC
Timehold local wall-clock text and are not migrated. They read back as before, butwhere(at: that_time)no longer matches them. WithTZ=Europe/Berlin, a rowmainwrote fromTime.local(2026, 5, 1, 14)gives count 1 onmainand 0 here. An app that shows a reloaded time withoutlocaltimenow shows UTC: in Berlin,Event.create!(at: Time.local(2026, 5, 1, 14))reloads as12:00 +0000, wheremainshowed14:00 +0000.at.localtimegives14:00 +0200.update_columnsandupdate_allwrite, and Hash conditions:where(col: hash),where.not,find_by, and the Hash or Array items of awhere(col: [...])IN list. Before, CRuby read a Hash as named parameters and raisedno such bind parameter. It flattened an Array into the binds, so two or more items raisedcolumn index out of range, one item stored only that item, and an empty Array or Hash stored NULL (an update with one matched no row). The device VM storedinspecttext.Raw SQL is unchanged.
connection.execute/select_allbinds andwhere("...", value)binds reach the driver as onmain, so CRuby named parameters and Array flattening still work.bound_attributesstill shows the values as given.jsoncolumns stay TEXT and are not parsed, and strings, numbers and booleans read back exactly as onmain("007","true",true→"1",1.0→"1.0").jsonis required the first time a model binds a Hash or Array. On device that loads the VM's builtin json. With no JSON library (a bare mruby), the value is bound unchanged, as before.Upgrade notes (device): rows that
main's VM wrote from a Hash holdinspecttext, andwhere(col: same_hash)no longer matches them (packaged VM: count 1 onmain, 0 here). The VM still binds a Hash in raw SQL asinspecttext, sowhere("data = ?", hash)no longer matches a value a model wrote. BindJSON.generate(hash)instead, as the README now says.pluralizekeeps a y after a vowel:survey→surveys(wassurveies), and likewisedayandkey.singularizestrips theesafterss(addresses→address,classes→class,passes→pass) and fromstatuses,busesandaliases. These drive table names, foreign-key targets andhas_manyclasses, so such models could not work.soliloquiesandabuseskeep their old results, and so do -sse words:SSE_PLURALSand the new whole-wordSSE_WORDSlist 46 (mousses→mousse,crevasses,bouillabaisses,noblesses, …). They cover the 56 -sse words in/usr/share/dict/words, except eight. The seven inSSE_WORDSmatch only as whole words, because as suffixes they would also catch plurals such ascarcasses,grassesandbosses. Over the -s, -es and -ies plurals of every word in that dictionary, none thatmainsingularized to a dictionary word now gives a non-word. Eight change from one word to another, because the -ss word is the usual one:asses,brasses,crosses,duchesses,masses,messes,passes,princesses(main:asse,brasse,crosse,duchesse,masse,messe,passe,princesse). An app that worked around this with asurveiestable or aPasseclass must now setself.table_nameorclass_name.Post.new/create/update(user: user)uses thebelongs_towriter. It raisedUnknownAttributeError. This applies only tobelongs_tonames, on the model or a parent, that are not also columns. A column is still written directly. Any other unknown key still raises,attr_accessornames included.establish_connectionuses the parent's connection. It raisedConnectionNotEstablished, or usedRufletRecord.connectionwhen one existed.establish_connectionalso clears the cached columns of the models that follow it, so they don't keep the previous database's columns. A model that had a connection when it was defined keeps it, and Base's direct subclasses still useRufletRecord.connection, as before.Column#null?was inverted. The adapter passednotnullasnull.Since the last review (f64a726)
SQLiteAdapter#normalize_bindsto the model layer (SQL.column_value, applied bycreate_record,update_alland Hash-condition fragments when a query runs). With the review's raw-binds probe, the output now matchesmainline for line. Item 2's description ofmainwas wrong and is corrected above.SSE_PLURALSlists 39 words and the new whole-wordSSE_WORDSlists 7. Of the 47 words the review found, 39 (mousses→mousseamong them) givemain's result again. The other eight are the ones listed in item 3.execute,where("...")binds,bound_attributes, Hash conditions inwhere.not/update_all/delete_all, every listed -sse word, and a column named like abelongs_to.main(9202eb0, then a9b5847, 43f7582, 3cb9418, bc45ad6, 313a0b2, f9d79b6, 27e2ffa, ea3f7eb, f034e99 and 9f2ca79). 3cb9418 brings ruflet_record: open-ended ranges in where (18.., ..17, ...18) no longer match nothing #107 (open-ended ranges), which also editssql.rband the README, and f9d79b6 brings ruflet_record: end the transaction on return, break and throw #99 (transaction exits), which also editssqlite_adapter.rb. Both merged without conflicts and this PR's changed lines are the same.Tests
24 → 72 runs.
api_surface_test.rbcovers queries,find_or_*, joins, merge, load, schema changes, table definition types, migrations andColumncasting. 35 planted mutations (reverts of each fix, plus each part of the JSON and -sse changes) all fail a test.Verified on this head (60dc125)
ruflet_record: each test file alone on Ruby 4.0.5 and on 3.4.5, 95 runs (72 plus 10 from ruflet_record: open-ended ranges in where (18.., ..17, ...18) no longer match nothing #107 and 13 from ruflet_record: end the transaction on return, break and throw #99), 0 failures.bundle exec rake testin an exported copy gives 95 runs, 0 failures under TZ UTC, Europe/Berlin, America/New_York, Pacific/Kiritimati and Asia/Kathmandu. The lockfile is unchanged.ruby test/compat/compat.rb --jobs 4checks 314 goldens, exit 0.ruby -Ilib -Itest test/compat_goldens_test.rbgives 322 runs, 0 failures.lib/loaded over its built-in copy:test/mruby_smoke.rbpasses there and on host_vm mruby. In a probe whose app never requires json, a model stores a Hash as{"mode":"dark"}, and strings, numbers and booleans read back as onmain. Raw named-Hash and nested-Array binds give the same results asmain. On host_vm, which has no json, a Hash is bound unchanged, as onmain. This was not checked with a rebuilt VM. These VM checks and the CRuby probes below ran on 635ce9d, before ruflet_record: open-ended ranges in where (18.., ..17, ...18) no longer match nothing #107 and ruflet_record: end the transaction on return, break and throw #99. On this head the packaged VM, with this head'slib/loaded over its built-in copy, passestest/mruby_smoke.rb(which now includes the range and transaction checks of ruflet_record: open-ended ranges in where (18.., ..17, ...18) no longer match nothing #107 and ruflet_record: end the transaction on return, break and throw #99) with and without--preload.main(Hash/Array writes and queries, connections, assignment, time, inflection) differ only in items 1–6.Not done
ruflet_recordis not in CI's test loop, and no meta-test enforces record API coverage.jsoncolumns don't parse back into Ruby data. That would change what existing values read back as.