DbUp / DbUp

DbUp is a .NET library that helps you to deploy changes to SQL Server databases. It tracks which SQL scripts have been run already, and runs the change scripts that are needed to get your database up to date.

Home Page:http://dbup.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The default character for Variable Substitution clashes with a common PostgreSQL idiom

ebellani opened this issue · comments

It is possible to nest dollar-quoted string constants by choosing different tags at each nesting level. This is most commonly used in writing function definitions. For example:

$function$
BEGIN
    RETURN ($1 ~ $q$[\t\r\n\v\\]$q$);
END;
$function$

source

Create or replace FUNCTION fff(p1 int)
  returns void
  LANGUAGE  plpgsql
AS
$$ --<< outer level quote
DECLARE
 v_Qry  VARCHAR(4000);
BEGIN
  v_Qry := format(
$string$ --<< quote for the string constant passed to the format function
    Create or replace FUNCTION fff_DYNAMIC_SQL()
       returns void
       LANGUAGE  plpgsql
    AS 
    $f1$ --<< quoting inside the actual function body
    DECLARE
      v1  INTEGER;
    begin
      v1 := %s;
      RETURN;
    END; 
    $f1$
$string$, p1);
  EXECUTE v_Qry;
  RETURN;
END; 
$$;

source

The following snippet demonstrates turning off the variable substitution altogether...

      var builder =
          DeployChanges.To
              .PostgresqlDatabase(connectionString)
              .WithScriptsEmbeddedInAssembly(Assembly ?? AppInfo.AppAssembly)
              .JournalToPostgresqlTable("public", "versionTable");

      // This line should substitute an instance of the ScriptExecutor that doesn't do variable substitution
      builder.Configure(c => c.ScriptExecutor = new PostgresqlScriptExecutor(() => c.ConnectionManager, () => c.Log, null, () => false, c.ScriptPreprocessors, () => c.Journal));
      var upgrader = builder.Build();