pgevolve-core 0.4.6

Postgres declarative schema management — core library (parser, IR, diff, planner) powering the pgevolve CLI.
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
//! Differ for `Catalog::ts_configurations`.
//!
//! Text-search configurations are **managed** (schema-scoped): a live
//! configuration that is absent from source IS auto-dropped — unlike lenient
//! objects such as event triggers or statistics.
//!
//! Identity is `qname`. The `BTreeMap` key is `qname.render_sql()` — a stable,
//! canonical representation.
//!
//! Logic summary:
//! - source-only → `Create` (Safe).
//! - target-only → `Drop` (Safe — configurations carry no data).
//! - both present, `parser` differs → `Replace` (Safe, subsumes
//!   mappings/owner/comment; PG has no `ALTER … PARSER`).
//! - else: per-token-type mapping diff (sorted by `token_type` for
//!   determinism):
//!   - source-only token → `AddMapping` (Safe).
//!   - both but `dictionaries` differ → `AlterMapping` (Safe).
//!   - target-only token → `DropMapping` (Safe).
//! - owner lenient (only when source declares one) → `AlterOwner` (Safe).
//! - comment differs → `CommentOn` (Safe).

use std::collections::BTreeMap;

use crate::diff::change::{Change, TsConfigurationChange};
use crate::diff::changeset::ChangeSet;
use crate::diff::destructiveness::Destructiveness;
use crate::identifier::QualifiedName;
use crate::ir::catalog::Catalog;
use crate::ir::text_search::configuration::{TsConfiguration, TsMapping};

/// Compute text-search configuration changes needed to converge `target` (live)
/// toward `source`.
///
/// Appends all emitted changes to `out`. Configurations are **managed**: a
/// target-only configuration (absent from source) IS auto-dropped.
pub fn diff_ts_configurations(target: &Catalog, source: &Catalog, out: &mut ChangeSet) {
    let target_map: BTreeMap<String, &TsConfiguration> = target
        .ts_configurations
        .iter()
        .map(|c| (c.qname.render_sql(), c))
        .collect();
    let source_map: BTreeMap<String, &TsConfiguration> = source
        .ts_configurations
        .iter()
        .map(|c| (c.qname.render_sql(), c))
        .collect();

    // Source-only → Create.
    for (key, src) in &source_map {
        if !target_map.contains_key(key) {
            out.push(
                Change::TsConfiguration(TsConfigurationChange::Create((*src).clone())),
                Destructiveness::Safe,
            );
        }
    }

    // Target-only → Drop (managed, not lenient).
    for (key, tgt) in &target_map {
        if !source_map.contains_key(key) {
            out.push(
                Change::TsConfiguration(TsConfigurationChange::Drop {
                    qname: tgt.qname.clone(),
                }),
                Destructiveness::Safe,
            );
        }
    }

    // Both present → granular diff.
    for (key, src) in &source_map {
        let Some(tgt) = target_map.get(key) else {
            continue;
        };
        emit_modify(tgt, src, out);
    }
}

fn emit_modify(t: &TsConfiguration, s: &TsConfiguration, out: &mut ChangeSet) {
    // Parser change requires DROP + CREATE; subsumes mappings/owner/comment.
    if t.parser != s.parser {
        out.push(
            Change::TsConfiguration(TsConfigurationChange::Replace {
                from: t.clone(),
                to: s.clone(),
            }),
            Destructiveness::Safe,
        );
        return;
    }

    // Diff mappings by token_type using a BTreeMap for deterministic ordering.
    diff_mappings(&s.qname, &t.mappings, &s.mappings, out);

    // Owner is lenient: only when source declares one and it differs.
    if let Some(src_owner) = &s.owner
        && t.owner.as_ref() != Some(src_owner)
    {
        out.push(
            Change::TsConfiguration(TsConfigurationChange::AlterOwner {
                qname: s.qname.clone(),
                owner: src_owner.clone(),
            }),
            Destructiveness::Safe,
        );
    }

    // Comment differs → CommentOn (both directions: set or clear).
    if t.comment != s.comment {
        out.push(
            Change::TsConfiguration(TsConfigurationChange::CommentOn {
                qname: s.qname.clone(),
                comment: s.comment.clone(),
            }),
            Destructiveness::Safe,
        );
    }
}

