-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Invalid parsing of SQL Init script containing procedures #570
Comments
Hmm, sorry for not responding sooner. Our I just need to get my head around this; I'm not clear if there is a suitable workaround given there. I'm afraid you've also been unlucky with timing; we merged a change earlier that moves |
OK, none of the options in the Spring ticket sound like they'll work for us. I've coded up a quick experiment to make ScriptUtils aware of BEGIN...END compound statements, and to treat them atomically. This actually works without any script modification, but I'm mildly hesitant and want to think this through. I don't want there to be a risk of breaking existing functionality, and it is potentially a risk here. |
Thank you @rnorth for looking into this. Timing seems to be really bad for me :). I see you are considering the occurrences of BEGIN and END, I am not really sure how that might work for the nested BEGIN .. END occurrences. But for the plain simple procedure, that might work. Is there a snapshot version available with the code you added? I tried with the solution in my PR (local snapshot) and that worked for me. If you think that could be a viable solution, I can rebase my PR. |
I'm not sure; doesn't By detecting BEGIN/END blocks I've attempted to keep us fully compatible with unmodified SQL. I think/hope the way I've done it also handles nested blocks well enough too; to be sure, I've updated the |
BTW if you could perhaps try it out, there's a jitpack.io build available: You'll need the jitpack repository added to your POM: <repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories> and replace any testcontainers dependencies you have with this (leaving <dependency>
<groupId>com.github.testcontainers.testcontainers-java</groupId>
<artifactId>MODULE_NAME_GOES_HERE</artifactId>
<version>experimental-sql-compound-statement-awareness-SNAPSHOT</version>
</dependency> |
I think you are right @rnorth. Using escape character would make script unusable elsewhere. I would prefer to use the production-ready, unmodified scripts during testing. So, using escape literal is not a good solution. I tried the jitpack version as you mentioned and it seemed to work perfectly. Here is my modified version of test procedure script -
There I intentionally added a nested Now for my usage, I am not sure when this fix will be released as new TestContainers-Java version. But I am sure, I am not the only one who would love to have this one as soon as possible released :). If you think this will take a while to release, then I am thinking to extract out ScriptUtils of 1.6.0 (like this) in my code, add this code fix in it (current extract code has escape literal fix, but would remove it) and run init script after db container is started (something like this). I hope that would be ok with you and of course I would switch to the new version as soon as it is released and let TC handle that initialization. Thank you for the help! |
Prevents compound statements from being naively split by ScriptUtils
Prevents compound statements from being naively split by ScriptUtils
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you believe this is a mistake, please reply to this comment to keep it open. If there isn't one already, a PR to fix or at least reproduce the problem in a test case will always help us get back on track to tackle this. |
This issue has been automatically closed due to inactivity. We apologise if this is still an active problem for you, and would ask you to re-open the issue if this is the case. |
Reopening- this is still an issue. |
Prevents compound statements from being naively split by ScriptUtils
Released in 1.10.4! |
Bumps [neo4j-java-driver](https://github.com/neo4j/neo4j-java-driver) from 1.7.2 to 1.7.3. <details> <summary>Commits</summary> - [`742d280`](neo4j/neo4j-java-driver@742d280) Merge pull request [#567](https://github-redirect.dependabot.com/neo4j/neo4j-java-driver/issues/567) from ali-ince/1.7-pass-access-mode - [`49ef4db`](neo4j/neo4j-java-driver@49ef4db) Fixing wrong file header - [`f66e194`](neo4j/neo4j-java-driver@f66e194) Merge pull request [#570](https://github-redirect.dependabot.com/neo4j/neo4j-java-driver/issues/570) from zhenlineo/1.7-flaky-test - [`5b58956`](neo4j/neo4j-java-driver@5b58956) Fixing the flaky test that is caused by certificate file changes. - [`f9a6fca`](neo4j/neo4j-java-driver@f9a6fca) Merge pull request [#569](https://github-redirect.dependabot.com/neo4j/neo4j-java-driver/issues/569) from michael-simons/fix-java-doc-trust-strategy - [`bc68f0f`](neo4j/neo4j-java-driver@bc68f0f) Merge pull request [#566](https://github-redirect.dependabot.com/neo4j/neo4j-java-driver/issues/566) from zhenlineo/1.7-hostname-for-sni - [`41949fc`](neo4j/neo4j-java-driver@41949fc) Fix JavaDoc of withTrustStrategy. - [`227c0fc`](neo4j/neo4j-java-driver@227c0fc) Fix test failure - [`11345c1`](neo4j/neo4j-java-driver@11345c1) Pass access mode as part of statement metadata - [`e9e5a93`](neo4j/neo4j-java-driver@e9e5a93) Fix some test failures due to changes to routing table procedure on read-repl... - Additional commits viewable in [compare view](neo4j/neo4j-java-driver@1.7.2...1.7.3) </details> <br /> [![Dependabot compatibility score](https://api.dependabot.com/badges/compatibility_score?dependency-name=org.neo4j.driver:neo4j-java-driver&package-manager=gradle&previous-version=1.7.2&new-version=1.7.3)](https://dependabot.com/compatibility-score.html?dependency-name=org.neo4j.driver:neo4j-java-driver&package-manager=gradle&previous-version=1.7.2&new-version=1.7.3) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) If all status checks pass Dependabot will automatically merge this pull request. [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot merge` will merge this PR after your CI passes on it - `@dependabot squash and merge` will squash and merge this PR after your CI passes on it - `@dependabot cancel merge` will cancel a previously requested merge and block automerging - `@dependabot reopen` will reopen this PR if it is closed - `@dependabot ignore this [patch|minor|major] version` will close this PR and stop Dependabot creating any more for this minor/major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) - `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme Additionally, you can set the following in the `.dependabot/config.yml` file in this repo: - Update frequency (including time of day and day of week) - Automerge options (never/patch/minor, and dev/runtime dependencies) - Pull request limits (per update run and/or open at any time) - Out-of-range updates (receive only lockfile updates, if desired) - Security updates (receive only security updates, if desired) Finally, you can contact us by mentioning @dependabot. </details>
Was this really solved... can someone verify it, I am using 1.11.2 and still getting this issue when trying to create procedures... |
@Ordiel Can you please provide a non-working example script? |
Will try to roll back to that non-working version (I am using Liquibase anyway, so I thought "why not just get the connection and let Liquibase do the init?) |
Not working here. Running version 1.11.3 mysql testcontainers. |
Not working on 1.11.3 postgresql as well :( |
1.12.0, released 15 days ago, includes a number of further fixes for SQL script splitting. This may resolve your problem - please try it. If it does not, then please provide examples of scripts that fail - otherwise there is nothing we can do. For what it's worth, @Ordiel's approach of using Liquibase (or Flyway, or any other DB migration tool) is sensible - these tools will always have stronger, more specialised SQL loading capabilities than we can afford to maintain. |
1.12.0 Not working with |
@rnorth |
@rnorth for my tests all work without init script splitting (fix in ScriptUtils) |
A workaround I found was creating my own implementation of |
PostgreSQLContainer postgres = new PostgreSQLContainer<>();
postgres.setImage(future); works too :) |
@bsideup Thanks for that, hehe, now my code will be leaner. |
Hi guys, I'm having some issues when using IF statement, it seems that it is assuming the word END in the "END IF" as the END of the BEGIN statement. CREATE OR REPLACE PROCEDURE updatealladvertisers() LANGUAGE plpgsql AS $$ BEGIN IF (SELECT count(1) FROM information_schema.tables WHERE table_schema = 'public' AND table_name='all_advertisers') = 0 THEN CREATE TABLE IF NOT EXISTS public.all_advertisers ( partnerid BIGINT, partnername VARCHAR(255), partner VARCHAR(255), gsm_id BIGINT ); END IF; END $$; |
Hi, |
We're facing this issue with testcontainers-java and Postgres 11.4. |
For those, who struggles with this issue and MS-SQL. As @vipulnewaskar7 suggested, I've tried to run init script from within container:
Finally, java-side config for singleton container looks like
|
I'm trying to run a postgres script and keep hitting this issue with Testcontainers 1.15.2... @Bean
public DataSource dataSource() {
if (POSTGRESQL_CONTAINER == null) {
PostgreSQLContainer<?> container = new PostgreSQLContainer<>(parse("postgres").withTag("9.6.12")) //
.withUsername("postgres") //
.withInitScript("scripts/postgres-stored-procedures.sql");
container.start();
POSTGRESQL_CONTAINER = container;
}
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setUrl(POSTGRESQL_CONTAINER.getJdbcUrl());
dataSource.setUser(POSTGRESQL_CONTAINER.getUsername());
dataSource.setPassword(POSTGRESQL_CONTAINER.getPassword());
return dataSource;
} This fails when I try to run a postgres script found at https://github.com/GabrielBB/spring-data-jpa-procedure-tests/blob/master/src/main/resources/postgres.sql. Why, again, is the issue closed? |
The only thing I could get to work was this: @BeforeEach
@Transactional
void setUp() {
Resource initScript = new ClassPathResource("scripts/postgres-stored-procedures.sql");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
try (Reader reader = new InputStreamReader(initScript.getInputStream(), StandardCharsets.UTF_8)) {
jdbcTemplate.execute(FileCopyUtils.copyToString(reader));
} catch (IOException e) {
throw new RuntimeException(e);
}
} This lets me grab an existing |
With 1.16.0 having either the same or similar issue. When I do
I get
But when I do
Everything works as expected. The same file also works correctly with docker-compose. |
The first example will use Testcontainers' There is always a chance for edge cases not being supported by |
Yes, I understand that they work differently, but I'd argue it shouldn't matter to the end-user how is the database initialized, as long as it's done. 😉
|
I can understand the argument from a user perspective, but from a maintainer perspective, Testcontainers simply can't support all SQL dialects to 100%. I think relying on the script execution by the mechanisms provided by the image (having a file in Note that removing the semicolon after |
My example was purely synthetic obviously, and I have no control over the DDLs in the real init file, so I can't really use |
Since this feature of the PostgreSQL image is documented on its Docker Hub Page, I don't see the need to replicate this in the Testcontainers docs. However, I agree that it might make sense to add an info block to the Testcontainers docs that mentions possible limitations of using the provided init-script functions. |
this issue is still actual for oracle at least |
I have a very simple init sql script for MySQL that is provided to TC JDBC URL using TC_INITSCRIPT parameter -
When TestContainer starts,
ScriptUtils
class splits the statements by default;
delimiter. This results in last CREATE PROCEDURE statement to break after inline;
and the SQL Statement created by utils looks like below which is incorrect and hence failsException:
Possible solution:
When
;
inside procedure are escaped\;
then CREATE PROCEDURE block is read completely but again the execution of that SQL fails because ; is an invalid delimiter at runtime. Before executing the statement, should\;
be replaced with;
?Or I am doing it wrong?
The text was updated successfully, but these errors were encountered: