How can I update a database with the following scenario?
I have a database in a test environment (server1) and I must update it with the one in the development environment (server2), but I only want to make the changes in the structure, without deleting the data already stored, I have already checked that:
I will not update the structure of existing columns, I will only add new ones, and the data that does not exist will be placed with a default
or put as null
.
So, how to make the comparison of the development server and get the differences with respect to the production server and bring as a result the changes that I must make in the production server?
If you are not working with Workbench I would recommend using it, it has a functionality that is to synchronize models.
1. Reverse engineering
It is necessary to first reverse engineer your development database, which using the tool is super simple. This retrieves your database by design.
Design can be stored in a file
2. Model synchronization
Then you can connect to your server from Workbench and perform the synchronization.
Later the program will show you the scripts that result in differences between your model and the production database.
There is a set of tools called
MySQL Utilities
, within which we can findmysqldiff
.Assume the following object definitions:
prod_host :
dev_host
To generate a set of statements
SQL
that transform the definition ofprod_db.table
intodev_db.table
, use this command:--
A common error when trying to run this command against a remote shared or dedicated server is the following:
This is because you are
mysqldiff
trying to runSELECT * FROM mysql.proc;
, and this command is blocked for all users except the accountroot
. In addition, it is necessary to validate that the userroot
has privileges to access remotely (eg:GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY PASSWORD ...
).If you work with Symfony + doctrine there is a bundle called doctrinemigrationsbundle t. This has a Diff command that obtains the differences between the database and the model you have defined, creating a PHP migration file.
In general, if you use an orm in your application to define the schema, there is usually a library that differentiates the schema depending on the environment you use (dev, prod, test...)