Skip to main content

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