Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I put a database under git (version control)?

People also ask

Can you version control a database?

What is database versioning? Database versioning begins with database schema, the structure of the database. In order to effectively version a database, you need to track and understand the changes that are happening.

How do I manage database versions?

The most basic method for managing database changes is to save the alter command in a script or set of scripts, and manage them in the exiting file-based version control. This guarantees a single repository that stores all the application component assets.

How do you use version control in SQL?

Open SQL Server Management Studio and connect to a SQL Server instance. Right-click on your database in the Object Explorer pane and select "Connect to Version Control". This will open the "Connect Database to Version Control" dialog. Copy the https repository path from GitHub and paste it into VersionSQL.


Take a database dump, and version control that instead. This way it is a flat text file.

Personally I suggest that you keep both a data dump, and a schema dump. This way using diff it becomes fairly easy to see what changed in the schema from revision to revision.

If you are making big changes, you should have a secondary database that you make the new schema changes to and not touch the old one since as you said you are making a branch.


I'm starting to think of a really simple solution, don't know why I didn't think of it before!!

  • Duplicate the database, (both the schema and the data).
  • In the branch for the new-major-changes, simply change the project configuration to use the new duplicate database.

This way I can switch branches without worrying about database schema changes.

EDIT:

By duplicate, I mean create another database with a different name (like my_db_2); not doing a dump or anything like that.


Use something like LiquiBase this lets you keep revision control of your Liquibase files. you can tag changes for production only, and have lb keep your DB up to date for either production or development, (or whatever scheme you want).


There is a great project called Migrations under Doctrine that built just for this purpose.

Its still in alpha state and built for php.

http://docs.doctrine-project.org/projects/doctrine-migrations/en/latest/index.html