ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Django - .gitignore활용 (feat. migrations)
    Project/git 2021. 1. 2. 01:48

    django 웹 개발을 진행 중일 때, 어떤 파일들을 .gitignore에 포함시켜야 할까?

     

    Jump to django 문서를 참고해 아래의 파일들을 .gitignore를 사용해 commit목록에서 제외시켜 왔다.

     

    .idea
    db.sqlite3
    __pycache__
    *.pyc 
    venv

     

    .idea

    • 사용자 설정을 저장하는 파이참 전용 파일

    db.sqlite3

    • SQLite의 데이터베이스 파일(다른 모델을 쓴다면, 그에 맞게 변경해 주어야 한다.)

    __pycache__, *.pyc

    • python 프로그램을 실행시킬 때, 인터프리터는 프로그램을 bytecode로 먼저 컴파일한다. 이 파일들(*.pyc)을 __pycache__폴더에 저장해 놓고, 프로그램이 재실행될 때, 조금 더 빠르게 실행될 수 있도록 한다. .gitignore에 포함되지 않더라도 문제가 발생하지는 않지만, 통상적으로 version control system을 사용할 때, ignore파일에 포함시킨다. - stackoverflow
    • Python will always compile your code to byte code. This is saved in the .pyc files. You can’t do much with that and we don’t need it, python will create them anyway. It’s best to just ignore them through .gitignore. - djangowaves

     

    venv

    • 가상환경 폴더

     

    우리 팀이 겪었던 Error

     

    git을 이용해 협업하던 중, '*** column does not exist'라는 에러를 마주하게 되었다. 

     

    model.py에서 schema를 다시 한번 확인해보고, 

    python manage.py migrate

    python manage.py makemigrations

    명령어로 migrate도 다시 해봤지만, 여전히 해결되지 않았다.

     

    그런데, 검색해보니 서로 commit하는 과정에서 django 앱의 migrations폴더에 존재하는 파일들이 충돌을 일으켰던 것이 문제였다.

     

    Django migrations are recorded in your database under the 'django_migrations' table. This is how Django knows which migrations have been applied and which still need to be applied.
    Have a look at django_migrations table in your DB. It may be that something went wrong when your migration was applied. So, delete the row in the table which has the migration file name that is related to that column that 'does not exist'. Then, try to re-run a migration. - stack overflow

     

    해결 방안

     

    migrations 폴더 내에 __init__.py를 제외한 파일들을 모두 삭제하고, 

     

    python manage.py makemigrations

    python manage.py migrate

     

    를 다시 실행해 문제를 해결할 수 있었다. 

     


     

    migrations 폴더를 .gitignore 포함시켜야 할까?

     

     

     

    이 문제를 겨우 해결하고, migrations폴더를 .gitignore에 포함시켜야 하는지에 대해서 찾아봤다.

    우리와 비슷하게 migrations폴더 내의 파일들 때문에 고통을 받는 사람들이 꽤 있는 듯했다.

     

    그런데, stackoverflow상에서 migrations폴더를 .gitignore에 추가해야 하는지에 대해서는 서로 의견들이 달랐다. 

    그중, 개발 단계에서는 migrations폴더를 .gitignore에 추가해 repository에 올라가지 않도록 하고, model schema에 큰 변화가 없는 배포 단계에서는 migrations폴더를 ignore하지 않는 대신, makemigrations 명령을 하지 않는다는 의견이 다수를 이루는 것 같다. [stack overflow]

     

     

    결론

     

    migrations폴더도 gitignore에 추가해주어 서로의 migrate내역들이 충돌하지 않도록 진행 중이다.

    만약 이와 비슷한 문제를 겪고 있다면, migrations폴더 내에 __init__.py를 제외한 모든 파일을 삭제하고, 다시 migrate명령을 수행하면 해결될 것이다.

     

    이와 관련해서 조금 더 조사가 필요할 것 같다.

     

     

    reference

    - stack overflow link

    - stack overflow link

     

    + tip

    - gitignore for a django project link

    Django 프로젝트에서 gitignore에 포함시켜야 할 파일들과 이유들이 잘 정리되어 있다.

    - gitignore.io link

    gitignore.io에서 검색을 통해 특정 프로젝트에서 어떤 파일을 ignore시켜야 하는지 상세하게 찾을 수 있다.

     

     

    틀린 부분이 있을 수 있습니다. 피드백 주시면 고치도록 하겠습니다! 감사합니다.👍

    [꼭 다시 풀어보기]

    'Project > git' 카테고리의 다른 글

    .gitignore에 대해 알아보자.  (0) 2020.12.31
Designed by Tistory.