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
// Copyright Materialize, Inc. and contributors. All rights reserved.
//
// Use of this software is governed by the Business Source License
// included in the LICENSE file.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0.

use mz_stash_types::upgrade::{objects_v37, objects_v38};

use crate::upgrade::MigrationAction;
use crate::{StashError, Transaction, TypedCollection};

/// Update fingerprint of builtin types to have timestsamp with precision
pub async fn upgrade(tx: &Transaction<'_>) -> Result<(), StashError> {
    const GID_MAPPING_COLLECTION: TypedCollection<
        objects_v37::GidMappingKey,
        objects_v37::GidMappingValue,
    > = TypedCollection::new("system_gid_mapping");

    GID_MAPPING_COLLECTION
        .migrate_to(tx, |entries| {
            let mut updates = Vec::new();

            for (key, value) in entries {
                let new_key: objects_v38::GidMappingKey = gid_mapping_key_from(key.clone());
                let new_fingerprint = value
                    .fingerprint
                    .replace("\"TimestampTz\"", "{\"TimestampTz\":{\"precision\":null}}");
                let new_value: objects_v38::GidMappingValue = objects_v38::GidMappingValue {
                    id: value.id,
                    fingerprint: new_fingerprint,
                };
                updates.push(MigrationAction::Update(key.clone(), (new_key, new_value)));
            }

            updates
        })
        .await?;
    Ok(())
}

fn gid_mapping_key_from(key: objects_v37::GidMappingKey) -> objects_v38::GidMappingKey {
    objects_v38::GidMappingKey {
        schema_name: key.schema_name,
        object_type: key.object_type,
        object_name: key.object_name,
    }
}