ReviewOS

also looking at this

stacks/bun-query-builder

fix(drift): give timezone awareness its own type family

#1178
Merged glennmichael123 wants to merge fix/drift-timestamptz into main
2 files +135 -7
packages/bun-query-builder/src/actions/schema-drift.tsmodified+56-6
Changes to packages/bun-query-builder/src/actions/schema-drift.ts
@@ -55,7 +55,7 @@ export interface SchemaDrift {
5555 * the ones that are. What it does separate is the pairs that silently lose
5656 * data — text from bounded varchar, and fractional from whole.
5757 */
58export type TypeFamily = 'text' | 'varchar' | 'integer' | 'fractional' | 'boolean' | 'date' | 'json' | 'binary' | 'other'
58export type TypeFamily = 'text' | 'varchar' | 'integer' | 'fractional' | 'boolean' | 'date' | 'timestamptz' | 'json' | 'binary' | 'other'
5959
6060/** The family a model's declared type belongs to. */
6161export function familyOfDeclared(type: NormalizedColumnType): TypeFamily {
@@ -76,8 +76,11 @@ export function familyOfDeclared(type: NormalizedColumnType): TypeFamily {
7676 return 'boolean'
7777 case 'date':
7878 case 'datetime':
79 case 'timestamptz':
8079 return 'date'
80 // Awareness is its own family: a naive value written into an aware column
81 // is read back as a different instant, which no widening rule covers.
82 case 'timestamptz':
83 return 'timestamptz'
8184 case 'json':
8285 return 'json'
8386 default:
@@ -85,8 +88,18 @@ export function familyOfDeclared(type: NormalizedColumnType): TypeFamily {
8588 }
8689}
8790
88/** The family a raw SQL type reported by a database belongs to. */
89export function familyOfSqlType(sqlType: string): TypeFamily {
91/**
92 * The family a raw SQL type reported by a database belongs to.
93 *
94 * `dialect` matters only for timestamps, and only for the bare word
95 * `timestamp`, whose awareness is not a property of the spelling: Postgres
96 * `timestamp` is naive, while MySQL's `TIMESTAMP` is converted to and from the
97 * session timezone on every read and write and `DATETIME` is the naive one. So
98 * the same four letters mean opposite things, and classifying them without
99 * knowing the server gets one dialect wrong whichever way it guesses. Omitted,
100 * it reads `timestamp` as naive, which is correct everywhere but MySQL.
101 */
102export function familyOfSqlType(sqlType: string, dialect?: SupportedDialect): TypeFamily {
90103 const type = String(sqlType ?? '').toLowerCase().trim()
91104
92105 // Order matters. `tinyint(1)` is MySQL's boolean and must not read as an
@@ -100,6 +113,22 @@ export function familyOfSqlType(sqlType: string): TypeFamily {
100113 if (/^(?:bytea|blob|binary|varbinary)\b/.test(type))
101114 return 'binary'
102115
116 // Before the generic date branch, which would otherwise swallow these.
117 //
118 // Tested against the whole string rather than a leading word, matching
119 // `normalizePlanType` in introspect-db: Postgres reports an aware column as
120 // `timestamp with time zone`, and `timestamp(6) with time zone` when it
121 // carries a precision. The bare `timestamptz` spelling needs its own test
122 // because `\b` cannot match between `timestamp` and `tz`, so the generic
123 // branch misses it and it used to fall through to `other` - where the
124 // unknown-is-not-drift rule passed it silently.
125 if (/\bwith time zone\b/.test(type) || /^timestamptz\b/.test(type))
126 return 'timestamptz'
127
128 // MySQL's aware type. `datetime` is its naive one and falls through below.
129 if (isMysqlLike(dialect) && /^timestamp\b/.test(type))
130 return 'timestamptz'
131
103132 if (/^(?:date|time|timestamp|datetime)\b/.test(type))
104133 return 'date'
105134
@@ -141,7 +170,13 @@ export function satisfies(declared: TypeFamily, actual: TypeFamily, dialect?: Su
141170 if (dialect === 'sqlite') {
142171 if (declared === 'boolean' && actual === 'integer')
143172 return true
144 if ((declared === 'date' || declared === 'json') && actual === 'text')
173 if ((declared === 'date' || declared === 'timestamptz' || declared === 'json') && actual === 'text')
174 return true
175 // SQLite has no timezone-aware type and performs no session conversion, so
176 // the distinction the `timestamptz` family exists to catch cannot arise
177 // here. The driver stores every date family as TEXT; a column declared
178 // `timestamptz` by some other tool is still just text.
179 if ((declared === 'date' && actual === 'timestamptz') || (declared === 'timestamptz' && actual === 'date'))
145180 return true
146181 }
147182
@@ -155,6 +190,21 @@ export function satisfies(declared: TypeFamily, actual: TypeFamily, dialect?: Su
155190 if (isMysqlLike(dialect) && declared === 'boolean' && actual === 'integer')
156191 return true
157192
193 /*
194 * MySQL has no aware type a model can ask for: the driver maps BOTH
195 * `datetime` and `timestamptz` to `DATETIME` (drivers/mysql.ts), so a model
196 * declaring `timestamptz` gets a naive column from a correct migration. If
197 * that read as drift, every MySQL schema bqb itself created would be dirty -
198 * the same false positive as the boolean rule above.
199 *
200 * The opposite direction is NOT excused. A live MySQL `TIMESTAMP` under a
201 * naive model is session-converted on every read, and bqb never emits one,
202 * so it can only have come from elsewhere. That is real drift and the
203 * dangerous half.
204 */
205 if (isMysqlLike(dialect) && declared === 'timestamptz' && actual === 'date')
206 return true
207
158208 // A bounded string fits in an unbounded one.
159209 if (declared === 'varchar' && actual === 'text')
160210 return true
@@ -212,7 +262,7 @@ export async function auditSchemaDrift(
212262 continue
213263 }
214264
215 const actual = familyOfSqlType(liveColumn.sqlType)
265 const actual = familyOfSqlType(liveColumn.sqlType, dialect)
216266
217267 if (!satisfies(declared, actual, dialect)) {
218268 typeMismatches.push({