also looking at this
fix(drift): give timezone awareness its own type family
#1178
2 files
+135
-7
| @@ -55,7 +55,7 @@ export interface SchemaDrift { | ||
| 55 | 55 | * the ones that are. What it does separate is the pairs that silently lose |
| 56 | 56 | * data — text from bounded varchar, and fractional from whole. |
| 57 | 57 | */ |
| 58 | export type TypeFamily = 'text' | 'varchar' | 'integer' | 'fractional' | 'boolean' | 'date' | 'json' | 'binary' | 'other' | |
| 58 | export type TypeFamily = 'text' | 'varchar' | 'integer' | 'fractional' | 'boolean' | 'date' | 'timestamptz' | 'json' | 'binary' | 'other' | |
| 59 | 59 | |
| 60 | 60 | /** The family a model's declared type belongs to. */ |
| 61 | 61 | export function familyOfDeclared(type: NormalizedColumnType): TypeFamily { |
| @@ -76,8 +76,11 @@ export function familyOfDeclared(type: NormalizedColumnType): TypeFamily { | ||
| 76 | 76 | return 'boolean' |
| 77 | 77 | case 'date': |
| 78 | 78 | case 'datetime': |
| 79 | case 'timestamptz': | |
| 80 | 79 | 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' | |
| 81 | 84 | case 'json': |
| 82 | 85 | return 'json' |
| 83 | 86 | default: |
| @@ -85,8 +88,18 @@ export function familyOfDeclared(type: NormalizedColumnType): TypeFamily { | ||
| 85 | 88 | } |
| 86 | 89 | } |
| 87 | 90 | |
| 88 | /** The family a raw SQL type reported by a database belongs to. */ | |
| 89 | export 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 | */ | |
| 102 | export function familyOfSqlType(sqlType: string, dialect?: SupportedDialect): TypeFamily { | |
| 90 | 103 | const type = String(sqlType ?? '').toLowerCase().trim() |
| 91 | 104 | |
| 92 | 105 | // Order matters. `tinyint(1)` is MySQL's boolean and must not read as an |
| @@ -100,6 +113,22 @@ export function familyOfSqlType(sqlType: string): TypeFamily { | ||
| 100 | 113 | if (/^(?:bytea|blob|binary|varbinary)\b/.test(type)) |
| 101 | 114 | return 'binary' |
| 102 | 115 | |
| 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 | ||
| 103 | 132 | if (/^(?:date|time|timestamp|datetime)\b/.test(type)) |
| 104 | 133 | return 'date' |
| 105 | 134 | |
| @@ -141,7 +170,13 @@ export function satisfies(declared: TypeFamily, actual: TypeFamily, dialect?: Su | ||
| 141 | 170 | if (dialect === 'sqlite') { |
| 142 | 171 | if (declared === 'boolean' && actual === 'integer') |
| 143 | 172 | 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')) | |
| 145 | 180 | return true |
| 146 | 181 | } |
| 147 | 182 | |
| @@ -155,6 +190,21 @@ export function satisfies(declared: TypeFamily, actual: TypeFamily, dialect?: Su | ||
| 155 | 190 | if (isMysqlLike(dialect) && declared === 'boolean' && actual === 'integer') |
| 156 | 191 | return true |
| 157 | 192 | |
| 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 | ||
| 158 | 208 | // A bounded string fits in an unbounded one. |
| 159 | 209 | if (declared === 'varchar' && actual === 'text') |
| 160 | 210 | return true |
| @@ -212,7 +262,7 @@ export async function auditSchemaDrift( | ||
| 212 | 262 | continue |
| 213 | 263 | } |
| 214 | 264 | |
| 215 | const actual = familyOfSqlType(liveColumn.sqlType) | |
| 265 | const actual = familyOfSqlType(liveColumn.sqlType, dialect) | |
| 216 | 266 | |
| 217 | 267 | if (!satisfies(declared, actual, dialect)) { |
| 218 | 268 | typeMismatches.push({ |