Table of Contents
Key Takeaways
Installing DBV
DBV Configuration
What Changes To Keep Track Of?
DBV Workflow
Revisions
Conclusion
Frequently Asked Questions on Database Versioning
What are the key benefits of database versioning?
How does database versioning work in practice?
What tools can be used for database versioning?
What are the challenges in implementing database versioning?
How can database versioning help in agile development?
How does database versioning relate to DevOps?
Can database versioning be used with cloud databases?
What is the role of database versioning in data governance?
How can database versioning improve database performance?
What are the best practices for database versioning?
Home Backend Development PHP Tutorial Database Versioning with DBV

Database Versioning with DBV

Feb 21, 2025 pm 12:09 PM

Database Versioning with DBV

Key Takeaways

  • DBV is a PHP-based database version control system for MySQL databases. It is not standalone and requires a version control system such as Git, Mercurial, or SVN for syncing changes with your team.
  • DBV allows developers to track changes made to a database, share these changes with team members, and ensure everyone is working with the latest copy of the database. It can track changes like new tables, renamed or dropped tables, new or updated fields, new or updated table rows, and more.
  • The DBV workflow involves creating a local copy of the database, making changes, exporting these changes to disk, committing them into source control, and pushing them to a central repository. Team members can then pull these changes to their local copy.
  • DBV also supports revisions, allowing developers to modify the structure of more than one table. However, it is good practice to make changes to a single table and make a revision for it, unless the changes are related to each other.

It’s good practice to always use a version control system in any of your projects. Be it a side-project in which you are the only developer, or a team project where five or more people are working on it together. But the idea of putting your database into version control isn’t really that widespread. Often times we take the database for granted.

But like the source files in our project, the database is constantly changing too. That’s why we also need a way to track the changes that we have made and easily share it to other members of our team.

In this article we will take a look at DBV, a database version control system written in PHP for MySQL databases so you need to have PHP and MySQL installed before you can use it, along with a web server like Apache or Nginx.

An important note about this software is that it is not a stand-alone database version control system, because it needs a version control system such as Git, Mercurial or SVN for syncing changes with your team.

Installing DBV

To start working with DBV, first you have to download the installer from their website, extract it into your project directory then rename the resulting folder to dbv. This will give you the following path:

<span>my_project/dbv</span>
Copy after login
Copy after login
Copy after login

An alternative approach is just cloning from Github.

DBV Configuration

You can start configuring the options for DBV by creating a copy of the config.php.sample file and renaming it to config.php.

The most important things to update here are the first two sections. Just substitute the values for my_username, my_password, my_database for the values in your current database configuration:

<span><span><?php
</span></span><span><span>/**
</span></span><span><span> * Your database authentication information goes here
</span></span><span><span> * <span>@see http://dbv.vizuina.com/documentation/
</span></span></span><span><span> */
</span></span><span><span>define('DB_HOST', 'localhost');
</span></span><span><span>define('DB_PORT', 3306);
</span></span><span><span>define('DB_USERNAME', 'my_username');
</span></span><span><span>define('DB_PASSWORD', 'my_password');
</span></span><span><span>define('DB_NAME', 'my_database');
</span></span><span>
</span><span><span>/**
</span></span><span><span> * Authentication data for access to DBV itself
</span></span><span><span> * If you leave any of the two constants below blank, authentication will be disabled
</span></span><span><span> * <span>@see http://dbv.vizuina.com/documentation/#optional-settings
</span></span></span><span><span> */
</span></span><span><span>define('DBV_USERNAME', 'my_username');
</span></span><span><span>define('DBV_PASSWORD', 'my_password');
</span></span><span><span>?></span></span>
Copy after login
Copy after login
Copy after login

The first section in the configuration file above is all about the MySQL database details in your machine.

The second section are the login details for dbv itself.

Next open up the .gitignore file. By default it contains the following:

<span>my_project/dbv</span>
Copy after login
Copy after login
Copy after login

These are the files that will be ignored by Git. But if you know that you have the same database information (user, password, database name) as your team mates then you can remove config.php from the .gitignore file. If not then they will have to create their own configuration file and exclude it from source control.

Next you also have to add the data/meta/revision file to .gitignore as that is where dbv puts information about your local copy of the database. It might be different for your team mates so it needs to be excluded from source control.

Once you’re done with the configuration you can now add dbv into version control:

