Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ebellani opened this issue Jun 21, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@ebellani
Copy link

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

@SprinterDave
Copy link

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();

@droyad droyad transferred this issue from DbUp/DbUp Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants