db_converter is an open-source database migration tool for PostgreSQL 9.6+ designed for high loaded installations.
The basic goal of db_converter is to simplify the database conversion (migration) process as much as possible, while maintaining flexibility and functionality.
Tasks that can be solved using db_converter:
- Transactional modification of data of any volume
- Database structure changing with locks control
- System and application notifications via
mattermost(or any other messenger) - Database maintenance (deleting old data, creating triggers on partitions, etc.)
- Export data in
csvformat into encrypted archive
The key features are:
- Parallel processing of several databases
- Handling of the locks to avoid impact on the regular workload
# run read-only packet
python38 db_converter.py \
--packet-name=dba_get_conf \
--db-name=dbc
# Info: =====> DBC 2.7 started
# Info: =====> Hold lock for packet dba_get_conf in DB dbc
# Info: Thread 'lock_observer_dbc' runned! Observed pids: []
# Info: --------> Packet 'dba_get_conf' started for 'dbc' database!
# Info: lock_observer_dbc: iteration done. Sleep on 5 seconds...
# Info: Thread 'lock_observer_dbc': Observed pids: []
# Info: Thread 'ro_manager_db_dbc', DB 'dbc', PID 24160, Packet 'dba_get_conf', ...
# Info:
# ---------------------------------------------------------------------------
# | name | value | pretty_value | boot_val | unit |
# ---------------------------------------------------------------------------
# | autovacuum_max_workers | 3 | | 3 | None |
# | autovacuum_naptime | 60 | | 60 | s |
# ....
# Info: <-------- Packet 'dba_get_conf' finished for 'dbc' database!
# Info: Thread lock_observer_dbc finished!
# Info: <===== DBC 2.7 finished
# run in background
nohup python38 db_converter.py \
--packet-name=my_packet \
--db-name=db01
> /dev/null 2>&1 &
tail -f log/dbc_db01_my_packet.log
# run all tests
python38 tests/test_packets.py -v
# run specific test
python38 tests/test_packets.py -v TestDBCLockPython 3.x with modules: sqlparse, requests, pyzipper
yum install -y python38
# if pip is not installed
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3.8 get-pip.py
pip3.8 install sqlparse
pip3.8 install requests
pip3.8 install pyzipperBuilt-in module py-postgresql.
Packet - is a package of changes (a directory with sql files) that apply to the specified database. Packet contains meta_data.json (an optional file with meta information describing the package) and several sql files in XX_step.sql format.
Step - is a sql file, the contents of which are executed in one transaction, and containing the following types of commands:
- DDL (Data Definition Language) - CREATE, DROP, ALTER, TRUNCATE, COMMENT, RENAME
- DML (Data Manipulation Language) - SELECT, INSERT, UPDATE, DELETE
- DCL (Data Control Language) - GRAND, REVOKE
Action - is a transaction formed on the basis of step. If step does not have a generator, then it creates one action. If step has a generator, then several transactions are generated.
Generator - is a sql file associated with some step by index number. If there is a generator, step contains placeholders for substituting the values returned by generator (for more details see the "Generators and Placeholders" section).
Conversion (migration, deployment) - is a transformation of the database structure according to specified package of changes.
When executing Packet, sql files are applied to the specified database sequentially in accordance with index.
db_convertrer works in the following modes:
-
List all target databases according
--db-namemask if the--listkey is specified -
Perform deployment - deploy specified
packetto the target database--db-name -
Perform force deployment - forced deployment if the
--forcekey is specified - ignore the difference between hashes ofpacketat the time of repeated execution and at the time of first launch -
Perform sequential deployment if the
--seqkey is specified, then parallel execution is disabled (if several databases are specified) and all databases are processed sequentially according to the specified list. Several databases can be processed in parallel, the possibility of parallelizing the conversion of one database does not make sense. -
Check packet status - display
packetstatus if the--statuskey is specified -
Wipe packet deployment history if the
--wipekey is specified. Wipe means delete fromdbc_ *tables. Removing information about an installed package can be used for debugging purposes. -
Unlock unexpectedly aborted deployment if the
--unlockkey is specified -
Stop all active transactions of unexpectedly aborted deployment if the
--stopkey is specified. It this mode all active connections will be terminated matching withapplication_name(specified in thedb_converter.confconfiguration file) +"_"+--packet-name -
Use template packet - copy
*.sqlfiles frompackets/templates/templatetopackets/packet-nameif the--templatekey is specified
Auxiliary deployment modes are also provided:
-
Skip whole step on first error like
Deadlock,QueryCanceledErrorif the--skip-step-cancelkey is specified -
Skip action errors like
Deadlock,QueryCanceledErrorif the--skip-action-cancelkey is specified
In all deployment modes two parameters are mandatory:
-
--db-name- name of directory located inpackets -
--packet-name- a name of one database or a list of databases separated by commas, orALLto automatically substitute all databases listed indb_converter.conf
- Database versioning
- Web interface
