📜 ⬆️ ⬇️

We generate the "correct" SQL dump

In the development process using MySQL, it is often necessary to do a database dump in order to save it to the repository (deployment to the server, etc.).
There are different clients for working with MySQL:
- MySQL Front
- PHPMyAdmin
- Aqua Data Studio
- EMS SQL manager
and so on.

Problem


In each of these, there is a function to export the database schema and its data to a file. Simply put - create a database dump. But bad luck! Each tool generates SQL code with its formatting. For example, some do not even insert ENGINE = MyISAM DEFAULT CHARSET = ... in a CREATE TABLE statement, the same with DROP TABLE IF EXISTS. In short, there is a swan, cancer and pike effect.

Difficulties arise if there are several developers in the team - most likely each of them is accustomed to using any one tool. Attempts to force developers to use a particular client can lead to holy wars (“I’m used to it, I don’t want something else .., and this sucks!”). Perhaps, the problem will seem far-fetched to someone, but in my practice it was like that. And what to do if people work on different operating systems? They will have different SQL managers too.
')
So, you need a way to generate a dump in some kind of unified format. Expected Benefit:

Decision


Abandon any third-party tools for generating a dump.
In the MySQL distribution, there is a set of useful utilities, including mysqldump, well-known to many. Below you can see the BAT file, which using this utility generates the correct SQL dump.
Correct - in this context means "corresponding to the agreed format." In any case, I do not want to question the correctness of the work of the native utility from MySQL :)

@echo off
rem author afi
echo =========================================
echo SQL generator
echo Output files :
echo scheme.sql - Scheme of database
echo data.sql - Data for database
echo =========================================

rem UTF-8
set ENCODING=utf8

IF "%1" == "" goto ERROR
IF "%2" == "" goto ERROR
IF "%3" == "" goto ERROR
IF "%4" == "" goto ERROR
IF NOT "%5" == "" (
set ENCODING="%5"
)

goto get

:get
echo Generating scheme for DB: %1
mysqldump --host=%1 --password=%4 -u %3 --disable-keys --add-drop-table --default-character-set=%ENCODING% --no-data --result-file=scheme.sql %2

echo Generating data for DB: %1
mysqldump --host=%1 --password=%4 -u %3 --disable-keys --default-character-set=%ENCODING% --no-create-info --extended-insert=false --result-file=data.sql %2
goto END

:ERROR
echo Please, define parameters. Example:
echo gensql.bat host_name database_name mysql_user mysql_password [encoding]
goto END

:END
echo =========================================

@pause



Now all worries are reduced to running the script with parameters:
gensql.bat host_name database_name mysql_user mysql_password [encoding]

where encoding is the output data encoding. Optional parameter, default is utf8.

As a result, we get 2 files:
scheme.sql - contains the database schema, i.e. script for creating tables, constraints, views, etc.
data.sql - contains data dump

Make a bash-version of the script, I think it will not be difficult for those who may need it.

Source: https://habr.com/ru/post/23465/


All Articles