flapdoodle-oss / de.flapdoodle.embed.mongo.spring

embedded mongo spring integration
Apache License 2.0
30 stars 7 forks source link

How to customize the Mongod configuration properly in a Spring Boot context? #11

Closed enolive closed 1 year ago

enolive commented 1 year ago

I am struggling to find a proper way to customize the embedded mongo.

I am looking for a way to override

I am aware of the Customizations described in the Howto Document but I am not sure if I adapted it correctly to a Spring Boot Application. I am also aware that I can easily set the mongo version with the property de.flapdoodle.mongodb.embedded.version

  @Bean
  fun mongoConfiguration(net: Net): Mongod {
    return Mongod.builder()
      .distributionBaseUrl(Start.to(DistributionBaseUrl::class.java)
        .initializedWith(DistributionBaseUrl.of("https://my-company.site"))
      )
      .persistentBaseDir(Start.to(PersistentDir::class.java)
        .initializedWith(PersistentDir.inWorkingDir("target/embeddedmongo").get())
      )
      .net(Start.to(Net::class.java).initializedWith(net))
      .build()
  }

Problems that I see:

as you might see, I am using Kotlin. I can also provide a Java sample instead if needed!

michaelmosmann commented 1 year ago

@enolive thats one way to go ..

you can set the base dir also with an env variable as you can see here: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/blob/main/src/main/java/de/flapdoodle/embed/mongo/transitions/ExtractFileSet.java#L60

You can also use:

Start.to(PersistenDir::class.java) .providedBy(PersistentDir.inWorkingDir(".embeddedMongodbCustomPath") .mapToUncheckedException(RuntimeException::new))

.. this way it will be evaluated later.. and you must handle a possible IOException .. in this case it will be mapped to a RuntimeException ..

There are at least two options .. a) use the builder pattern, b) use method overrides.. but i had some strange case, where this did not work in one case.. so builder pattern should work..

michaelmosmann commented 1 year ago

@enolive spring 3.x changed some internals .. i have to find a way to restore this port handling from 2.7.x somehow...

enolive commented 1 year ago

thanks for your helpful pointers!

While the ENV variable solution would be a very elegant solution to my nasty CI/CD problem, it would force me to scatter the embedded mongo configuration to different locations which I'd like to avoid.

I like the .mapToUncheckedException(...) operation, but it seems to depend on a later version of de.flapdoodle.embed.mongo:4.3.* than the one specified as the peer dependency of *.spring30x:4.3.2 (same goes for the 4.3.3-SNAPSHOT version I find here in the repo). I want to avoid messing around with overriden peer dependencies if possible. An updated version of spring30x with new peer dependencies would be really helpful to me :wink:

michaelmosmann commented 1 year ago

@enolive there will be a new version soon ..

mcordeiro73 commented 1 year ago

I am similarly looking for a way to configure the storage directory of the embedded database. With Spring's old implementation this used to be accomplished by setting spring.mongodb.embedded.storage.databaseDir.

I tried de.flapdoodle.mongodb.embedded.storage.databaseDir but it appears that had no affect.

michaelmosmann commented 1 year ago

@mcordeiro73 ah.. ok. I think i can reimplement this feature ..

michaelmosmann commented 1 year ago

@mcordeiro73 feature is back as you can see here: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/blob/main/HowTo.md#custom-database-dir .. you must use de.flapdoodle.mongodb.embedded.databaseDir (backport to my spring3.0.x, spring2.7.x and spring2.6.x adapter) ..

can you close this, if this works for you? .. and if not, please open an new issue:)

mcordeiro73 commented 1 year ago

@michaelmosmann The property worked great. Not sure I can close the issue since I didn't open it. @enolive would you like to do the honors?

michaelmosmann commented 1 year ago

@mcordeiro73 @enolive .. so i will close this:)