Skip to content

feat(postgres): add WithOrderedInitScripts for Postgres testcontainers#3093

Closed
AlisCode wants to merge 3 commits intotestcontainers:mainfrom
AlisCode:main
Closed

feat(postgres): add WithOrderedInitScripts for Postgres testcontainers#3093
AlisCode wants to merge 3 commits intotestcontainers:mainfrom
AlisCode:main

Conversation

@AlisCode
Copy link
Contributor

@AlisCode AlisCode commented Apr 8, 2025

Type of change: Enhancement

What does this PR do?

As suggested in the discussion, this adds a brand new option to add init scripts to your Postgres testcontainer, and run them in the order that the user provides them to the function.

It also further documents WithInitScripts to clarify in what order the scripts are executed.

Why is it important?

The files that are passed to WithInitScripts are copied inside the container, then run in alphanumerical order. I believe this is surprising behavior, as I expect the init scripts to be run in the order I passed them to testcontainers.

Someone ran into this behavior before and raised an issue : #2542

Related issues

How to test this PR

Try using the newly-added function WithOrderedInitScripts for the postgres container.
Any setup using WithInitScripts should work just like before.

@AlisCode AlisCode requested a review from a team as a code owner April 8, 2025 16:42
@netlify
Copy link

netlify bot commented Apr 8, 2025

Deploy Preview for testcontainers-go ready!

Name Link
🔨 Latest commit 59f7cf7
🔍 Latest deploy log https://app.netlify.com/sites/testcontainers-go/deploys/67ff5b04ffe99a0008096d65
😎 Deploy Preview https://deploy-preview-3093--testcontainers-go.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@AlisCode AlisCode changed the title Add WithOrderedInitScripts for Postgres testcontainers feat: Add WithOrderedInitScripts for Postgres testcontainers Apr 8, 2025
@mdelapenya mdelapenya changed the title feat: Add WithOrderedInitScripts for Postgres testcontainers feat: add WithOrderedInitScripts for Postgres testcontainers Apr 8, 2025
@mdelapenya mdelapenya changed the title feat: add WithOrderedInitScripts for Postgres testcontainers feat(postgres): add WithOrderedInitScripts for Postgres testcontainers Apr 8, 2025
@mdelapenya
Copy link
Member

A first look and this looks good. Could you please add tests verifying the new behavior?

@AlisCode
Copy link
Contributor Author

AlisCode commented Apr 8, 2025

I sure can, would you mind pointing me to a test that I can take inspiration from ?

@mdelapenya
Copy link
Member

Sure, please take as look here:

func TestWithInitScript(t *testing.T) {

@AlisCode
Copy link
Contributor Author

AlisCode commented Apr 9, 2025

Hi! I've added a test that checks that both scripts are executed in the order they are provided. If the behavior changes or becomes inconsistent somehow, and insert-user.sql gets executed first, then the test should break.

@AlisCode AlisCode force-pushed the main branch 2 times, most recently from 0ab0417 to 6fca103 Compare April 9, 2025 11:37
Copy link
Member

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just added some minor comments

Thanks!

return func(req *testcontainers.GenericContainerRequest) error {
for idx, script := range scripts {
filename := filepath.Base(script)
containerFilePath := fmt.Sprintf("%06d-%s", idx, filename)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, not a blocker: let's use a smaller number. Not sure if we need thousands of files 😅. Please update the tests too if agreed

Suggested change
containerFilePath := fmt.Sprintf("%06d-%s", idx, filename)
containerFilePath := fmt.Sprintf("%03d-%s", idx, filename)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Sorry for the noise :)

@AlisCode
Copy link
Contributor Author

@mdelapenya I've taken the time to address all those comments :)

Enjoy!

}

// Adds an initialization script to the Postgres container
func appendInitScript(req *testcontainers.GenericContainerRequest, hostFilePath string, containerFilePath string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, we just merged an option to append container files, I think we can simplify this code here, just calling the WithFiles option: https://golang.testcontainers.org/modules/postgres/#withfiles

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

@AlisCode AlisCode requested a review from mdelapenya April 16, 2025 07:47
}

// Adds the file located at hostFilePath as an init script that postgres will launch
func buildInitScriptContainerFile(hostFilePath string, containerFilePath string) testcontainers.ContainerFile {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: do we need this simple function that just returns the struct? I'd consider removing it as it does not do anything special inside. But I'll let you decide

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You asked to share some code between WithOrderedInitScripts and WithInitScripts, so this function does just that. The only alternative I can think of is duplicating the ContainerFile declaration in the 2 functions, so I believe this is better?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I asked that before having the new WithFiles options, which now removes the duplication. I'm fine having it as a function, although with the current implementation I see it as a pass-through function. In any case, it's not a blocker to me.

Once the docs are added, I'm happy to merge this 😊

Copy link
Member

@mdelapenya mdelapenya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; although I'd like to have the new option documented at docs/modules/postgres.md. Please do not forget adding the Not available... marker 🙏 (https://github.com/testcontainers/testcontainers-go/blob/main/docs/modules/arangodb.md?plain=1#L62)

@mdelapenya
Copy link
Member

@AlisCode friendly ping 🙏

@mdelapenya
Copy link
Member

@AlisCode given this PR is sent from your main branch, I cannot contribute to it with the requested changes.

I'm pushing a new branch with all your commits, adding mine on top, so that you appear as contributor.

@mdelapenya
Copy link
Member

Closing as there is a superseding PR. When a release is done, I'll update the release notes to include your name as the original author of that PR

Thanks for contributing to the project! 🙇

@mdelapenya mdelapenya closed this Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement]: Postgres WithInitScripts files should be run in the order they are being passed

2 participants