/// Diff token-type → dictionary-chain mappings between `target_mappings` (live)
/// and `source_mappings` (desired). Emits Add/Alter/Drop in `token_type`
/// order (`BTreeMap` guarantees ascending order).
fn diff_mappings(
    qname: &QualifiedName,
    target_mappings: &[TsMapping],
    source_mappings: &[TsMapping],
    out: &mut ChangeSet,
) {
    let t_map: BTreeMap<&str, &Vec<QualifiedName>> = target_mappings
        .iter()
        .map(|m| (m.token_type.as_str(), &m.dictionaries))
        .collect();
    let s_map: BTreeMap<&str, &Vec<QualifiedName>> = source_mappings
        .iter()
        .map(|m| (m.token_type.as_str(), &m.dictionaries))
        .collect();

    // Iterate over all token types in source (sorted by BTreeMap).
    for (token_type, s_dicts) in &s_map {
        match t_map.get(token_type) {
            None => {
                // Source-only → AddMapping.
                out.push(
                    Change::TsConfiguration(TsConfigurationChange::AddMapping {
                        qname: qname.clone(),
                        token_type: (*token_type).to_owned(),
                        dictionaries: (*s_dicts).clone(),
                    }),
                    Destructiveness::Safe,
                );
            }
            Some(t_dicts) if t_dicts != s_dicts => {
                // Both present, dictionaries differ → AlterMapping.
                out.push(
                    Change::TsConfiguration(TsConfigurationChange::AlterMapping {
                        qname: qname.clone(),
                        token_type: (*token_type).to_owned(),
                        dictionaries: (*s_dicts).clone(),
                    }),
                    Destructiveness::Safe,
                );
            }
            Some(_) => {
                // Identical — no change.
            }
        }
    }

    // Target-only token types → DropMapping (iterate in sorted order).
    for token_type in t_map.keys() {
        if !s_map.contains_key(token_type) {
            out.push(
                Change::TsConfiguration(TsConfigurationChange::DropMapping {
                    qname: qname.clone(),
                    token_type: (*token_type).to_owned(),
                }),
                Destructiveness::Safe,
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::diff::change::{Change, TsConfigurationChange};
    use crate::identifier::{Identifier, QualifiedName};
    use crate::ir::catalog::Catalog;
    use crate::ir::text_search::configuration::{TsConfiguration, TsMapping};

    fn id(s: &str) -> Identifier {
        Identifier::from_unquoted(s).unwrap()
    }

    fn qn(schema: &str, name: &str) -> QualifiedName {
        QualifiedName::new(id(schema), id(name))
    }

    /// Build a minimal configuration with the default parser and no mappings.
    fn basic_config(name: &str) -> TsConfiguration {
        TsConfiguration {
            qname: qn("app", name),
            parser: qn("pg_catalog", "default"),
            mappings: vec![],
            owner: None,
            comment: None,
        }
    }

    fn mapping(token_type: &str, dicts: Vec<QualifiedName>) -> TsMapping {
        TsMapping {
            token_type: token_type.to_owned(),
            dictionaries: dicts,
        }
    }

    fn cat(configs: Vec<TsConfiguration>) -> Catalog {
        let mut c = Catalog::empty();
        c.ts_configurations = configs;
        c
    }

    fn run(target: &Catalog, source: &Catalog) -> ChangeSet {
        let mut out = ChangeSet::new();
        diff_ts_configurations(target, source, &mut out);
        out
    }

    // ---- source-only → Create ----

    #[test]
    fn source_only_creates() {
        let changes = run(&cat(vec![]), &cat(vec![basic_config("english")]));
        assert_eq!(changes.len(), 1);
        assert!(matches!(
            changes.iter().next().unwrap().change,
            Change::TsConfiguration(TsConfigurationChange::Create(_))
        ));
    }

    // ---- target-only → Drop (managed, NOT lenient) ----

    #[test]
    fn target_only_drops() {
        let changes = run(&cat(vec![basic_config("english")]), &cat(vec![]));
        assert_eq!(
            changes.len(),
            1,
            "managed configuration must emit Drop when absent from source"
        );
        assert!(
            matches!(
                changes.iter().next().unwrap().change,
                Change::TsConfiguration(TsConfigurationChange::Drop { .. })
            ),
            "expected Drop, got {:?}",
            changes.iter().next().unwrap().change
        );
    }

    // ---- parser change → Replace (from=target/live, to=source/desired) ----

    #[test]
    fn different_parser_replaces() {
        let t = basic_config("english");
        let mut s = basic_config("english");
        s.parser = qn("app", "custom_parser");
        let changes = run(&cat(vec![t.clone()]), &cat(vec![s.clone()]));
        assert_eq!(changes.len(), 1);
        let entry = changes.iter().next().unwrap();
        match &entry.change {
            Change::TsConfiguration(TsConfigurationChange::Replace { from, to }) => {
                assert_eq!(from, &t, "from must be the target (live) configuration");
                assert_eq!(to, &s, "to must be the source (desired) configuration");
            }
            other => panic!("expected Replace, got {other:?}"),
        }
    }

    // ---- parser Replace short-circuits mappings/owner/comment ----

    #[test]
    fn replace_subsumes_mappings_owner_and_comment() {
        let t = basic_config("english");
        let mut s = basic_config("english");
        s.parser = qn("app", "custom_parser"); // structural
        s.mappings = vec![mapping("word", vec![qn("app", "english_stem")])];
        s.owner = Some(id("alice"));
        s.comment = Some("custom config".into());
        let changes = run(&cat(vec![t]), &cat(vec![s]));
        assert_eq!(
            changes.len(),
            1,
            "Replace must subsume mappings + owner + comment"
        );
        assert!(matches!(
            changes.iter().next().unwrap().change,
            Change::TsConfiguration(TsConfigurationChange::Replace { .. })
        ));
    }

    // ---- AddMapping: token in source only ----

    #[test]
    fn add_mapping_when_token_in_source_only() {
        let t = basic_config("english");
        let mut s = basic_config("english");
        s.mappings = vec![mapping("word", vec![qn("app", "english_stem")])];
        let changes = run(&cat(vec![t]), &cat(vec![s]));
        assert_eq!(changes.len(), 1);
        let entry = changes.iter().next().unwrap();
        match &entry.change {
            Change::TsConfiguration(TsConfigurationChange::AddMapping {
                token_type,
                dictionaries,
                ..
            }) => {
                assert_eq!(token_type, "word");
                assert_eq!(dictionaries, &vec![qn("app", "english_stem")]);
            }
            other => panic!("expected AddMapping, got {other:?}"),
        }
    }

    // ---- AlterMapping: dict chain differs ----

    #[test]
    fn alter_mapping_when_dictionaries_differ() {
        let mut t = basic_config("english");
        t.mappings = vec![mapping("word", vec![qn("app", "english_stem")])];
        let mut s = basic_config("english");
        s.mappings = vec![mapping(
            "word",
            vec![qn("app", "english_stem"), qn("pg_catalog", "simple")],
        )];
        let changes = run(&cat(vec![t]), &cat(vec![s]));
        assert_eq!(changes.len(), 1);
        let entry = changes.iter().next().unwrap();
        match &entry.change {
            Change::TsConfiguration(TsConfigurationChange::AlterMapping {
                token_type,
                dictionaries,
                ..
            }) => {
                assert_eq!(token_type, "word");
                assert_eq!(
                    dictionaries,
                    &vec![qn("app", "english_stem"), qn("pg_catalog", "simple")]
                );
            }
            other => panic!("expected AlterMapping, got {other:?}"),
        }
    }

    // ---- DropMapping: token in target only ----

    #[test]
    fn drop_mapping_when_token_in_target_only() {
        let mut t = basic_config("english");
        t.mappings = vec![mapping("word", vec![qn("app", "english_stem")])];
        let s = basic_config("english"); // no mappings
        let changes = run(&cat(vec![t]), &cat(vec![s]));
        assert_eq!(changes.len(), 1);
        let entry = changes.iter().next().unwrap();
        match &entry.change {
            Change::TsConfiguration(TsConfigurationChange::DropMapping { token_type, .. }) => {
                assert_eq!(token_type, "word");
            }
            other => panic!("expected DropMapping, got {other:?}"),
        }
    }

    // ---- Mixed: one add + one alter + one drop in a single config ----

    #[test]
    fn mixed_add_alter_drop_mapping() {
        // Target has: "asciiword" → [simple], "word" → [english_stem]
        // Source has: "asciiword" → [english_stem, simple], "numword" → [simple]
        //
        // Expected:
        //   AddMapping "numword"
        //   AlterMapping "asciiword"  (dict chain changed)
        //   DropMapping "word"
        let mut t = basic_config("english");
        t.mappings = vec![
            mapping("asciiword", vec![qn("pg_catalog", "simple")]),
            mapping("word", vec![qn("app", "english_stem")]),
        ];
        let mut s = basic_config("english");
        s.mappings = vec![
            mapping(
                "asciiword",
                vec![qn("app", "english_stem"), qn("pg_catalog", "simple")],
            ),
            mapping("numword", vec![qn("pg_catalog", "simple")]),
        ];

        let changes = run(&cat(vec![t]), &cat(vec![s]));
        assert_eq!(
            changes.len(),
            3,
            "expected 3 changes (add+alter+drop), got: {changes:?}"
        );

        let kinds: Vec<_> = changes
            .iter()
            .map(|e| match &e.change {
                Change::TsConfiguration(c) => c.clone(),
                other => panic!("unexpected change kind: {other:?}"),
            })
            .collect();

        assert!(
            kinds.iter().any(|c| matches!(
                c,
                TsConfigurationChange::AddMapping { token_type, .. } if token_type == "numword"
            )),
            "expected AddMapping for numword"
        );
        assert!(
            kinds.iter().any(|c| matches!(
                c,
                TsConfigurationChange::AlterMapping { token_type, .. } if token_type == "asciiword"
            )),
            "expected AlterMapping for asciiword"
        );
        assert!(
            kinds.iter().any(|c| matches!(
                c,
                TsConfigurationChange::DropMapping { token_type, .. } if token_type == "word"
            )),
            "expected DropMapping for word"
        );
    }

    // ---- owner change → AlterOwner (lenient) ----

    #[test]
    fn owner_change_emits_alter_owner() {
        let mut t = basic_config("english");
        t.owner = Some(id("alice"));
        let mut s = basic_config("english");
        s.owner = Some(id("bob"));
        let changes = run(&cat(vec![t]), &cat(vec![s]));
        assert_eq!(changes.len(), 1);
        assert!(matches!(
            changes.iter().next().unwrap().change,
            Change::TsConfiguration(TsConfigurationChange::AlterOwner { .. })
        ));
    }

    #[test]
    fn source_owner_none_no_alter_owner() {
        let mut t = basic_config("english");
        t.owner = Some(id("alice"));
        let s = basic_config("english"); // owner = None
        let changes = run(&cat(vec![t]), &cat(vec![s]));
        assert!(
            changes.is_empty(),
            "source owner None = unmanaged, no change expected"
        );
    }

    // ---- comment change → CommentOn ----

    #[test]
    fn comment_change_emits_comment_on() {
        let t = basic_config("english");
        let mut s = basic_config("english");
        s.comment = Some("English text search configuration".into());
        let changes = run(&cat(vec![t]), &cat(vec![s]));
        assert_eq!(changes.len(), 1);
        assert!(matches!(
            changes.iter().next().unwrap().change,
            Change::TsConfiguration(TsConfigurationChange::CommentOn { .. })
        ));
    }

    // ---- identical → no changes ----

    #[test]
    fn identical_configurations_produce_no_changes() {
        let mut cfg = basic_config("english");
        cfg.mappings = vec![
            mapping("asciiword", vec![qn("app", "english_stem")]),
            mapping("word", vec![qn("app", "english_stem")]),
        ];
        cfg.owner = Some(id("alice"));
        cfg.comment = Some("English config".into());
        let c = cat(vec![cfg]);
        let changes = run(&c, &c);
        assert!(changes.is_empty());
    }
}