반응형
마이그레이션이란 ?
: 데이터베이스 스키마의 변경 내역을 버전 관리하여, 변경에 대한 이력을 남기고,
데이터베이스를 이전 버전에서 최신 버전으로 옮기는 일련의 과정들을 의미한다.
( MySQL 데이터베이스를 사용한 것이 아닌 H2 데이터베이스를 사용했었음. )
→ H2는 내부 DB(인메모리)로써 서버를 재시작하면 휘발성이기 때문에 DB서버가 꺼지면 모든 데이터가 유실된다.
하지만 MySQL 데이터베이스를 사용하면 서버가 꺼지더라도 데이터가 유실되지 않고 남아있다.
즉, 일반적으로 마이그레이션은 스키마를 변경하거나 새로운 테이블이나 컬럼을 추가하는 등의 작업을 포함하고 따라서 우리가 할 작업 H2 데이터베이스에서 MySQL로 변경할 때도 마이그레이션을 수행한다고 할 수 있다.
이러한 경우 데이터 스키마를 변경하거나 데이터를 이전하는 작업 등이 포함될 수 있다.
마이그레이션을 하려면 yml 파일을 수정해야 한다. (이전 yml 설정 확인 후 수정)
이전 yml 설정 (H2 데이터베이스 사용)
server:
port: 8080
servlet:
encoding:
charset: utf-8
force: true
spring:
mvc:
view:
prefix: /WEB-INF/view/
suffix: .jsp
datasource:
url: jdbc:h2:mem:testdb;MODE=MySQL
driver-class-name: org.h2.Driver
username: sa
password:
sql:
init:
schema-locations:
- classpath:db/table.sql
data-locations:
- classpath:db/data.sql
h2:
console:
enabled: true
output:
ansi:
enabled: always
mybatis:
mapper-locations:
- classpath:mapper/**.xml
configuration:
map-underscore-to-camel-case: true
수정 후 yml 파일 (MySQL 데이터베이스 사용)
server:
port: 8080
servlet:
encoding:
charset: utf-8
force: true
spring:
mvc:
view:
prefix: /WEB-INF/view/
suffix: .jsp
datasource:
url: jdbc:mysql://localhost:3306/bank2?serverTimezone=Asia/Seoul
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 1234
# sql:
# init:
# schema-locations:
# - classpath:db/table.sql
# data-locations:
# - classpath:db/data.sql
# h2:
# console:
# enabled: true
# output:
# ansi:
# enabled: always
mybatis:
mapper-locations:
- classpath:mapper/**.xml
configuration:
map-underscore-to-camel-case: true
build.gradle 파일에 connector-j 의존성 추가하기
// 커넥터 J
runtimeOnly 'com.mysql:mysql-connector-j'
새로운 데이터베이스 생성 후 최종 테이블, 최종 데이터들 넣어주기
CREATE DATABASE bank2;
USE bank2;
CREATE TABLE user_tb(
id int auto_increment primary key,
username varchar(50) not null unique,
password varchar(30) not null,
fullname varchar(50) not null,
created_at timestamp not null default now()
);
CREATE TABLE account_tb(
id int auto_increment primary key,
number varchar(30) not null unique,
password varchar(20) not null,
balance bigint not null comment '계좌 잔액',
-- user_id를 foreign key로 설정 할 수 있는데 foreign key 없이 설계
user_id int,
created_at timestamp not null default now()
);
CREATE TABLE history_tb(
id int auto_increment primary key comment '거래 내역 ID',
amount bigint not null comment '거래 금액',
w_account_id int comment '출금 계좌 ID',
d_account_id int comment '입금 계좌 ID',
w_balance bigint comment '출금 요청된 계좌에 잔액',
d_balance bigint comment '입금 요청된 계좌에 잔액',
created_at timestamp not null default now()
);
INSERT INTO user_tb(username, password, fullname, created_at) values('길동', '1234',
'고', now());
INSERT INTO user_tb(username, password, fullname, created_at) values('둘리', '1234',
'애기공룡', now());
INSERT INTO user_tb(username, password, fullname, created_at) values('콜', '1234',
'마이', now());
INSERT INTO user_tb(username, password, fullname, created_at) values('홍아', '1234',
'항아', now());
INSERT INTO account_tb(number, password, balance, user_id, created_at)
values('1111', '1234', 1200, 1, now());
INSERT INTO account_tb(number, password, balance, user_id, created_at)
values('2222', '1234', 2200, 2, now());
INSERT INTO account_tb(number, password, balance, user_id, created_at)
values('3333', '1234', 0, 3, now());
-- 이체 내역을 기록 ( 1번 계좌에서 2번 계좌로 100원을 이체 한다)
INSERT INTO history_tb(amount, w_balance, d_balance,
w_account_id, d_account_id, created_at)
VALUES (100, 800, 1200, 1, 2, now());
-- 출금 내역 ( 1번계좌에서 100원을 출금 처리 )
INSERT INTO history_tb(amount, w_balance, d_balance,
w_account_id, d_account_id, created_at)
VALUES (100, 700, null, 1, null, now());
-- 입금 내역 (1번 계좌에 500원 입금 처리 )
INSERT INTO history_tb(amount, w_balance, d_balance,
w_account_id, d_account_id, created_at)
VALUES (500, null, 1200, null, 1, now());
-- 입금 내역 (2번 계좌에 1000원 입금 처리 )
INSERT INTO history_tb(amount, w_balance, d_balance,
w_account_id, d_account_id, created_at)
VALUES (1000, null, 2200, null, 2, now());
반응형
'프로그래밍 > Spring Boot' 카테고리의 다른 글
Spring Boot_javax.el.PropertyNotFoundException (0) | 2023.04.24 |
---|---|
Spring Boot_암호화 처리 (0) | 2023.04.21 |
Spring Boot_예외 처리 기술 (Exception) (0) | 2023.04.18 |
Spring Boot_MyBatis 설정 (0) | 2023.04.18 |
Spring Boot_H2 DB 초기 설정 (0) | 2023.04.18 |