question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TC_INITSCRIPT can't handle PostgreSQL dollar quoted strings

See original GitHub issue

A simple program like this:

public static void main(String[] args) throws SQLException {
    Properties properties = new Properties();
    properties.setProperty("username", "postgres");
    properties.setProperty("password", "postgres");

    new ContainerDatabaseDriver().connect(
        "jdbc:tc:postgresql:13:///sakila?TC_TMPFS=/testtmpfs:rw&TC_INITSCRIPT=file:c:/temp/dollar-quoted.sql",
        properties
    );
}

Where the dollar-quoted.sql file contains:

CREATE FUNCTION f ()
RETURNS INT
AS $$
BEGIN
    RETURN 1;
END;
$$ LANGUAGE plpgsql;

Fails with this exception:

Exception in thread "main" org.testcontainers.ext.ScriptUtils$UncategorizedScriptException: Failed to execute database script from resource [CREATE FUNCTION f ()
RETURNS INT
AS $$
BEGIN
    RETURN 1;
END;
$$ LANGUAGE plpgsql;
]
	at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:375)
	at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:313)
	at org.testcontainers.jdbc.ContainerDatabaseDriver.runInitScriptIfRequired(ContainerDatabaseDriver.java:196)
	at org.testcontainers.jdbc.ContainerDatabaseDriver.connect(ContainerDatabaseDriver.java:132)
	at org.jooq.example.test.containers.TestContainersTest.main(TestContainersTest.java:34)
Caused by: org.testcontainers.ext.ScriptUtils$ScriptStatementFailedException: Script execution failed (file:c:/temp/dollar-quoted.sql:1): CREATE FUNCTION f () RETURNS INT AS $$ BEGIN
    RETURN 1;
END
	at org.testcontainers.jdbc.JdbcDatabaseDelegate.execute(JdbcDatabaseDelegate.java:49)
	at org.testcontainers.delegate.AbstractDatabaseDelegate.execute(AbstractDatabaseDelegate.java:34)
	at org.testcontainers.ext.ScriptUtils.executeDatabaseScript(ScriptUtils.java:362)
	... 4 more
Caused by: org.postgresql.util.PSQLException: Unterminated dollar quote started at position 36 in SQL CREATE FUNCTION f () RETURNS INT AS $$ BEGIN
    RETURN 1;
END. Expected terminating $$
	at org.postgresql.core.Parser.checkParsePosition(Parser.java:1328)
	at org.postgresql.core.Parser.parseSql(Parser.java:1227)
	at org.postgresql.core.Parser.replaceProcessing(Parser.java:1179)
	at org.postgresql.core.CachedQueryCreateAction.create(CachedQueryCreateAction.java:43)
	at org.postgresql.core.QueryExecutorBase.createQueryByKey(QueryExecutorBase.java:337)
	at org.postgresql.jdbc.PgStatement.executeCachedSql(PgStatement.java:300)
	at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:284)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:279)
	at org.testcontainers.jdbc.JdbcDatabaseDelegate.execute(JdbcDatabaseDelegate.java:42)
	... 6 more

It seems that the logic borrowed from Spring’s ScriptUtils doesn’t handle real world SQL scripts very well? I can think of numerous other issues that aren’t currently supported, including e.g. MySQL’s DELIMITER statement, etc.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
lukasedercommented, Sep 15, 2021

Obviously, like many features, this one is an endless one that’s never finished, given how many different SQL script interpreters and preprocessors there are… I get your arguments here. But people would probably love running their pg_dump generated scripts too…

0reactions
membersoundcommented, Feb 8, 2022

Also stumbled upon this today. It would be great it stored procedures/functions would support DELIMITER in testcontainers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

PostgreSQL Dollar-Quoted Strings Constants to Prevent SQL ...
I know the best was to handle dynamic queries is to have them generated in a application layer with a parametrized query, that's...
Read more >
PostgreSQL - Dollar-Quoted String Constants - GeeksforGeeks
The problem arises when the string constant contains many single quotes and backslashes. Doubling every single quote and backslash makes the ...
Read more >
Documentation: 15: 4.1. Lexical Structure - PostgreSQL
The tag, if any, of a dollar-quoted string follows the same rules as an unquoted identifier, except that it cannot contain a dollar...
Read more >
Dollar-Quoted String Constants - PostgreSQL Tutorial
Introduction the dollar-quoted string constant syntax. In PostgreSQL, you use single quotes for a string constant like this: select 'String constant';.
Read more >
Equivalent to PostgreSQL's Dollar-quoted String Constants for ...
This is a PostgreSQL extension of the spec. There is nothing like it in SQL Server. The idea is that you can quote...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found