1

Firstly I'm only discussing my development database (development.sqlite3).

I have recently just realised that by default the database is not under version control (after creating a model and switching branches and trying to create that same model - I was redoing a tutorial - I got an error stating that it already existed). I checked my .gitignore file that was created by default and indeed it was not under version control.

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal

I find this somewhat annoying and actually worrying that it's not under version control however I'm sure this default behaviour is done with good reason.

Why is the database not under version control?

Similarly is it a bad idea if I was to do so? (Although I agree it will be massively annoying to keep committing the database to git after new CRUD to the database are done).

How would I go about doing this? - I would've thought it was nice to be able to switch branches (not the master) and alter the model and know that when I return to the master branch and know that I haven't broken anything (if I haven't merged of course).

Mark
  • 1,337
  • 23
  • 34
  • 1
    Possible duplicate of [How can I put a database under git (version control)?](https://stackoverflow.com/questions/846659/how-can-i-put-a-database-under-git-version-control) – evolutionxbox Dec 27 '17 at 18:51
  • @evolutionxbox No, I want to be able to switch branch and have different versions of the database per branch. – Mark Dec 27 '17 at 18:54
  • That is not what branches are for, but if that is what you want, the only thing I can think of.. is creating a script for each branch that scrap and rebuild your database. In this way you can control the database version in every branch. – Alejandro Montilla Dec 27 '17 at 19:00
  • 1
    What do you mean by this is not what branches are for? If the database is put under version control then effectively you have a different version per branch (like any other file under version control) ? Ok I could phrase this better by saying you have one file with the deltas applied depending on the branch.... – Mark Dec 27 '17 at 19:15
  • Check [this answer](https://stackoverflow.com/a/44992548/2303202). Basically, there are links to 2 ways to do it: https://github.com/cannadayr/git-sqlite , https://github.com/DataWraith/gitfilters (see sqlite filters there) – max630 Dec 28 '17 at 11:14
  • @max630 I need something stable though. These don't look that stable/reliable. – Mark Dec 28 '17 at 13:00

1 Answers1

1

The best way IMO is to create separate long-running worktrees for different branches:

git clone -b branch1 master-repo project-b1
git clone -b branch2 master-repo project-b2

Create different DB structures/data in these worktrees and don't switch branches.

phd
  • 82,685
  • 13
  • 120
  • 165
  • I have no issues with using git... I have no idea what a working tree is or does? And how is it different to branches? – Mark Dec 27 '17 at 22:24
  • A working tree is what you get when you clone a repository — a directory tree full of files. Your current workflow is to use 1 worktree and switch branches inside it. My recommendation is to use 1 worktree per branch and stop switching branches at all; when you need to work in another branch you run `cd` and update the branch. You can eve use [git worktree command](https://www.kernel.org/pub/software/scm/git/docs/git-worktree.html) if you use fresh git. – phd Dec 27 '17 at 22:27
  • this wouldn't work see https://stackoverflow.com/q/39665570/5506988 – Mark Apr 11 '21 at 22:02
  • I don't see any problems. I recommended `git worktree` and two different worktrees, I never recommended to check out the same branch. Exactly opposite, I recommended to check different branches. – phd Apr 11 '21 at 22:46