<span><span><?php
</span></span><span><span>/**
</span></span><span><span> * Your database authentication information goes here
</span></span><span><span> * <span>@see http://dbv.vizuina.com/documentation/
</span></span></span><span><span> */
</span></span><span><span>define('DB_HOST', 'localhost');
</span></span><span><span>define('DB_PORT', 3306);
</span></span><span><span>define('DB_USERNAME', 'my_username');
</span></span><span><span>define('DB_PASSWORD', 'my_password');
</span></span><span><span>define('DB_NAME', 'my_database');
</span></span><span>
</span><span><span>/**
</span></span><span><span> * Authentication data for access to DBV itself
</span></span><span><span> * If you leave any of the two constants below blank, authentication will be disabled
</span></span><span><span> * <span>@see http://dbv.vizuina.com/documentation/#optional-settings
</span></span></span><span><span> */
</span></span><span><span>define('DBV_USERNAME', 'my_username');
</span></span><span><span>define('DBV_PASSWORD', 'my_password');
</span></span><span><span>?></span></span>
Copy after login
Copy after login
Copy after login

And then push it to your central repository for the other members of your team to pull:

<span>config.php
</span><span>.buildpath
</span><span>.project
</span><span>.settings</span>
Copy after login
Copy after login

What Changes To Keep Track Of?

Before we move on into actually using dbv. I’d like to touch a bit on which changes to keep track of. In the database world pretty much any change can be put into source control. This includes the following:

  • new tables
  • renamed tables
  • dropped tables
  • new fields
  • updated field
  • deleted field
  • new table row (default table data)
  • updated table row
  • deleted table row
  • views
  • stored procedures
  • triggers
  • functions (user-defined functions)

DBV Workflow

You can fire up dbv from your browser by accessing the following URL:

<span>git add dbv
</span><span>git commit -m "add dbv into project"</span>
Copy after login
Copy after login

Or if you defined a virtual host, by accessing its URL.

This will give you an interface similar to the following:

Database Versioning with DBV

From the screenshot above you will see the tables that are currently in the database that you supplied in the config.php earlier. There’s also an In DB field that displays whether a specific table is currently in the database and the On disk which displays whether the current table is saved in your filesystem. With this information you’re pretty much aware whether you currently have the latest copy of the database.

An important thing to remember when working with dbv is that any changes you make to your local copy of your database should have a local copy that you can commit into your source control.

That means that if you create a new table in the database you have to export it to disk. All the tables that are exported to disk are saved in the data/schema directory of your dbv installation. You can see from the screenshot below that we currently do not have the tbl_leadinfo table in our filesystem:

Database Versioning with DBV

After exporting the newly created table to disk you have to commit it into version control:

<span>my_project/dbv</span>
Copy after login
Copy after login
Copy after login

Then you can just push it to your central repository:

<span><span><?php
</span></span><span><span>/**
</span></span><span><span> * Your database authentication information goes here
</span></span><span><span> * <span>@see http://dbv.vizuina.com/documentation/
</span></span></span><span><span> */
</span></span><span><span>define('DB_HOST', 'localhost');
</span></span><span><span>define('DB_PORT', 3306);
</span></span><span><span>define('DB_USERNAME', 'my_username');
</span></span><span><span>define('DB_PASSWORD', 'my_password');
</span></span><span><span>define('DB_NAME', 'my_database');
</span></span><span>
</span><span><span>/**
</span></span><span><span> * Authentication data for access to DBV itself
</span></span><span><span> * If you leave any of the two constants below blank, authentication will be disabled
</span></span><span><span> * <span>@see http://dbv.vizuina.com/documentation/#optional-settings
</span></span></span><span><span> */
</span></span><span><span>define('DBV_USERNAME', 'my_username');
</span></span><span><span>define('DBV_PASSWORD', 'my_password');
</span></span><span><span>?></span></span>
Copy after login
Copy after login
Copy after login

At this point you can just tell to your team mates that you have created a new table in the database and that you have already pushed it to the central repo. Now they can just pull it down to their local copy.

<span>config.php
</span><span>.buildpath
</span><span>.project
</span><span>.settings</span>
Copy after login
Copy after login

