Git Remote branch 삭제하기

|

Git을 사용하다보면 branch이름을 바꿔야 할 때가 있습니다.
아래 명령어로 로컬 branch를 간단히 바꿀 수 있습니다.

$ git branch -m [old-name] [new-name]

하지만 remote저장소에 있는 branch이름은 바꿀 수가 없는데 바꾸려면 remote저장소의 branch를 삭제하고 다시 로컬 branch를 이용하여 재생성해야합니다.

remote 저장소 삭제

아래 명령어로 remote 브랜치를 삭제합니다.

$ git push [remote-name] --delete [old-branch-name]

remote저장소에 local branch 올리기

로컬 저장소의 이름을 새 이름으로 변경 후 remote저장소에 새로 올립니다.

$ git branch -m [old-name] [new-name]
$ git push [remote-name] [new-name]
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/jistol/jistol.github.io.git
* [new branch]      new-name -> new-name

(SpringBoot) 데이터베이스 초기화 (spring.jpa.hibernate.ddl-auto, import.sql, spring.datasource.data)

|

spring.jpa.hibernate.ddl-auto

옵션 설명
create 기존에 생성되 있던 테이블들을 삭제하고 새로 만듭니다.
create-drop create와 같은 동작을 하나 종료시에 DROP합니다.
update 변경된 부분만 반영합니다.
validate 테이블과 Entity가 매핑되는지 유효성 검사를 실행합니다.
none 초기화 동작을 사용하지 않습니다.

import.sql

  • 리소스에 위 파일이 위치하면 테이블 생성시 자동으로 스크립트를 실행시켜줍니다.

spring.datasource.data

  • 이 옵션에 지정한 파일을 테이블 생성시 자동으로 실행시켜줍니다.
  • 파일은 ,(쉼표)로 여러개를 지정하거나 *기호를 이용하여 패턴 지정가능합니다.
  • 파일 경로는 클래스패스, 절대경로, 상대경로 모두 지정가능합니다.

예시 : classpath:/sql/test/init-*.sql,file:/home/jistol/sql/test.sql,/META-INF/sql/initScript.sql

(JPA,SpringData) 쿼리 메소드 정의하기

|

iBatis만 사용하다가 SpringData를 처음 접해보면 신세계를 느끼는 것 중 하나가 interface에 메소드 하나 정의 했는데 쿼리가 완성된다는 점이 아닐까 싶습니다. xml에 죽어라 SQL문 만들다가 이렇게 간단하게 해결되면 SpringData를 안쓸수가 없죠.(반대로 더 불편해지거나 힘든점도 있긴 합니다.)

그동안 사용해온 쿼리 메소드들에 대해 몇몇가지 정리해봅니다. 자세한 내용은 Spring Data JPA - Reference Documentation(1.10.7)영문이나 이쁘게 번역해놓은 번역본을 참고하시기 바랍니다.

메소드 이름 안에서 지원되는 키워드들

Keyword Sample JPQL snippet
And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2
Or findByLastnameOrFirstname … where x.lastname = ?1 or x.firstname = ?2
Is,Equals findByFirstname,findByFirstnameIs,findByFirstnameEquals … where x.firstname = 1?
Between findByStartDateBetween … where x.startDate between 1? and ?2
LessThan findByAgeLessThan … where x.age < ?1
LessThanEqual findByAgeLessThanEqual … where x.age ⇐ ?1
GreaterThan findByAgeGreaterThan … where x.age > ?1
GreaterThanEqual findByAgeGreaterThanEqual … where x.age >= ?1
After findByStartDateAfter … where x.startDate > ?1
Before findByStartDateBefore … where x.startDate < ?1
IsNull findByAgeIsNull … where x.age is null
IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null
Like findByFirstnameLike … where x.firstname like ?1
NotLike findByFirstnameNotLike … where x.firstname not like ?1
StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %)
EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %)
Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %)
OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc
Not findByLastnameNot … where x.lastname <> ?1
In findByAgeIn(Collection ages) … where x.age in ?1
NotIn findByAgeNotIn(Collection age) … where x.age not in ?1
True findByActiveTrue() … where x.active = true
False findByActiveFalse() … where x.active = false
IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1)

쿼리 결과 Limit(Top)하기

