Skip to main content

mz_catalog_protos/
lib.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
10//! All types we durably persist in the Catalog.
11
12pub mod objects;
13pub mod objects_v74;
14pub mod objects_v75;
15pub mod objects_v76;
16pub mod objects_v77;
17pub mod objects_v78;
18pub mod objects_v79;
19pub mod objects_v80;
20pub mod objects_v81;
21pub mod objects_v82;
22pub mod objects_v83;
23pub mod objects_v84;
24pub mod objects_v85;
25pub mod objects_v86;
26pub mod objects_v87;
27pub mod objects_v88;
28pub mod objects_v89;
29pub mod objects_v90;
30pub mod objects_v91;
31pub mod objects_v92;
32pub mod objects_v93;
33
34impl From<String> for objects::StringWrapper {
35    fn from(value: String) -> Self {
36        objects::StringWrapper { inner: value }
37    }
38}
39
40/// The current version of the `Catalog`.
41///
42/// We will initialize new `Catalog`s with this version, and migrate existing `Catalog`s to this
43/// version. Whenever the `Catalog` changes, e.g. the types we serialize in the `Catalog`
44/// change, we need to bump this version.
45pub const CATALOG_VERSION: u64 = 93;
46
47/// The minimum `Catalog` version number that we support migrating from.
48///
49/// After bumping this we can delete the old migrations.
50pub const MIN_CATALOG_VERSION: u64 = 74;
51
52#[cfg(test)]
53mod tests {
54    use std::collections::BTreeSet;
55    use std::fs;
56    use std::path::PathBuf;
57
58    use crate::{CATALOG_VERSION, MIN_CATALOG_VERSION};
59
60    #[mz_ore::test]
61    fn test_assert_snapshots_exist() {
62        let src_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "src"].iter().collect();
63
64        // Get all the versioned object definition files.
65        let mut filenames: BTreeSet<_> = fs::read_dir(src_dir)
66            .expect("failed to read src dir")
67            .map(|entry| entry.expect("failed to read dir entry").file_name())
68            .map(|filename| filename.to_str().expect("utf8").to_string())
69            .filter(|filename| filename.starts_with("objects_v"))
70            .collect();
71
72        // Assert snapshots exist for all of the versions we support.
73        for version in MIN_CATALOG_VERSION..=CATALOG_VERSION {
74            let filename = format!("objects_v{version}.rs");
75            assert!(
76                filenames.remove(&filename),
77                "Missing snapshot for v{version}."
78            );
79        }
80
81        // Common case. Check to make sure the user bumped the CATALOG_VERSION.
82        if !filenames.is_empty()
83            && filenames.remove(&format!("objects_v{}.proto", CATALOG_VERSION + 1))
84        {
85            panic!(
86                "Found snapshot for v{}, please also bump `CATALOG_VERSION`.",
87                CATALOG_VERSION + 1
88            )
89        }
90
91        // Assert there aren't any extra snapshots.
92        assert!(
93            filenames.is_empty(),
94            "Found snapshots for unsupported catalog versions {filenames:?}.\n\
95             If you just increased `MIN_CATALOG_VERSION`, then please delete the old snapshots. \
96             If you created a new snapshot, please bump `CATALOG_VERSION`."
97        );
98    }
99
100    #[mz_ore::test]
101    fn test_assert_current_snapshot() {
102        let src_dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "src"].iter().collect();
103        let current_rs = src_dir.join("objects.rs");
104        let snapshot_rs = src_dir.join(format!("objects_v{CATALOG_VERSION}.rs"));
105
106        let current = fs::read_to_string(current_rs).expect("read current");
107        let snapshot = fs::read_to_string(snapshot_rs).expect("read snapshot");
108
109        // Note: objects.rs and objects_v<CATALOG_VERSION>.rs should be exactly the same. The
110        // reason being, when bumping the catalog to the next version, CATALOG_VERSION + 1, we need a
111        // snapshot to migrate _from_, which should be a snapshot of how the types are today.
112        // Hence why the two files should be exactly the same.
113        similar_asserts::assert_eq!(current, snapshot);
114    }
115}