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