/
postgres.rs
651 lines (592 loc) Β· 26.9 KB
/
postgres.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
use super::SqlSchemaDifferFlavour;
use crate::{
database_schema::SqlDatabaseSchema,
flavour::PostgresFlavour,
pair::Pair,
sql_migration::{AlterEnum, SequenceChange, SequenceChanges, SqlMigrationStep},
sql_schema_differ::{column::ColumnTypeChange, differ_database::DifferDatabase},
};
use enumflags2::BitFlags;
use native_types::{CockroachType, PostgresType};
use once_cell::sync::Lazy;
use regex::RegexSet;
use sql_schema_describer::{
postgres::PostgresSchemaExt,
walkers::{ColumnWalker, IndexWalker},
};
/// These can be tables or views, depending on the PostGIS version. In both cases, they should be ignored.
static POSTGIS_TABLES_OR_VIEWS: Lazy<RegexSet> = Lazy::new(|| {
RegexSet::new(&[
// PostGIS. Reference: https://postgis.net/docs/manual-1.4/ch04.html#id418599
"(?i)^spatial_ref_sys$",
"(?i)^geometry_columns$",
"(?i)^geography_columns$",
"(?i)^raster_columns$",
"(?i)^raster_overviews$",
])
.unwrap()
});
// https://www.postgresql.org/docs/12/pgbuffercache.html
static EXTENSION_VIEWS: Lazy<RegexSet> = Lazy::new(|| RegexSet::new(&["(?i)^pg_buffercache$"]).unwrap());
impl SqlSchemaDifferFlavour for PostgresFlavour {
fn can_alter_primary_keys(&self) -> bool {
self.is_cockroachdb()
}
fn can_rename_foreign_key(&self) -> bool {
true
}
fn column_autoincrement_changed(&self, columns: Pair<ColumnWalker<'_>>) -> bool {
if self.is_cockroachdb() {
return false;
}
columns.previous.is_autoincrement() != columns.next.is_autoincrement()
}
fn column_type_change(&self, columns: Pair<ColumnWalker<'_>>) -> Option<ColumnTypeChange> {
// Handle the enum cases first.
match columns.map(|col| col.column_type_family().as_enum()).into_tuple() {
(Some(previous_enum), Some(next_enum)) if previous_enum == next_enum => return None,
(Some(_), Some(_)) => return Some(ColumnTypeChange::NotCastable),
(None, Some(_)) | (Some(_), None) => return Some(ColumnTypeChange::NotCastable),
(None, None) => (),
};
if self.is_cockroachdb() {
cockroach_column_type_change(columns)
} else {
postgres_column_type_change(columns)
}
}
fn push_enum_steps(&self, steps: &mut Vec<SqlMigrationStep>, db: &DifferDatabase<'_>) {
for enum_differ in db.enum_pairs() {
let mut alter_enum = AlterEnum {
id: enum_differ.enums.map(|e| e.id),
created_variants: enum_differ.created_values().map(String::from).collect(),
dropped_variants: enum_differ.dropped_values().map(String::from).collect(),
previous_usages_as_default: Vec::new(), // this gets filled in later
};
if alter_enum.is_empty() {
continue;
}
push_alter_enum_previous_usages_as_default(db, &mut alter_enum);
steps.push(SqlMigrationStep::AlterEnum(alter_enum));
}
for enm in db.created_enums() {
steps.push(SqlMigrationStep::CreateEnum(enm.id))
}
for enm in db.dropped_enums() {
steps.push(SqlMigrationStep::DropEnum(enm.id))
}
}
fn push_alter_sequence_steps(&self, steps: &mut Vec<SqlMigrationStep>, db: &DifferDatabase<'_>) {
if !self.is_cockroachdb() {
return;
}
let schemas: Pair<(&SqlDatabaseSchema, &PostgresSchemaExt)> = db
.schemas()
.map(|schema| (schema, schema.describer_schema.downcast_connector_data()));
let sequence_pairs = db
.all_column_pairs()
.map(|cols| {
schemas
.zip(cols)
.map(|((schema, ext), column_id)| (schema.walk(column_id), ext))
})
.filter_map(|cols| {
cols.map(|(col, ext)| {
col.default()
.and_then(|d| d.as_sequence())
.and_then(|sequence_name| ext.get_sequence(sequence_name))
})
.transpose()
});
for pair in sequence_pairs {
let prev = pair.previous.1;
let next = pair.next.1;
let mut changes: BitFlags<SequenceChange> = BitFlags::default();
if prev.min_value != next.min_value {
changes |= SequenceChange::MinValue;
}
if prev.max_value != next.max_value {
changes |= SequenceChange::MaxValue;
}
if prev.start_value != next.start_value {
changes |= SequenceChange::Start;
}
if prev.cache_size != next.cache_size {
changes |= SequenceChange::Cache;
}
if prev.increment_by != next.increment_by {
changes |= SequenceChange::Increment;
}
if !changes.is_empty() {
steps.push(SqlMigrationStep::AlterSequence(
pair.map(|p| p.0 as u32),
SequenceChanges(changes),
));
}
}
}
fn indexes_match(&self, a: IndexWalker<'_>, b: IndexWalker<'_>) -> bool {
let columns_previous = a.columns();
let columns_next = b.columns();
let pg_ext_previous: &PostgresSchemaExt = a.schema.downcast_connector_data();
let pg_ext_next: &PostgresSchemaExt = b.schema.downcast_connector_data();
let previous_algo = pg_ext_previous.index_algorithm(a.id);
let next_algo = pg_ext_next.index_algorithm(b.id);
columns_previous.len() == columns_next.len()
&& previous_algo == next_algo
&& columns_previous.zip(columns_next).all(|(col_a, col_b)| {
let a_class = pg_ext_previous.get_opclass(col_a.id);
let b_class = pg_ext_next.get_opclass(col_b.id);
let a_kind = a_class.map(|c| &c.kind);
let b_kind = b_class.map(|c| &c.kind);
let a_is_default = a_class.map(|c| c.is_default).unwrap_or(false);
let b_is_default = b_class.map(|c| c.is_default).unwrap_or(false);
// the dml doesn't always have opclass defined if it's the
// default.
a_kind == b_kind || (a_class.is_none() && b_is_default) || (b_class.is_none() && a_is_default)
})
}
fn indexes_should_be_recreated_after_column_drop(&self) -> bool {
true
}
fn index_should_be_renamed(&self, pair: Pair<IndexWalker<'_>>) -> bool {
// Implements correct comparison for truncated index names.
let (previous_name, next_name) = pair.map(|idx| idx.name()).into_tuple();
previous_name != next_name
}
fn set_tables_to_redefine(&self, db: &mut DifferDatabase<'_>) {
if !self.is_cockroachdb() {
return;
}
let id_gets_dropped = db
.table_pairs()
.filter(|tables| {
tables.column_pairs().any(|columns| {
let type_change = self.column_type_change(columns);
let is_id = columns.previous.is_single_primary_key();
is_id && matches!(type_change, Some(ColumnTypeChange::NotCastable))
}) || tables.dropped_columns().any(|col| col.is_single_primary_key())
})
.map(|t| t.table_ids());
db.tables_to_redefine = id_gets_dropped.collect();
}
fn string_matches_bytes(&self, string: &str, bytes: &[u8]) -> bool {
if !string.starts_with("\\x") || string.len() - 2 != bytes.len() * 2 || !string.is_ascii() {
return false;
}
let string = &string[2..];
bytes.iter().enumerate().all(|(idx, byte)| {
let chars = &string[idx * 2..idx * 2 + 2];
if let Ok(byte_from_string) = u8::from_str_radix(chars, 16) {
byte_from_string == *byte
} else {
false
}
})
}
fn table_should_be_ignored(&self, table_name: &str) -> bool {
POSTGIS_TABLES_OR_VIEWS.is_match(table_name)
}
fn view_should_be_ignored(&self, view_name: &str) -> bool {
POSTGIS_TABLES_OR_VIEWS.is_match(view_name) || EXTENSION_VIEWS.is_match(view_name)
}
}
fn cockroach_column_type_change(columns: Pair<ColumnWalker<'_>>) -> Option<ColumnTypeChange> {
use ColumnTypeChange::*;
let previous_type: Option<CockroachType> = columns.previous.column_native_type();
let next_type: Option<CockroachType> = columns.next.column_native_type();
let from_list_to_scalar = columns.previous.arity().is_list() && !columns.next.arity().is_list();
let from_scalar_to_list = !columns.previous.arity().is_list() && columns.next.arity().is_list();
match (previous_type, next_type) {
(_, Some(CockroachType::String(None))) if from_list_to_scalar => Some(SafeCast),
(_, Some(CockroachType::String(_))) if from_list_to_scalar => Some(RiskyCast),
(_, Some(CockroachType::Char(_))) if from_list_to_scalar => Some(RiskyCast),
(_, _) if from_scalar_to_list || from_list_to_scalar => Some(NotCastable),
(Some(previous), Some(next)) => cockroach_native_type_change_riskyness(previous, next, columns),
// Unsupported types will have None as Native type
(None, Some(_)) => Some(RiskyCast),
(Some(_), None) => Some(RiskyCast),
(None, None)
if columns.previous.column_type().full_data_type == columns.previous.column_type().full_data_type =>
{
None
}
(None, None) => Some(RiskyCast),
}
}
// https://go.crdb.dev/issue-v/49329/v22.1
fn cockroach_native_type_change_riskyness(
previous: CockroachType,
next: CockroachType,
columns: Pair<ColumnWalker<'_>>,
) -> Option<ColumnTypeChange> {
let covered_by_index = columns
.map(|col| col.is_part_of_secondary_index() || col.is_part_of_primary_key())
.into_tuple()
== (true, true);
match (previous, next) {
(CockroachType::Int4, CockroachType::String(None)) if !covered_by_index => Some(ColumnTypeChange::SafeCast),
(CockroachType::Int8, CockroachType::Int4) if !covered_by_index => Some(ColumnTypeChange::RiskyCast),
(previous, next) if previous == next => None,
// Timestamp default precisions
(CockroachType::Time(None), CockroachType::Time(Some(6)))
| (CockroachType::Time(Some(6)), CockroachType::Time(None))
| (CockroachType::Timetz(None), CockroachType::Timetz(Some(6)))
| (CockroachType::Timetz(Some(6)), CockroachType::Timetz(None))
| (CockroachType::Timestamptz(None), CockroachType::Timestamptz(Some(6)))
| (CockroachType::Timestamptz(Some(6)), CockroachType::Timestamptz(None))
| (CockroachType::Timestamp(None), CockroachType::Timestamp(Some(6)))
| (CockroachType::Timestamp(Some(6)), CockroachType::Timestamp(None)) => None,
_ => Some(ColumnTypeChange::NotCastable),
}
}
fn postgres_column_type_change(columns: Pair<ColumnWalker<'_>>) -> Option<ColumnTypeChange> {
use ColumnTypeChange::*;
let previous_type: Option<PostgresType> = columns.previous.column_native_type();
let next_type: Option<PostgresType> = columns.next.column_native_type();
let from_list_to_scalar = columns.previous.arity().is_list() && !columns.next.arity().is_list();
let from_scalar_to_list = !columns.previous.arity().is_list() && columns.next.arity().is_list();
match (previous_type, next_type) {
(_, Some(PostgresType::Text)) if from_list_to_scalar => Some(SafeCast),
(_, Some(PostgresType::VarChar(None))) if from_list_to_scalar => Some(SafeCast),
(_, Some(PostgresType::VarChar(_))) if from_list_to_scalar => Some(RiskyCast),
(_, Some(PostgresType::Char(_))) if from_list_to_scalar => Some(RiskyCast),
(_, _) if from_scalar_to_list || from_list_to_scalar => Some(NotCastable),
(Some(previous), Some(next)) => postgres_native_type_change_riskyness(previous, next),
// Unsupported types will have None as Native type
(None, Some(_)) => Some(RiskyCast),
(Some(_), None) => Some(RiskyCast),
(None, None)
if columns.previous.column_type().full_data_type == columns.previous.column_type().full_data_type =>
{
None
}
(None, None) => Some(RiskyCast),
}
}
fn postgres_native_type_change_riskyness(previous: PostgresType, next: PostgresType) -> Option<ColumnTypeChange> {
use native_types::PostgresType::*;
use ColumnTypeChange::*;
// varchar / varbit without param=> unlimited length
// char / bit without param => length is 1
let next_is_char = || matches!(next, Char(_));
let cast = || {
Some(match previous {
PostgresType::Inet => match next {
PostgresType::Citext | PostgresType::Text | PostgresType::VarChar(_) => ColumnTypeChange::SafeCast,
_ => NotCastable,
},
PostgresType::Money => match next {
PostgresType::Citext | PostgresType::Text | PostgresType::VarChar(_) => ColumnTypeChange::SafeCast,
PostgresType::Decimal(_) => ColumnTypeChange::RiskyCast,
_ => RiskyCast,
},
PostgresType::Citext => match next {
PostgresType::Text => SafeCast,
PostgresType::VarChar(_) => SafeCast,
_ => RiskyCast,
},
PostgresType::Oid => match next {
PostgresType::Text => SafeCast,
PostgresType::VarChar(_) => SafeCast,
PostgresType::BigInt | PostgresType::Integer => SafeCast,
_ => NotCastable,
},
SmallInt => match next {
Integer => SafeCast,
BigInt => SafeCast,
Decimal(params) => match params {
// SmallInt can be at most three digits, so this might fail.
Some((p, s)) if p - s < 3 => RiskyCast,
_ => SafeCast,
},
Real => SafeCast,
DoublePrecision => SafeCast,
VarChar(param) | Char(param) => match param {
// Smallint can have three digits and an optional sign.
Some(len) if len < 4 => RiskyCast,
None if next_is_char() => RiskyCast,
_ => SafeCast,
},
Text => SafeCast,
_ => NotCastable,
},
Integer => match next {
SmallInt => RiskyCast,
BigInt => SafeCast,
Decimal(params) => match params {
// Integer can be at most 10 digits, so this might fail.
Some((p, s)) if p - s < 10 => RiskyCast,
_ => SafeCast,
},
Real => SafeCast,
DoublePrecision => SafeCast,
VarChar(param) | Char(param) => match param {
// Integer can have five digits and an optional sign.
Some(len) if len < 11 => RiskyCast,
None if next_is_char() => RiskyCast,
_ => SafeCast,
},
Text => SafeCast,
_ => NotCastable,
},
BigInt => match next {
SmallInt => RiskyCast,
Integer => RiskyCast,
Decimal(params) => match params {
// Bigint can be at most nineteen digits, so this might fail.
Some((p, s)) if p - s < 19 => RiskyCast,
_ => SafeCast,
},
Real => SafeCast,
DoublePrecision => SafeCast,
VarChar(param) | Char(param) => match param {
// Bigint can have twenty digits and an optional sign.
Some(len) if len < 20 => RiskyCast,
None if next_is_char() => RiskyCast,
_ => SafeCast,
},
Text => SafeCast,
_ => NotCastable,
},
Decimal(old_params) => match next {
SmallInt => match old_params {
None => RiskyCast,
Some((_, s)) if s > 0 => RiskyCast,
Some((p, 0)) if p > 2 => RiskyCast,
_ => SafeCast,
},
Integer => match old_params {
None => RiskyCast,
Some((_, s)) if s > 0 => RiskyCast,
Some((p, 0)) if p > 9 => RiskyCast,
_ => SafeCast,
},
BigInt => match old_params {
None => RiskyCast,
Some((_, s)) if s > 0 => RiskyCast,
Some((p, 0)) if p > 18 => RiskyCast,
_ => SafeCast,
},
Decimal(new_params) => match (old_params, new_params) {
(Some(_), None) => SafeCast,
(None, Some((p_new, s_new))) if p_new < 131072 || s_new < 16383 => RiskyCast,
// Sigh... So, numeric(4,0) to numeric(4,2) would be risky,
// so would numeric(4,2) to numeric(4,0).
(Some((p_old, s_old)), Some((p_new, s_new))) if p_old - s_old > p_new - s_new || s_old > s_new => {
RiskyCast
}
_ => SafeCast,
},
//todo this is the same as in mssql, but could be more finegrained
Real => RiskyCast,
DoublePrecision => RiskyCast,
VarChar(length) | Char(length) => match (length, old_params) {
// We must fit p digits and a possible sign to our
// string, otherwise might truncate.
(Some(len), Some((p, 0))) if p + 1 > len => RiskyCast,
// We must fit p digits, a possible sign and a comma to
// our string, otherwise might truncate.
(Some(len), Some((p, n))) if n > 0 && p + 2 > len => RiskyCast,
//up to 131072 digits before the decimal point; up to 16383 digits after the decimal point
(Some(len), None) if len < 131073 => RiskyCast,
(None, _) if next_is_char() => RiskyCast,
(None, _) => SafeCast,
_ => SafeCast,
},
Text => SafeCast,
_ => NotCastable,
},
Real => match next {
SmallInt => RiskyCast,
Integer => RiskyCast,
BigInt => RiskyCast,
Decimal(_) => RiskyCast,
Real => SafeCast,
DoublePrecision => SafeCast,
VarChar(len) | Char(len) => match len {
// If float, we can have 47 characters including the sign and comma.
Some(len) if len < 47 => RiskyCast,
None if next_is_char() => RiskyCast,
_ => SafeCast,
},
Text => SafeCast,
_ => NotCastable,
},
DoublePrecision => match next {
SmallInt => RiskyCast,
Integer => RiskyCast,
BigInt => RiskyCast,
Decimal(_) => RiskyCast,
Real => RiskyCast,
DoublePrecision => SafeCast,
VarChar(len) | Char(len) => match len {
// If double, we can have 317 characters including the sign and comma.
Some(len) if len < 317 => RiskyCast,
None if next_is_char() => RiskyCast,
_ => SafeCast,
},
Text => SafeCast,
_ => NotCastable,
},
VarChar(old_param) => match next {
Text => SafeCast,
VarChar(new_param) | Char(new_param) => match (old_param, new_param) {
(None, None) if next_is_char() => RiskyCast,
(None, Some(_)) => RiskyCast,
(Some(1), None) => SafeCast,
(Some(_), None) if next_is_char() => RiskyCast,
(Some(_), None) => SafeCast,
(Some(old_length), Some(new_length)) if old_length > new_length => RiskyCast,
_ => SafeCast,
},
_ => NotCastable,
},
Char(old_param) => match next {
Text => SafeCast,
VarChar(new_param) | Char(new_param) => match (old_param, new_param) {
(None, _) => SafeCast,
(Some(1), None) => SafeCast,
(Some(_), None) if next_is_char() => RiskyCast,
(Some(_), None) => SafeCast,
(Some(old_length), Some(new_length)) if old_length > new_length => RiskyCast,
_ => SafeCast,
},
_ => NotCastable,
},
Text => match next {
Text | VarChar(None) | Citext => SafeCast,
VarChar(_) | Char(_) => RiskyCast,
_ => NotCastable,
},
ByteA => match next {
Text | VarChar(None) => SafeCast,
VarChar(Some(length)) | Char(Some(length)) if length > 2 => RiskyCast,
_ => NotCastable,
},
Timestamp(a) => match next {
Text | VarChar(None) => SafeCast,
Char(Some(len)) | VarChar(Some(len)) if len > 22 => SafeCast,
PostgresType::Timestamp(None) => return None,
PostgresType::Timestamp(Some(b)) if a.is_none() || a == Some(b) => return None,
Timestamp(_) | Timestamptz(_) | Date | Time(_) | Timetz(_) => SafeCast,
_ => NotCastable,
},
Timestamptz(a) => match next {
Text | VarChar(None) => SafeCast,
Char(Some(len)) | VarChar(Some(len)) if len > 27 => SafeCast,
PostgresType::Timestamptz(None) => return None,
PostgresType::Timestamptz(Some(b)) if a.is_none() || a == Some(b) => return None,
Timestamp(_) | Timestamptz(_) | Date | Time(_) | Timetz(_) => SafeCast,
_ => NotCastable,
},
Date => match next {
Text | VarChar(None) => SafeCast,
Char(Some(len)) | VarChar(Some(len)) if len > 27 => SafeCast,
Timestamp(_) | Timestamptz(_) => SafeCast,
_ => NotCastable,
},
Time(a) => match next {
Text | VarChar(None) => SafeCast,
Char(Some(len)) | VarChar(Some(len)) if len > 13 => SafeCast,
PostgresType::Time(None) => return None,
PostgresType::Time(Some(b)) if a.is_none() || a == Some(b) => return None,
Timetz(_) => SafeCast,
_ => NotCastable,
},
Timetz(a) => match next {
Text | VarChar(None) => SafeCast,
Char(Some(len)) | VarChar(Some(len)) if len > 18 => SafeCast,
PostgresType::Timetz(None) => return None,
PostgresType::Timetz(Some(b)) if a.is_none() || a == Some(b) => return None,
Timetz(_) | Time(_) => SafeCast,
_ => NotCastable,
},
Boolean => match next {
Text | VarChar(_) => SafeCast,
Char(Some(length)) if length > 4 => SafeCast,
Char(Some(length)) if length > 3 => RiskyCast,
_ => NotCastable,
},
Bit(None) => match next {
Text | VarChar(_) | Char(_) | VarBit(_) => SafeCast,
_ => NotCastable,
},
Bit(Some(length)) => match next {
Text | VarChar(None) | VarBit(None) => SafeCast,
VarChar(Some(new_length)) if new_length >= length => SafeCast,
VarBit(Some(new_length)) | Char(Some(new_length)) if new_length >= length => SafeCast,
_ => NotCastable,
},
VarBit(None) => match next {
Text | VarChar(None) => SafeCast,
VarChar(_) | Char(_) | Bit(_) => RiskyCast,
_ => NotCastable,
},
VarBit(Some(length)) => match next {
Text | VarChar(None) | VarBit(None) => SafeCast,
VarBit(Some(new_length)) if new_length > length => SafeCast,
VarChar(Some(new_length)) | Char(Some(new_length)) if new_length >= length => SafeCast,
Bit(Some(new_length)) if new_length <= length => RiskyCast,
Bit(None) => RiskyCast,
Char(_) | VarChar(_) => RiskyCast,
_ => NotCastable,
},
Uuid => match next {
Text | VarChar(None) => SafeCast,
VarChar(Some(length)) | Char(Some(length)) if length > 31 => SafeCast,
_ => NotCastable,
},
Xml => match next {
Text | VarChar(None) => SafeCast,
VarChar(_) | Char(_) => RiskyCast,
_ => NotCastable,
},
Json => match next {
Text | JsonB | VarChar(None) => SafeCast,
VarChar(_) | Char(_) => RiskyCast,
_ => NotCastable,
},
JsonB => match next {
Text | Json | VarChar(None) => SafeCast,
VarChar(_) | Char(_) => RiskyCast,
_ => NotCastable,
},
})
};
if previous == next {
None
} else {
cast()
}
}
fn push_alter_enum_previous_usages_as_default(db: &DifferDatabase<'_>, alter_enum: &mut AlterEnum) {
let mut previous_usages_as_default: Vec<(_, Option<_>)> = Vec::new();
let enum_names = db.schemas().walk(alter_enum.id).map(|enm| enm.name());
for table in db.dropped_tables() {
for column in table
.columns()
.filter(|col| col.column_type_is_enum(enum_names.previous) && col.default().is_some())
{
previous_usages_as_default.push((column.id, None));
}
}
for tables in db.table_pairs() {
for column in tables
.dropped_columns()
.filter(|col| col.column_type_is_enum(enum_names.previous) && col.default().is_some())
{
previous_usages_as_default.push((column.id, None));
}
for columns in tables
.column_pairs()
.filter(|col| col.previous.column_type_is_enum(enum_names.previous) && col.previous.default().is_some())
{
let next_usage_as_default = Some(&columns.next)
.filter(|col| col.column_type_is_enum(enum_names.next) && col.default().is_some())
.map(|col| col.id);
previous_usages_as_default.push((columns.previous.id, next_usage_as_default));
}
}
alter_enum.previous_usages_as_default = previous_usages_as_default;
}