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
// 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_v38 as v38, objects_v39 as v39};

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

wire_compatible!(v38::RoleKey with v39::RoleKey);
wire_compatible!(v38::RoleAttributes with v39::RoleAttributes);
wire_compatible!(v38::RoleMembership with v39::RoleMembership);

const ROLES_COLLECTION: TypedCollection<v38::RoleKey, v38::RoleValue> =
    TypedCollection::new("role");

/// Update all Roles to contains an empy set of RoleVars.
pub async fn upgrade(tx: &Transaction<'_>) -> Result<(), StashError> {
    ROLES_COLLECTION
        .migrate_to(tx, |entries| {
            let mut updates = Vec::with_capacity(entries.len());

            for (key, value) in entries {
                let new_key: v39::RoleKey = WireCompatible::convert(key);
                let new_value = v39::RoleValue {
                    name: value.name.clone(),
                    attributes: value.attributes.as_ref().map(WireCompatible::convert),
                    membership: value.membership.as_ref().map(WireCompatible::convert),
                    //
                    // ** MIGRATION ** Adding a default RoleVars message to all types.
                    //
                    vars: Some(v39::RoleVars::default()),
                };

                updates.push(MigrationAction::Update(key.clone(), (new_key, new_value)));
            }

            updates
        })
        .await?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Stash;

    const ROLES_COLLECTION_V39: TypedCollection<v39::RoleKey, v39::RoleValue> =
        TypedCollection::new("role");

    #[mz_ore::test(tokio::test)]
    #[cfg_attr(miri, ignore)] // unsupported operation: can't call foreign function `TLS_client_method` on OS `linux`
    async fn smoke_test_role_vars_added() {
        Stash::with_debug_stash(|mut stash| async move {
            ROLES_COLLECTION
                .insert_without_overwrite(
                    &mut stash,
                    vec![
                        (
                            v38::RoleKey {
                                id: Some(v38::RoleId {
                                    value: Some(v38::role_id::Value::System(1)),
                                }),
                            },
                            v38::RoleValue {
                                name: "mz_system".to_string(),
                                attributes: Some(v38::RoleAttributes { inherit: true }),
                                membership: None,
                            },
                        ),
                        (
                            v38::RoleKey {
                                id: Some(v38::RoleId {
                                    value: Some(v38::role_id::Value::User(42)),
                                }),
                            },
                            v38::RoleValue {
                                name: "parker".to_string(),
                                attributes: None,
                                membership: Some(v38::RoleMembership {
                                    map: Default::default(),
                                }),
                            },
                        ),
                        (
                            v38::RoleKey {
                                id: Some(v38::RoleId {
                                    value: Some(v38::role_id::Value::Public(Default::default())),
                                }),
                            },
                            v38::RoleValue {
                                name: "public".to_string(),
                                attributes: Some(v38::RoleAttributes { inherit: false }),
                                membership: Some(v38::RoleMembership {
                                    map: Default::default(),
                                }),
                            },
                        ),
                    ],
                )
                .await
                .expect("insert success");

            // Run the migration.
            stash
                .with_transaction(|tx| {
                    Box::pin(async move {
                        upgrade(&tx).await?;
                        Ok(())
                    })
                })
                .await
                .unwrap();

            let roles = ROLES_COLLECTION_V39
                .peek_one(&mut stash)
                .await
                .expect("read v39");
            insta::assert_debug_snapshot!(roles, @r###"
        {
            RoleKey {
                id: Some(
                    RoleId {
                        value: Some(
                            System(
                                1,
                            ),
                        ),
                    },
                ),
            }: RoleValue {
                name: "mz_system",
                attributes: Some(
                    RoleAttributes {
                        inherit: true,
                    },
                ),
                membership: None,
                vars: Some(
                    RoleVars {
                        entries: [],
                    },
                ),
            },
            RoleKey {
                id: Some(
                    RoleId {
                        value: Some(
                            User(
                                42,
                            ),
                        ),
                    },
                ),
            }: RoleValue {
                name: "parker",
                attributes: None,
                membership: Some(
                    RoleMembership {
                        map: [],
                    },
                ),
                vars: Some(
                    RoleVars {
                        entries: [],
                    },
                ),
            },
            RoleKey {
                id: Some(
                    RoleId {
                        value: Some(
                            Public(
                                Empty,
                            ),
                        ),
                    },
                ),
            }: RoleValue {
                name: "public",
                attributes: Some(
                    RoleAttributes {
                        inherit: false,
                    },
                ),
                membership: Some(
                    RoleMembership {
                        map: [],
                    },
                ),
                vars: Some(
                    RoleVars {
                        entries: [],
                    },
                ),
            },
        }
        "###);
        })
        .await
        .unwrap();
    }
}