mz_testdrive/action/
skip_if.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 anyhow::{Context, bail};
11use tokio_postgres::types::Type;
12
13use crate::action::{ControlFlow, State};
14use crate::parser::BuiltinCommand;
15
16pub async fn run_skip_if(cmd: BuiltinCommand, state: &State) -> Result<ControlFlow, anyhow::Error> {
17    let query = cmd.input.join("\n");
18    let stmt = state
19        .materialize
20        .pgclient
21        .prepare(&query)
22        .await
23        .context("failed to prepare skip-if query")?;
24
25    if stmt.columns().len() != 1 || *stmt.columns()[0].type_() != Type::BOOL {
26        bail!("skip-if query must return exactly one boolean column");
27    }
28
29    let should_skip: bool = state
30        .materialize
31        .pgclient
32        .query_one(&stmt, &[])
33        .await
34        .context("executing skip-if query failed")?
35        .get(0);
36
37    if should_skip {
38        println!("skip-if query returned true; skipping until next skip-end or end of the file");
39        Ok(ControlFlow::SkipBegin)
40    } else {
41        println!("skip-if query returned false; continuing");
42        Ok(ControlFlow::Continue)
43    }
44}