Skip to content

Document how to initialize the database when using R2DBC #20524

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

Closed
hantsy opened this issue Mar 13, 2020 · 10 comments
Closed

Document how to initialize the database when using R2DBC #20524

hantsy opened this issue Mar 13, 2020 · 10 comments
Assignees
Labels
type: documentation A documentation update
Milestone

Comments

@hantsy
Copy link

hantsy commented Mar 13, 2020

In a general web flux application, if added spring-boot-starter-data-r2dbc into dependencies, the schema.sql and data.sql in classpath will be executed when the application is started.

In my example, I created a RSocket server that serves at TCP/7000, I am using the following stack:

  • Spring Boot 2.3.0.M3
  • Java 11

The project dependencies.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-rsocket</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-r2dbc</artifactId>
        </dependency>
        <dependency>
            <groupId>io.r2dbc</groupId>
            <artifactId>r2dbc-h2</artifactId>
            <scope>runtime</scope>
        </dependency>

The project application.properties.

spring.rsocket.server.port=7000
spring.rsocket.server.transport=tcp

I have to declare a ConnectionFactoryInitializer bean to initialize the schema.sql and data.sql myself.

    @Bean
    public ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {

        ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
        initializer.setConnectionFactory(connectionFactory);

        CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
        populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("schema.sql")));
        populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("data.sql")));
        initializer.setDatabasePopulator(populator);

        return initializer;
    }

Get the complete sample codes here.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 13, 2020
@wilkinsona
Copy link
Member

When you're using R2DBC, there's no support in Spring Boot for initialising a database using schema.sql or data.sql. This was prototyped in the experimental project but there were a number of problems with it so it did not move over into Spring Boot.

We should clarify the situation by adding some content to the reference documentation. This commit message summarises the situation quite well.

@wilkinsona wilkinsona changed the title ConnectionFactoryInitializer is not instantiated in a RSocket server application Document how to initialize the database when using R2DBC Mar 13, 2020
@wilkinsona wilkinsona added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 13, 2020
@wilkinsona wilkinsona added this to the 2.3.x milestone Mar 13, 2020
@hantsy
Copy link
Author

hantsy commented Mar 13, 2020

OK, thanks, understand it.

I just encountered this problem when I created a new project with Spring Boot 2.3.0.M3 today, the former sample projects are based on 2.2.x and the experimental spring data r2dbc starter. So I thought it could be a problem with the change of the dependencies, not realized there are some changes when spring data r2dbc merged into the official Spring Data and Spring Boot train.

@snicoll
Copy link
Member

snicoll commented Mar 13, 2020

There is a note in the documentation but, reading this again, it's the last paragraph of a quite lengthy section so we should probably reword it. Is that what you have in mind @wilkinsona?

@wilkinsona
Copy link
Member

wilkinsona commented Mar 13, 2020

I missed those two sentences when I went looking for something. Thanks for the pointer. I think we should have a separate section for database initialization with R2DBC and provide some more detailed examples.

I also wonder if we should restructure the whole SQL section a bit. We could mention R2DBC in the opening description and then I think we should have separate JDBC and R2DBC subsections. All of the existing content other than the new R2DBC section would move beneath the JDBC section.

@Thinkenterprise
Copy link

I have the same problem as @hantsy. I switched the Spring Boot Experimental Project to Spring Boot 2.3.M3 and the loading of the schema no longer works. I think, this is probably due to the test autoconfiguration that is also integrated?? But I'm not sure!!

        <dependency>
            <groupId>org.springframework.boot.experimental</groupId>
            <artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
            <scope>test</scope>
        </dependency>

However, there is a workaround for this? Does it make sense to use the DatabaseClient to load a DDL?

It try it

client.execute("CREATE TABLE customer (id SERIAL PRIMARY KEY, first_name VARCHAR(255), last_name VARCHAR(255));").then();

but it fails again.

io.r2dbc.spi.R2dbcBadGrammarException: [42102] [42S02] Table "CUSTOMER" not found; SQL statement:

The insert statements can't be executed because of the missing CUSTOMER TABLE?

@Thinkenterprise
Copy link

Sorry @hantsy workaround is already working :-) So my contribution was not so important!!

@snicoll snicoll self-assigned this Apr 2, 2020
@snicoll snicoll modified the milestones: 2.3.x, 2.3.0.RC1 Apr 3, 2020
@snicoll snicoll closed this as completed in 1df8b58 Apr 3, 2020
@snicoll
Copy link
Member

snicoll commented Apr 3, 2020

Actually, I already documented that in #20742 but I forgot to link the How-To in the main R2DBC section. I just did that.

@nkonev
Copy link

nkonev commented Apr 5, 2020

Sorry for PR, but I'd written simple migration library and published it to Maven Central

https://github.com/nkonev/r2dbc-migrate

@langley-agm
Copy link

@wilkinsona might also be a good idea to update https://spring.io/guides/gs/accessing-data-r2dbc/

@wilkinsona
Copy link
Member

Thanks for the suggestion. I've opened spring-guides/gs-accessing-data-r2dbc#1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

7 participants