safe-migrate 0.9.0

Check PostgreSQL migrations against a synchronized database baseline
Documentation
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
use crate::_internal::analysis::expr_ir::ExprIr;
use crate::_internal::ast::identifiers::{Ident, QualifiedName};

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum PersistenceFact {
    Permanent,
    Temporary,
    Unlogged,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum OnCommitFact {
    PreserveRows,
    DeleteRows,
    Drop,
}

/// Properties selected by `CREATE TABLE ... LIKE` after applying its ordered
/// INCLUDING/EXCLUDING clauses.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub(crate) struct LikePropertiesFact {
    pub defaults: bool,
    pub generated: bool,
    pub storage: bool,
    pub compression: bool,
    pub statistics: bool,
    pub constraints: bool,
    pub indexes: bool,
    pub identity: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct LikeSourceFact {
    pub relation: QualifiedName,
    pub properties: LikePropertiesFact,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum ReplicaIdentityFact {
    Default,
    Full,
    Nothing,
    UsingIndex(QualifiedName),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum PolicyCommand {
    All,
    Select,
    Insert,
    Update,
    Delete,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum SearchPathTarget {
    Default,
    Schemas(Vec<String>),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum TimeoutSetting {
    Lock,
    Statement,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum TimeoutSettingValue {
    Default,
    Milliseconds(u64),
    Current,
    Invalid(String),
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum ResetSettingTarget {
    All,
    SearchPath,
    LockTimeout,
    StatementTimeout,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum TypeCreationKind {
    Enum {
        variants: Vec<String>,
    },
    #[expect(dead_code, reason = "reserved for typed Squawk range support")]
    Range,
    Composite {
        fields: Vec<CompositeFieldFact>,
    },
    #[expect(dead_code, reason = "reserved for typed Squawk base-type support")]
    Base,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) struct CompositeFieldFact {
    pub name: String,
    pub data_type: String,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterViewAction {
    RenameTo {
        new_name: Ident,
    },
    OwnerTo {
        new_owner: RoleFact,
    },
    SetSchema {
        new_schema: String,
    },
    #[expect(dead_code, reason = "reserved for typed Squawk view-default support")]
    SetDefault {
        column: String,
        default: Option<ExprIr>,
    },
    #[expect(dead_code, reason = "reserved for typed Squawk view-default support")]
    DropDefault {
        column: String,
    },
    RenameColumn {
        from: Ident,
        to: Ident,
    },
    #[expect(dead_code, reason = "reserved for typed Squawk view-option support")]
    SetOptions {
        options: Vec<String>,
    },
    #[expect(dead_code, reason = "reserved for typed Squawk view-option support")]
    ResetOptions {
        options: Vec<String>,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterSchemaActionFact {
    RenameTo { new_name: Ident },
    OwnerTo { new_owner: RoleFact },
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum StatementFact {
    CreateSchema {
        name: QualifiedName,
        if_not_exists: bool,
        authorization: Option<RoleFact>,
    },
    AlterSchema {
        name: QualifiedName,
        action: AlterSchemaActionFact,
    },
    DropSchema {
        names: Vec<QualifiedName>,
        if_exists: bool,
        cascade: bool,
    },
    CreateTable {
        name: QualifiedName,
        if_not_exists: bool,
        as_select: bool,
        persistence: PersistenceFact,
        on_commit: Option<OnCommitFact>,
        columns: Vec<ColumnFact>,
        foreign_keys: Vec<FkFact>,
        table_constraints: Vec<TableConstraintFact>,
        partition_by: Option<String>,
        partition_strategy: Option<String>,
        partition_of: Option<QualifiedName>,
        partition_bound: Option<String>,
        inherits: Vec<QualifiedName>,
        like_sources: Vec<LikeSourceFact>,
        of_type: Option<QualifiedName>,
        select_source: Option<QualifiedName>,
        select_outputs: Vec<SelectOutputFact>,
        select_projection_complete: bool,
    },
    CreateView {
        name: QualifiedName,
        or_replace: bool,
        depends_on: Vec<QualifiedName>,
    },
    AlterView {
        name: QualifiedName,
        action: AlterViewAction,
    },
    CreateMaterializedView {
        name: QualifiedName,
        depends_on: Vec<QualifiedName>,
    },
    AlterMaterializedView {
        name: QualifiedName,
        new_name: Option<Ident>,
    },
    RefreshMaterializedView {
        name: QualifiedName,
        concurrently: bool,
    },
    CreateIndex {
        name: QualifiedName,
        relation: QualifiedName,
        if_not_exists: bool,
        concurrently: bool,
        using_method: Option<String>,
        has_predicate: bool,
        unique: bool,
        key_columns: Vec<String>,
        included_columns: Vec<String>,
        has_expression_keys: bool,
        has_default_sort_order: bool,
        has_default_opclasses: bool,
        has_default_collations: bool,
    },
    CreatePolicy {
        name: String,
        table: QualifiedName,
        permissive: bool,
        command: PolicyCommand,
        /// False when roles or policy expressions are present but not modeled.
        semantics_complete: bool,
    },
    DropPolicy {
        name: String,
        table: QualifiedName,
        if_exists: bool,
    },
    CreateTrigger {
        name: String,
        table: QualifiedName,
        function: Option<QualifiedName>,
        row_level: bool,
    },
    DropTrigger {
        name: String,
        table: QualifiedName,
        if_exists: bool,
    },
    AlterTrigger {
        name: String,
        table: QualifiedName,
        new_name: String,
    },
    AlterTable {
        name: QualifiedName,
        only: bool,
        actions: Vec<AlterTableActionFact>,
    },
    AlterIndex {
        name: QualifiedName,
        actions: Vec<AlterIndexActionFact>,
    },
    CreateType(CreateTypeFact),
    AlterType(AlterTypeFact),
    CreateDomain {
        name: QualifiedName,
        base_type: String,
    },
    AlterDomain {
        name: QualifiedName,
        action: Option<AlterDomainActionFact>,
    },
    DropDomain {
        names: Vec<QualifiedName>,
        if_exists: bool,
        cascade: bool,
    },
    DropType {
        names: Vec<QualifiedName>,
        if_exists: bool,
        cascade: bool,
    },
    CreateSequence {
        name: QualifiedName,
        if_not_exists: bool,
        owned_by: Option<(QualifiedName, String)>,
        persistence: PersistenceFact,
        options: IdentitySequenceOptionsFact,
    },
    AlterSequence {
        name: QualifiedName,
        if_exists: bool,
        action: AlterSequenceActionFact,
    },
    DropSequence {
        names: Vec<QualifiedName>,
        if_exists: bool,
        cascade: bool,
    },
    DropTable {
        names: Vec<QualifiedName>,
        if_exists: bool,
        cascade: bool,
    },
    DropView {
        names: Vec<QualifiedName>,
        if_exists: bool,
        cascade: bool,
    },
    DropMaterializedView {
        names: Vec<QualifiedName>,
        if_exists: bool,
        cascade: bool,
    },
    DropIndex {
        names: Vec<QualifiedName>,
        if_exists: bool,
        concurrently: bool,
        cascade: bool,
    },
    Lock {
        targets: Vec<RelationTargetFact>,
        mode: LockModeFact,
        nowait: bool,
    },
    Truncate {
        targets: Vec<RelationTargetFact>,
        cascade: bool,
        restart_identity: bool,
    },
    SetSearchPath {
        target: SearchPathTarget,
        local: bool,
    },
    SetTimeout {
        setting: TimeoutSetting,
        value: TimeoutSettingValue,
        local: bool,
    },
    ResetSettings {
        target: ResetSettingTarget,
    },
    BeginTransaction,
    CommitTransaction,
    CommitAndChain,
    RollbackTransaction,
    RollbackAndChain,
    RollbackToSavepoint {
        name: String,
    },
    Savepoint {
        name: String,
    },
    ReleaseSavepoint {
        name: String,
    },
    PrepareTransaction {
        name: String,
    },
    SetTransaction,
    SetConstraints,
    OpaqueBlock,
    Execute,
    /// Parsed SQL that changes PostgreSQL metadata but no schema state modeled
    /// by safe-migrate, such as `COMMENT ON`.
    SchemaNeutralNoop,
    Vacuum {
        relation: Option<QualifiedName>,
        is_full: bool,
    },
    CreateFunction(CreateFunctionFact),
    AlterFunction(AlterFunctionFact),
    DropFunction(DropFunctionFact),
    CreateProcedure(CreateProcedureFact),
    AlterProcedure(AlterProcedureFact),
    DropProcedure(DropProcedureFact),
    CreateAggregate(CreateAggregateFact),
    AlterAggregate(AlterAggregateFact),
    DropAggregate(DropAggregateFact),
    CreatePublication(CreatePublicationFact),
    AlterPublication(AlterPublicationFact),
    DropPublication(DropPublicationFact),
    CreateSubscription(CreateSubscriptionFact),
    AlterSubscription(AlterSubscriptionFact),
    DropSubscription(DropSubscriptionFact),
    CreateRole(CreateRoleFact),
    AlterRole(AlterRoleFact),
    DropRole(DropRoleFact),
    Grant(GrantFact),
    Revoke(RevokeFact),
    CreateDatabase(CreateDatabaseFact),
    AlterDatabase(AlterDatabaseFact),
    DropDatabase(DropDatabaseFact),
    /// `SET [LOCAL] ROLE { rolename | NONE }` or
    /// `SET [LOCAL] SESSION AUTHORIZATION { rolename | DEFAULT }`.
    /// `role = None` means NONE / DEFAULT (restore the session default).
    /// `local = true` means the change is scoped to the current transaction.
    /// `is_session_auth = true` means SET SESSION AUTHORIZATION (which also
    /// updates the session-level default, unlike plain SET ROLE).
    SetRole {
        role: Option<RoleFact>,
        local: bool,
        is_session_auth: bool,
    },
}

/// A relation target whose `ONLY` modifier materially changes inheritance and
/// partition behavior.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct RelationTargetFact {
    pub name: QualifiedName,
    pub only: bool,
}

/// Lifecycle form used by `ALTER TABLE ... DETACH PARTITION`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum DetachPartitionMode {
    Immediate,
    Concurrently,
    Finalize,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum LockModeFact {
    AccessShare,
    RowShare,
    RowExclusive,
    ShareUpdateExclusive,
    Share,
    ShareRowExclusive,
    Exclusive,
    AccessExclusive,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum RuleEnableModeFact {
    Origin,
    Disabled,
    Replica,
    Always,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterSequenceActionFact {
    OwnedBy(Option<(QualifiedName, String)>),
    OwnerTo(RoleFact),
    RenameTo(Ident),
    SetSchema(String),
    Other,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterIndexActionFact {
    RenameTo { new_name: Ident },
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CreateTypeFact {
    pub name: QualifiedName,
    pub kind: TypeCreationKind,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AlterTypeFact {
    pub name: QualifiedName,
    pub actions: Vec<AlterTypeActionFact>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterTypeActionFact {
    RenameTo {
        new_name: Ident,
    },
    SetSchema {
        new_schema: String,
    },
    AddValue {
        new_value: String,
        neighbor: Option<String>,
        before: bool,
    },
    RenameValue {
        old_value: String,
        new_value: String,
    },
}

#[derive(Clone, Debug, PartialEq, Default)]
pub(crate) enum RoleFact {
    #[default]
    Unknown,
    Named {
        name: String,
        via_legacy_group_syntax: bool,
    },
    CurrentUser,
    CurrentRole,
    SessionUser,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CreateRoleFact {
    pub name: String,
    pub inherits: bool,
    pub can_login: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AlterRoleFact {
    pub name: RoleFact,
    pub inherits: Option<bool>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DropRoleFact {
    pub names: Vec<String>,
    pub if_exists: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CreateFunctionFact {
    pub name: QualifiedName,
    pub or_replace: bool,
    pub params: Vec<ParamFact>,
    pub return_type: Option<RetTypeFact>,
    pub options: Vec<FuncOptionFact>,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) struct ParamFact {
    pub mode: ParamModeFact,
    pub name: Option<String>,
    pub ty: String,
    pub default: Option<ExprIr>,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum ParamModeFact {
    In,
    Out,
    InOut,
    Variadic,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum RetTypeFact {
    Table(Vec<ColumnFact>),
    Scalar(String),
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum FuncOptionFact {
    Language(String),
    Volatility(VolatilityKind),
    Security(SecurityKind),
    Strict(StrictKind),
    Leakproof(bool),
    Parallel(String),
    Cost,
    Rows,
    Reset(String),
    As {
        definition: Option<String>,
        obj_file: Option<String>,
        link_symbol: Option<String>,
    },
    Transform,
    Window,
    Support,
    Unknown,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum VolatilityKind {
    Immutable,
    Stable,
    Volatile,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum SecurityKind {
    Invoker,
    Definer,
}
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum StrictKind {
    Strict,
    CalledOnNull,
    ReturnsNullOnNull,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AlterFunctionFact {
    pub name: QualifiedName,
    pub params: Vec<String>,
    pub action: AlterFunctionAction,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterFunctionAction {
    Rename { from: String, to: String },
    OwnerChange(RoleFact),
    SchemaChange { new_schema: String },
    DependsOnExtension { extension: String },
    NoDependsOnExtension { extension: String },
    OptionsChange(Vec<FuncOptionFact>),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DropFunctionFact {
    pub signatures: Vec<FunctionSigFact>,
    pub if_exists: bool,
    pub cascade: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct FunctionSigFact {
    pub name: QualifiedName,
    pub params: Vec<String>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CreateProcedureFact {
    pub name: QualifiedName,
    pub or_replace: bool,
    pub params: Vec<ParamFact>,
    pub options: Vec<FuncOptionFact>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AlterProcedureFact {
    pub name: QualifiedName,
    pub params: Vec<String>,
    pub action: AlterFunctionAction,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DropProcedureFact {
    pub signatures: Vec<FunctionSigFact>,
    pub if_exists: bool,
    pub cascade: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CreateAggregateFact {
    pub name: QualifiedName,
    pub or_replace: bool,
    pub params: Vec<ParamFact>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AlterAggregateFact {
    pub name: QualifiedName,
    pub params: Vec<String>,
    pub action: AlterFunctionAction,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DropAggregateFact {
    pub signatures: Vec<FunctionSigFact>,
    pub if_exists: bool,
    pub cascade: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CreatePublicationFact {
    pub name: String,
    pub scope: PublicationScope,
    pub params: Vec<AttributeFact>,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum PublicationScope {
    AllTables { except: Vec<String> },
    Explicit(Vec<PublicationObjectFact>),
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum PublicationObjectFact {
    Table {
        name: QualifiedName,
        only: bool,
        include_partitions: bool,
        columns: Option<Vec<String>>,
        row_filter: Option<PublicationRowFilter>,
    },
    SchemaTables {
        schema: String,
        row_filter: Option<PublicationRowFilter>,
    },
    CurrentSchemaShorthand,
    Unknown,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum PublicationRowFilter {
    Parsed(ExprIr),
    CatalogSql(String),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AlterPublicationFact {
    pub name: String,
    pub action: AlterPublicationActionFact,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterPublicationActionFact {
    AddObjects(Vec<PublicationObjectFact>),
    SetObjects(PublicationScope),
    DropObjects(Vec<PublicationObjectFact>),
    SetOptions(Vec<AttributeFact>),
    OwnerChange(RoleFact),
    Rename { to: String },
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DropPublicationFact {
    pub names: Vec<String>,
    pub if_exists: bool,
    pub cascade: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CreateSubscriptionFact {
    pub name: Option<String>,
    pub connection: ConnectionTarget,
    pub publications: Vec<String>,
    pub params: Option<Vec<AttributeFact>>,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) enum ConnectionTarget {
    Literal(Option<String>),
    Server(Option<String>),
    /// A synchronized subscription exists, but its connection string is never
    /// read from PostgreSQL or written to the cache.
    Redacted,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AlterSubscriptionFact {
    pub name: String,
    pub action: AlterSubscriptionActionFact,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum SubscriptionPublicationMode {
    Set,
    Add,
    Drop,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterSubscriptionActionFact {
    SetConnection(ConnectionTarget),
    Publications {
        mode: SubscriptionPublicationMode,
        publications: Vec<String>,
        params: Vec<AttributeFact>,
    },
    RefreshPublication(Vec<AttributeFact>),
    SetEnabled(bool),
    SetOptions(Vec<AttributeFact>),
    SetServer(Option<String>),
    Skip(Vec<AttributeFact>),
    OwnerChange(RoleFact),
    Rename {
        to: String,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DropSubscriptionFact {
    pub name: String,
    pub if_exists: bool,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct GrantFact {
    pub privileges: PrivilegeSpec,
    pub target: GrantTarget,
    pub grantees: Vec<RoleFact>,
    pub with_grant_option: bool,
    pub role_options: Vec<RoleMembershipOptionFact>,
    pub granted_by: Option<RoleFact>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct RevokeFact {
    pub grant_option_only: bool,
    pub role_option: Option<RoleMembershipOptionFact>,
    pub privileges: PrivilegeSpec,
    pub target: GrantTarget,
    pub revokees: Vec<RoleFact>,
    pub granted_by: Option<RoleFact>,
    pub cascade: bool,
}

/// Per-membership option supported by PostgreSQL 16+ role grants. The option
/// name and value are kept typed at extraction time so state transitions never
/// need to infer semantics from raw SQL text.
#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum RoleMembershipOptionFact {
    Admin(bool),
    Inherit(bool),
    Set(bool),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum PrivilegeSpec {
    All,
    List(Vec<PrivilegeFact>),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum PrivilegeFact {
    Select,
    Insert,
    Update,
    Delete,
    Truncate,
    References,
    Trigger,
    Maintain,
    Execute,
    Create,
    Temporary,
    AlterSystem,
    All,
    Named(String),
    RoleMembership(String),
    Unknown,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum GrantTarget {
    Tables(Vec<QualifiedName>),
    AllTablesInSchema(Vec<String>),
    Roles(Vec<String>),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct CreateDatabaseFact {
    pub name: String,
    pub options: Vec<DatabaseOptionFact>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum DatabaseOptionFact {
    Owner(DatabaseOptionValue),
    Template(DatabaseOptionValue),
    Encoding(DatabaseOptionValue),
    Tablespace(DatabaseOptionValue),
    ConnectionLimit(DatabaseOptionValue),
    Named(String, DatabaseOptionValue),
    Unknown(DatabaseOptionValue),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum DatabaseOptionValue {
    Default,
    Literal(Option<String>),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct AlterDatabaseFact {
    pub name: QualifiedName,
    pub action: AlterDatabaseAction,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterDatabaseAction {
    Rename { to: String },
    OwnerChange(RoleFact),
    TablespaceChange { new_tablespace: String },
    SetConfigParam { param: String },
    ResetConfigParam { param: Option<String> },
    RefreshCollationVersion,
    OptionChanges(Vec<DatabaseOptionFact>),
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DropDatabaseFact {
    pub name: QualifiedName,
    pub if_exists: bool,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) struct AttributeFact {
    pub name: String,
    pub value: String,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub(crate) enum ColumnGeneration {
    Ordinary,
    Serial,
    IdentityAlways,
    IdentityByDefault,
    GeneratedStored,
    GeneratedVirtual,
}

#[derive(Clone, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub(crate) struct IdentitySequenceOptionsFact {
    pub data_type: Option<String>,
    pub start_value: Option<i64>,
    pub increment: Option<i64>,
    pub min_value: Option<Option<i64>>,
    pub max_value: Option<Option<i64>>,
    pub cache_size: Option<i64>,
    pub cycle: Option<bool>,
    pub persistence: Option<bool>,
    pub sequence_name: Option<QualifiedName>,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
pub(crate) struct ColumnFact {
    pub name: String,
    pub ty: Option<String>,
    pub not_null: bool,
    pub is_primary_key: bool,
    pub primary_key_constraint_name: Option<String>,
    pub is_unique: bool,
    pub unique_constraint_name: Option<String>,
    pub default: Option<ExprIr>,
    pub generation: ColumnGeneration,
    pub identity_sequence: Option<IdentitySequenceOptionsFact>,
    pub generated_expr: Option<ExprIr>,
    #[serde(default)]
    pub generated_expr_sql: Option<String>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum SelectOutputFact {
    AllColumns,
    Column {
        source_name: String,
        output_name: String,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) struct FkFact {
    pub constraint_name: Option<String>,
    pub references: QualifiedName,
    pub from_columns: Vec<String>,
    pub to_columns: Vec<String>,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum TableConstraintFact {
    PrimaryKey {
        constraint_name: Option<String>,
        columns: Vec<String>,
    },
    Unique {
        constraint_name: Option<String>,
        columns: Vec<String>,
    },
    Check {
        constraint_name: Option<String>,
        name_hint: Option<String>,
        definition: String,
        columns: Vec<String>,
        columns_complete: bool,
    },
    Exclude {
        constraint_name: Option<String>,
        columns: Vec<String>,
        columns_complete: bool,
    },
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterDomainActionFact {
    AddConstraint,
    DropConstraint,
    DropDefault,
    DropNotNull,
    OwnerChange,
    RenameConstraint,
    RenameTo,
    SetDefault,
    SetNotNull,
    SetSchema,
    ValidateConstraint,
}

#[derive(Clone, Debug, PartialEq)]
pub(crate) enum AlterTableActionFact {
    AddColumn {
        name: String,
        ty: Option<String>,
        if_not_exists: bool,
        not_null: bool,
        default: Option<ExprIr>,
        generation: ColumnGeneration,
        identity_sequence: Option<Box<IdentitySequenceOptionsFact>>,
        generated_expr: Option<ExprIr>,
        generated_expr_sql: Option<String>,
    },
    DropColumn {
        name: String,
        if_exists: bool,
        cascade: bool,
    },
    RenameColumn {
        from: Ident,
        to: Ident,
    },
    RenameTo {
        new_name: Ident,
    },
    AddForeignKey {
        constraint_name: Option<String>,
        references: QualifiedName,
        from_columns: Vec<String>,
        to_columns: Vec<String>,
        not_valid: bool,
    },
    AlterConstraint {
        name: Option<String>,
        deferrable: bool,
    },
    RenameConstraint {
        old_name: String,
        new_name: String,
    },
    DropConstraint {
        name: String,
        if_exists: bool,
        cascade: bool,
    },
    AddCheckConstraint {
        constraint_name: Option<String>,
        definition: String,
        columns: Vec<String>,
        columns_complete: bool,
        not_valid: bool,
    },
    AddUniqueConstraint {
        constraint_name: Option<String>,
        columns: Vec<String>,
        using_index: Option<QualifiedName>,
    },
    AddPrimaryKeyConstraint {
        constraint_name: Option<String>,
        columns: Vec<String>,
        using_index: Option<QualifiedName>,
    },
    AddExcludeConstraint {
        constraint_name: Option<String>,
        columns: Vec<String>,
        columns_complete: bool,
    },
    SetNotNull {
        column: String,
    },
    DropNotNull {
        column: String,
    },
    SetType {
        column: String,
        ty: String,
        has_using: bool,
    },
    SetDefault {
        column: String,
        default: Option<ExprIr>,
    },
    SetExpression {
        column: String,
        expr: ExprIr,
        expression_sql: String,
    },
    SetOptions {
        column: String,
        attributes: Vec<AttributeFact>,
    },
    ResetOptions {
        column: String,
        names: Vec<String>,
    },
    SetTableOptions {
        attributes: Vec<AttributeFact>,
    },
    ResetTableOptions {
        names: Vec<String>,
    },
    Inherit {
        column: String,
        parent: QualifiedName,
    },
    NoInherit {
        column: String,
        parent: QualifiedName,
    },
    ValidateConstraint {
        constraint_name: String,
    },
    DisableTrigger {
        trigger_name: Option<String>,
    },
    EnableTrigger {
        trigger_name: Option<String>,
    },
    AttachPartition {
        child: QualifiedName,
        strategy: Option<String>,
        bound: Option<String>,
    },
    DetachPartition {
        child: QualifiedName,
        mode: DetachPartitionMode,
    },
    SetStorage {
        column: String,
        mode: String,
    },
    SetCompression {
        column: String,
        method: Option<String>,
    },
    SetStatistics {
        column: String,
        target: Option<i32>,
    },
    DropExpression {
        column: String,
        if_exists: bool,
    },
    SetAccessMethod {
        access_method: Option<String>,
    },
    ClusterOn {
        index: QualifiedName,
    },
    SetWithoutCluster,
    InheritTable {
        parent: QualifiedName,
    },
    NoInheritTable {
        parent: QualifiedName,
    },
    OfType {
        type_name: QualifiedName,
    },
    NotOf,
    MergePartitions {
        parent: QualifiedName,
    },
    SplitPartition,
    SetSchema {
        new_schema: String,
    },
    SetTablespace {
        tablespace: String,
    },
    SetLogged,
    SetUnlogged,
    OwnerTo {
        new_owner: RoleFact,
    },
    ReplicaIdentity {
        option: ReplicaIdentityFact,
    },
    ForceRls,
    NoForceRls,
    EnableRls,
    DisableRls,
    EnableAlwaysTrigger {
        trigger_name: Option<String>,
    },
    EnableReplicaTrigger {
        trigger_name: Option<String>,
    },
    SetRuleMode {
        rule_name: Option<String>,
        mode: RuleEnableModeFact,
    },
}