How to sync databases?

Hi!

I would like to use Gitlab for versioning and deploying a Website.
At the Moment I am able to push my local repo to gitlab, but a push only includes the files.

Now my question:

How can I sync the databases of the different environments (local, test, production)?

Or should I use only one database for all three environments?

Please excuse this (probably) easy question, but I am new to Gitlab/git :slight_smile:

Thank you very much for your help!

1 Like

Hi @olewald,
You can use git for source version control. If you need to deploy code you can use Gitlab CI

Thank you for your fast reply!

And CI also syncs the different databases?
Or is it better to use one database for all three environments?

Can someone give me a further explanation?

That would be great!

In general, syncing databases between environments is very bad practice. You want to have DB for each environment where data are separated. You don’t want to overwrite data in your production DB with data from some local test DB.
You store your code in GIT, in your case a website in some language. You use CI to deploy that code to a webhosting or something else.
It usually works like this. You have the code on your local workstation, you write the code and test it locally. If it works fine you commit and push it to GitLab. GitLab CI will take your code and deploy it to the devel/staging/test environment where you can do further tests or whatever you need. If everything is fine you can merge the changes and deploy to production.
And by code I mean the website code, NOT the database data. If you need to alter DB schemas you can use something like liquibase in your CI.

Ok, right.

One last question:

If I use CI/CD to deploy my code to a staging and production server…then I deploy the code, but whats about the database entries?

For example I add an image somewhere on the website (local)…and there is a database entry for this image (also local).
Now I deploy my code to a staging server…so the image and the code are on my staging server then.
But how does that work without the database entry?

I hope you can understand my „problem“.

Thank you so much for your informations, it helps me a lot to understand the whole process!

The most common frameworks already have a solution for your problem. For example Django Migrations or Laravel Seeder. So my first step would be to check if your framework doesn’t have a solution for that.
If you are not using framework or it doesn’t have this functionality you can always do it using simple sql files with INSERT INTO ... WHERE NOT EXISTS ... that you execute against the database as part of the deployment.
Or you can write down your own script in your language that first check if the value exist and insert it to database if it doesn’t (basically your own seeder). It would be part of your application’s code. You would then run this script by calling it using HTTP or by the code Interpreter like PHP or Python during deployment.

+1

The thing to search for is “database migrations” and your tech stack or framework. There are some slightly more generic migration tools, such as flyway for any JVM based language, or liquibase that @balonik has already mentioned.

1 Like