1use anyhow::Context;
2
3use crate::db::Conn;
4
5pub fn set_up_helpers(db: &mut dyn Conn, target_migration: &str) -> anyhow::Result<()> {
6 let query = format!(
7 "
8 CREATE OR REPLACE FUNCTION reshape.is_new_schema()
9 RETURNS BOOLEAN AS $$
10 DECLARE
11 setting TEXT := current_setting('reshape.is_new_schema', TRUE);
12 setting_bool BOOLEAN := setting IS NOT NULL AND setting = 'YES';
13 BEGIN
14 RETURN current_setting('search_path') = 'migration_{}' OR setting_bool;
15 END
16 $$ language 'plpgsql';
17 ",
18 target_migration,
19 );
20 db.query(&query)
21 .context("failed creating helper function reshape.is_new_schema()")?;
22
23 Ok(())
24}
25
26pub fn tear_down_helpers(db: &mut dyn Conn) -> anyhow::Result<()> {
27 db.query("DROP FUNCTION IF EXISTS reshape.is_new_schema;")?;
28 Ok(())
29}