DB 마이그이션 Flyway

code_able·2024년 4월 7일
0
post-custom-banner

flyway

DB의 마이그레이션을 형상관리할 수 있도록 도와주는 java의 라이브러리

flyway 설정

gradle 설정

// DB Migration
implementation 'org.flywaydb:flyway-core'
runtimeOnly group: 'org.flywaydb', name: 'flyway-database-postgresql', version: '10.8.1'

application.yml

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/db
    username: user
    password: password
    driver-class-name: org.postgresql.Driver
  flyway:
    enabled: true
    baseline-on-migrate: true

entity/User.java

package com.boilerplate.stygj.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.persistence.Id;
import jakarta.persistence.Column;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@Entity
@Table(name="users")
public class User {
    @Id
    private int id;
    @Column(length = 50)
    private String username;
    @Column(length = 100)
    private String email;
    @Column(length = 155)
    private String password;
    private LocalDateTime createdDate;
    private LocalDateTime updatedDate;
    private LocalDateTime deletedDate;
}

Flyway 적용해보기

sql 파일 위치

resources/db/migration/

파일명 규칙

init sql 스크립트 작성

CREATE TABLE user (
    id SERIAL PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(100) NOT NULL,
    created_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    deleted_date TIMESTAMP
);

Spring boot 실행 >> 테이블 확인

profile
할수 있다! code able
post-custom-banner

0개의 댓글