mz_catalog/durable/upgrade/
v71_to_v72.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10use crate::durable::upgrade::MigrationAction;
11use crate::durable::upgrade::{objects_v71 as v71, objects_v72 as v72};
12
13const USER_NETWORK_POLICY_ID_ALLOC_KEY: &str = "user_network_policy";
14
15/// Adds the "user_network_policy" ID allocator.
16pub fn upgrade(
17    snapshot: Vec<v71::StateUpdateKind>,
18) -> Vec<MigrationAction<v71::StateUpdateKind, v72::StateUpdateKind>> {
19    // New environments will already have this ID Allocator created.
20    let network_policies_id_alloc = snapshot
21        .iter()
22        .filter_map(|update| match &update.kind {
23            Some(v71::state_update_kind::Kind::IdAlloc(id_allocator)) => {
24                id_allocator.key.as_ref().map(|key| &key.name)
25            }
26            _ => None,
27        })
28        .any(|id_alloc_name| id_alloc_name == USER_NETWORK_POLICY_ID_ALLOC_KEY);
29
30    if !network_policies_id_alloc {
31        let key = v72::IdAllocKey {
32            name: USER_NETWORK_POLICY_ID_ALLOC_KEY.to_string(),
33        };
34        // V71 introduced a default network policy with ID 1, so ID 2 is next.
35        let val = v72::IdAllocValue { next_id: 2 };
36
37        let create_id_alloc = v72::StateUpdateKind {
38            kind: Some(v72::state_update_kind::Kind::IdAlloc(
39                v72::state_update_kind::IdAlloc {
40                    key: Some(key),
41                    value: Some(val),
42                },
43            )),
44        };
45        vec![MigrationAction::Insert(create_id_alloc)]
46    } else {
47        vec![]
48    }
49}