spring-projects / spring-data-r2dbc

Provide support to increase developer productivity in Java when using Reactive Relational Database Connectivity. Uses familiar Spring concepts such as a DatabaseClient for core API usage and lightweight repository style data access.
Apache License 2.0
709 stars 132 forks source link

Create automatically tables at startup #748

Open mmaryo opened 2 years ago

mmaryo commented 2 years ago

Hello

It looks like tables are not created Postgres stay empty

Even with spring.jpa.hibernate.ddl-auto=create

This is my entity

@Table(schema = "common", name = "\"Exchanges\"")
data class Exchanges (
    @Column("ID") @Id var id: Int = 0,
    @Column("KaikoExchangeCode") var kaikoExchangeCode: String? = null,
)

And the repository

interface ExchangesRepository : R2dbcRepository<Exchanges, Long>

I use Spring Boot 3.0.0.M1, WebFlux, Jdk 17, Kotin, R2DBC

Do you have an idea? How to debug and find a solution?

EDIT Also I try to connect to H2 db spring.r2dbc.url=r2dbc:h2:mem:///testdb Connection is done But test end with

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "COMMON" not found; SQL statement:
SELECT common."Exchanges".* FROM common."Exchanges" [90079-210]

That means the table is not created at test startup

mmaryo commented 2 years ago

I did a little project Please find the attachment

MappingTest.zip

This test

@SpringBootTest
class OneTest {
    @Autowired
    lateinit var exchangesRepository: ExchangesRepository
    @Test
    fun test() {
        exchangesRepository.save(Exchanges(44, "66")).block()
        val res = exchangesRepository.findAll().collectList().block()
        println(res)
    }

}

Throw this error

org.springframework.r2dbc.BadSqlGrammarException: execute; bad SQL grammar [UPDATE common."ExchangesCodesMapping" SET ExchangeCode = $1 WHERE common."ExchangesCodesMapping".ID = $2]; nested exception is io.r2dbc.spi.R2dbcBadGrammarException: [90079] [90079] Schema "COMMON" not found; SQL statement:
UPDATE common."ExchangesCodesMapping" SET ExchangeCode = $1 WHERE common."ExchangesCodesMapping".ID = $2 [90079-210]

Notice when I run the server to an existing database ExchangesRepository works very well It seems this code is unable to create the table at startup

mmaryo commented 2 years ago

It seems that is related to NamingStrategy How to override it ? I need to add a double quote and keep the case of string inside @Column("Name")

mmaryo commented 2 years ago
open class R2dbcConfiguration : AbstractR2dbcConfiguration() {
    override fun connectionFactory(): ConnectionFactory {
        throw ExecutionControl.NotImplementedException("No need")
    }

    override fun r2dbcMappingContext(namingStrategy: Optional<NamingStrategy>, r2dbcCustomConversions: R2dbcCustomConversions): R2dbcMappingContext {
        val context = super.r2dbcMappingContext(namingStrategy, r2dbcCustomConversions)
        context.isForceQuote = true
        return context
    }
}
linghengqian commented 2 years ago

spring.jpa.hibernate.ddl-auto is completely unrelated to R2DBC

Kabir1506 commented 1 year ago

@mmaryo did you find any solution to this?

franciskinyuru commented 1 year ago

Below worked for me in a configuration class and below bean @Bean public ConnectionFactoryInitializer connectionFactoryInitializer(ConnectionFactory connectionFactory) { ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer(); initializer.setConnectionFactory(connectionFactory); ResourceDatabasePopulator populator = new ResourceDatabasePopulator(); populator.addScript(new ClassPathResource("schema.sql")); initializer.setDatabasePopulator(populator); return initializer; }

Then have you schema.sql in the resources folder

Finally add below in your application.properties or yml file spring.resources.static-locations=classpath:/,classpath:/resources/