Next tell your team mate to visit the dbv page (http://localhost/your_project/dbv).
Your team mate will probably have a screen similar to the following:

Database Versioning with DBV

At this point he can just tick the checkbox next to the tbl_tasks table and click on the ‘Push to database’ button. This will create the tbl_tasks table in the database.

And that’s the workflow for working with dbv. Pretty easy, isn’t it? But what if you need to make changes to the current database schema? Maybe you missed a field or forgot to add it on a specific table before you distributed it to your team. That’s where revisions come in.

Revisions

If you’re like me and you tried updating the schema (added a new field, remove a field, updated the data type, etc.) for a specific table you might have noticed that dbv isn’t aware of it by default. For those changes you need to create a revision file. You can do that by creating a new folder in the data/revisions directory in your dbv installation. The convention for naming the folder is making use of a number. So the first time you make a revision the folder name would be 1 and then the next time it would be 2 and so on. Note that revisions are changes that can be applied to the whole database. This means that you have the freedom to modify the structure of more than one table, but its good practice to only make changes to a single table and make a revision for it. This is for you and your team to easily manage the changes and make sense of it later on. The only exception to this practice is when the changes are related to each other. In that case it would make sense to put those changes together in one revision.

Let’s try creating a new field inside the tbl_users table and name it email:

<span>git add dbv
</span><span>git commit -m "add dbv into project"</span>
Copy after login
Copy after login

Next create a new file in the data/revisions/1 directory in your dbv installation and put the query that you just executed as the contents. Name the file tbl_users.sql. The convention here is using the name of the modified table as the name for the revision file.
If you are making modifications to more than one table then create a separate file for each table.

After that you can commit the new file into your source control:

<span>git push</span>
Copy after login

And then push it to your central repository:

<span>http://localhost/your_project/dbv</span>
Copy after login

Again you can inform your team mates about the particular change. Communication is key when making changes to the database. You want to make sure that everyone on your team is on the same page as you.

Now if they access dbv from the browser they can now see the revision. All they have to do now is to tick the checkbox beside the revision and then click on the ‘Run selected revisions’ button. This will commit your changes to their local database copy:

Database Versioning with DBV

Conclusion

DBV is a nice way to easily manage your database version control needs. It lets you and your team easily keep track of the changes made in your database. It also allows for easy sharing of your changes with the rest of your team through the use of Git. This ensures that everyone always has the latest copy of the database.

In this article we have looked at using DBV with Git but you can pretty much use any version control system of your choice. Feedback? Please leave it in the comments below!

Frequently Asked Questions on Database Versioning

What are the key benefits of database versioning?

Database versioning offers several benefits. Firstly, it provides a historical record of all changes made to the database schema, which can be useful for debugging and auditing purposes. Secondly, it allows for easy rollback of changes in case of errors or issues. Thirdly, it facilitates collaboration among team members by ensuring everyone is working with the same version of the database. Lastly, it helps in maintaining consistency and integrity of the database, especially in a distributed development environment.

How does database versioning work in practice?

In practice, database versioning involves maintaining a version history of the database schema. This is typically done using a version control system like Git. Each change to the database schema is committed as a new version in the version control system. These versions are then used to update the database schema in different environments (development, testing, production, etc.) as needed.

What tools can be used for database versioning?

There are several tools available for database versioning. Some of the popular ones include Liquibase, Flyway, and DBmaestro. These tools provide features like automated schema updates, rollback capabilities, and support for multiple database types. The choice of tool depends on the specific requirements and preferences of the development team.

What are the challenges in implementing database versioning?

Implementing database versioning can be challenging due to several reasons. Firstly, it requires a change in the development process, which can be difficult to manage. Secondly, it requires careful handling of database migrations to avoid data loss or corruption. Lastly, it requires a good understanding of the database schema and the changes being made to it.

How can database versioning help in agile development?

In agile development, changes are made frequently and rapidly. Database versioning can help manage these changes effectively by providing a historical record of all changes, facilitating easy rollback of changes, and ensuring consistency across different environments. This can greatly enhance the agility and efficiency of the development process.

How does database versioning relate to DevOps?

Database versioning is a key component of DevOps, as it enables continuous integration and continuous deployment (CI/CD) of database changes. By maintaining a version history of the database schema, it allows for automated deployment of changes across different environments, thereby enhancing the speed and efficiency of the DevOps process.

Can database versioning be used with cloud databases?

Yes, database versioning can be used with cloud databases. Most database versioning tools support a wide range of database types, including cloud databases. However, the specific features and capabilities may vary depending on the tool and the type of cloud database.

What is the role of database versioning in data governance?

Database versioning plays a crucial role in data governance by ensuring the integrity and consistency of the database schema. It provides a historical record of all changes, which can be useful for auditing and compliance purposes. It also facilitates collaboration and communication among team members, which is essential for effective data governance.

How can database versioning improve database performance?

While database versioning itself does not directly improve database performance, it can help identify performance issues by providing a historical record of schema changes. This can be useful for debugging and performance tuning. Moreover, by ensuring consistency and integrity of the database schema, it can indirectly contribute to better database performance.

What are the best practices for database versioning?

Some of the best practices for database versioning include: using a version control system to maintain a version history of the database schema; committing each change as a new version; testing each version thoroughly before deployment; using automated tools for schema updates and rollbacks; and maintaining good communication and collaboration among team members.

The above is the detailed content of Database Versioning with DBV. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? What are HTTP request methods (GET, POST, PUT, DELETE, etc.) and when should each be used? Apr 09, 2025 am 12:09 AM

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

Explain the difference between self::, parent::, and static:: in PHP OOP. Explain the difference between self::, parent::, and static:: in PHP OOP. Apr 09, 2025 am 12:04 AM

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles