[Spring] 실행시 DB 특정 상태로 초기화 하기

유알·2023년 4월 4일
0

[Spring]

목록 보기
11/17

schema.sql

DROP TABLE IF EXISTS customer;
CREATE TABLE customer (id SERIAL PRIMARY KEY, first_name VARCHAR(255), last_name VARCHAR(255));

초기화 코드

package com.oj.flux_demo;

import com.oj.flux_demo.domain.Customer;
import com.oj.flux_demo.domain.CustomerRepository;
import io.r2dbc.spi.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer;
import org.springframework.r2dbc.connection.init.ResourceDatabasePopulator;

import java.time.Duration;
import java.util.Arrays;

@Slf4j
@Configuration
public class DbInit {
    @Bean
    ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {

        ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
        initializer.setConnectionFactory(connectionFactory);
        initializer.setDatabasePopulator(new ResourceDatabasePopulator(new ClassPathResource("schema.sql")));

        return initializer;
    }

    @Bean
    public CommandLineRunner demo(CustomerRepository repository) {

        return (args) -> {
            // save a few customers
            repository.saveAll(Arrays.asList(new Customer("Jack", "Bauer"),
                            new Customer("Chloe", "O'Brian"),
                            new Customer("Kim", "Bauer"),
                            new Customer("David", "Palmer"),
                            new Customer("Michelle", "Dessler")))
                    .blockLast(Duration.ofSeconds(10));

            // fetch all customers
            log.info("Customers found with findAll():");
            log.info("-------------------------------");
            repository.findAll().doOnNext(customer -> {
                log.info(customer.toString());
            }).blockLast(Duration.ofSeconds(10));

            log.info("");

            // fetch an individual customer by ID
            repository.findById(1L).doOnNext(customer -> {
                log.info("Customer found with findById(1L):");
                log.info("--------------------------------");
                log.info(customer.toString());
                log.info("");
            }).block(Duration.ofSeconds(10));


            // fetch customers by last name
            log.info("Customer found with findByLastName('Bauer'):");
            log.info("--------------------------------------------");
            repository.findByLastName("Bauer").doOnNext(bauer -> {
                log.info(bauer.toString());
            }).blockLast(Duration.ofSeconds(10));;
            log.info("");
        };
    }

}
2023-04-04T13:37:33.989+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer[id=1, firstName='Jack', lastName='Bauer']
2023-04-04T13:37:33.990+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer[id=2, firstName='Chloe', lastName='O'Brian']
2023-04-04T13:37:33.990+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer[id=3, firstName='Kim', lastName='Bauer']
2023-04-04T13:37:33.990+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer[id=4, firstName='David', lastName='Palmer']
2023-04-04T13:37:33.990+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer[id=5, firstName='Michelle', lastName='Dessler']
2023-04-04T13:37:33.990+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : 
2023-04-04T13:37:33.996+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer found with findById(1L):
2023-04-04T13:37:33.996+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : --------------------------------
2023-04-04T13:37:33.996+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer[id=1, firstName='Jack', lastName='Bauer']
2023-04-04T13:37:33.996+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : 
2023-04-04T13:37:33.996+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer found with findByLastName('Bauer'):
2023-04-04T13:37:33.996+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : --------------------------------------------
2023-04-04T13:37:34.004+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer[id=1, firstName='Jack', lastName='Bauer']
2023-04-04T13:37:34.004+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : Customer[id=3, firstName='Kim', lastName='Bauer']
2023-04-04T13:37:34.005+09:00  INFO 6964 --- [  restartedMain] com.oj.flux_demo.DbInit                  : 
profile
더 좋은 구조를 고민하는 개발자 입니다

0개의 댓글