User findFirstByOrderByLastnameAsc();
User findTopByOrderByAgeDesc();
Page<User> queryFirst10ByLastname(String lastname, Pageable pageable);
Slice<User> findTop3ByLastname(String lastname, Pageable pageable);
List<User> findFirst10ByLastname(String lastname, Sort sort);
List<User> findTop10ByLastname(String lastname, Pageable pageable);

중복제거 Disctinct 사용하기

List<Person> findDistinctPeopleByLastnameOrFirstname(String lastname, String firstname);
List<Person> findPeopleDistinctByLastnameOrFirstname(String lastname, String firstname);

(SpringBoot) MVC 테스트 하기 - `@WebMvcTest`, `@AutoConfigureMockMvc`

|

SpringBoot Reference에 접속하여 목차를 보면 거의 끝쯤에 Testing관련 내용이 나옵니다. 이 중 SpringBoot의 Controller를 JUnit으로 테스트 하고 싶은 경우 41.3.7 Auto-configured Spring MVC tests를 보면 Http Connection을 별도 구현하지 않고도 MVC 테스트를 가능하게 하는 설명이 나옵니다.

캡처화면

@WebMvcTest

일반적으로 사용하는 MVC테스트용 어노테이션입니다. 해당 어노테이션을 명시하고 그림과 같이 MockMvc@Autowired하면 해당 객체를 통해 MVC테스트가 가능합니다.

@WebMvcTest사용시 주의사항 1

@WebMvcTest어노테이션 사용시 @SpringBootTest을 같이 사용할 수 없습니다. 서로 MockMvc를 설정하기 때문에 충돌이 나는거 같은데요, MVC 기능만 사용할 거라면 @WebMvcTest를 사용하면 됩니다.

@WebMvcTest사용시 주의사항 2

@WebMvcTest 사용시 다른 설정들은 자동으로 올리지 않기 때문에 @Repository@Resource, @Service, @Component등은 사용할 수 없습니다. 아래 글과 같이 자동설정하는 영역은 @Controller, @ControllerAdvice, @JsonComponent 등등이네요. 그런데 저는 실제로 테스트 해보니 @ControllerAdvice도 먹히지 않았습니다. (이유는 아직도 모르는중…)

@WebMvcTest사용시 주의사항 3

@WebMvcTest가 포함하는 실제 설정은 Appendix D. Test auto-configuration annotations에서 확인 가능 합니다.

@WebMvcTest이 포함하는 설정

@AutoConfigureMockMvc

@WebMvcTest외에 MVC테스트를 할 수 있는 다른 방법입니다. 위 설정은 MVC테스트 외 모든 설정을 같이 올립니다. AOP도 되고 JPA Repository도 사용가능하네요. 실제적으로 동작하는 MVC테스트를 하려면 위 어노테이션을 사용해야 합니다. @AutoConfigureMockMvc@SpringBootTest와 같이 사용 가능합니다.

(SpringBoot) H2 DB 서버모드로 띄워 외부 툴(DBeaver)로 접속하기

|

SpringBoot에서 H2 DB Embedded를 사용하다보면 항상 console에 들어가 쿼리해야하는 불편함이 있어 외부 툴에서 접근하는 방법을 찾아 정리해 보았습니다.

우선 application.xml에 정의되 있는 아래 항목을 바꿔줍니다.

[변경 전]
spring.datasource.url=jdbc:h2:file:./db/devdb;

[변경 후]
spring.datasource.url=jdbc:h2:file:./db/devdb;AUTO_SERVER=TRUE

※ 주의사항 : H2 DB를 메모리 모드로 올릴 경우에는 사용 할 수 없습니다. 반드시 file모드로 올려주세요.

위와 같이 변경후 실행 시키면 서버모드로 뜨게 되고 DBeaver를 통해 아래와 같이 설정합니다.

  • “Create New Connect” 창을 띄우고 아래와 같이 “H2 - Embedded”를 선택합니다.

Create New Connect

  • Setting 화면에 아래와 같이 각 정보를 넣어줍니다. ※ JDBC URL 항목이 빈 란일 경우 “Driver Properties”탭을 선택하여 H2 Driver를 다운 받았는지 확인합니다. ※ 파일 경로를 full로 적어줘야합니다.

Create New Connect

  • Finish 버튼을 클릭하여 마무리.

Create New Connect

위와 같이 설정해도 한쪽에서 붙어 있는 상황에선 다른쪽이 붙질 못하더군요. 그 방법까진 아직 못찾아봐서 패스.