dbv数据库乱码_使用DBV进行数据库版本控制

本文介绍了DBV,一个用PHP编写的MySQL数据库版本控制系统。DBV依赖于Git等版本控制系统来跟踪和共享数据库变更,适用于团队协作。文章涵盖了安装DBV、配置、跟踪的变更以及工作流程,强调了版本控制在数据库管理中的重要性。
摘要由CSDN通过智能技术生成

dbv数据库乱码

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.

在本文中,我们将介绍DBV ,这是一个用PHP编写MySQL数据库版本控制系统,用于MySQL数据库,因此您需要先安装PHP和MySQL,再使用Web服务器(例如Apache或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.

关于此软件的重要说明是,它不是独立的数据库版本控制系统,因为它需要一个版本控制系统(例如Git,Mercurial或SVN)来与您的团队同步更改。

安装DBV (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:

要开始使用DBV,首先必须从他们的网站下载安装程序,将其解压缩到项目目录中,然后将结果文件夹重命名为dbv 。 这将为您提供以下路径:

my_project/dbv

An alternative approach is just cloning from Github.

另一种方法是从Github克隆。

DBV配置 (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.

您可以通过创建config.php.sample文件的副本并将其重命名为config.php来开始配置DBV的选项。

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:

这里最重要的更新是前两个部分。 只需将my_usernamemy_passwordmy_database的值my_database为当前数据库配置中的值:

<?php
/**
 * Your database authentication information goes here
 * @see http://dbv.vizuina.com/documentation/
 */
define('DB_HOST', 'localhost');
define('DB_PORT', 3306);
define('DB_USERNAME', 'my_username');
define('DB_PASSWORD', 'my_password');
define('DB_NAME', 'my_database');

/**
 * Authentication data for access to DBV itself
 * If you leave any of the two constants below blank, authentication will be disabled
 * @see http://dbv.vizuina.com/documentation/#optional-settings
 */
define('DBV_USERNAME', 'my_username');
define('DBV_PASSWORD', 'my_password');
?>

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

上面的配置文件中的第一部分是关于机器中MySQL数据库详细信息的。

The second section are the login details for dbv itself.

第二部分是dbv本身的登录详细信息。

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

接下来打开.gitignore文件。 默认情况下,它包含以下内容:

config.php
.buildpath
.project
.settings

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.

这些是Git将忽略的文件。 但是,如果您知道与团队伙伴具有相同的数据库信息(用户,密码,数据库名称),则可以从.gitignore文件中删除config.php 。 否则,他们将必须创建自己的配置文件并将其从源代码管理中排除。

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.

接下来,您还必须将data/meta/revision文件添加到.gitignore ,因为dbv在该文件中放置有关数据库本地副本的信息。 您的队友可能会有所不同,因此需要将其从源代码管理中排除。

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

完成配置后,现在可以将dbv添加到版本控制中:

git add dbv
git commit -m "add dbv into project"

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

然后将其推送到中央存储库,以供团队的其他成员拉出:

git push
要跟踪哪些变化? (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:

在继续实际使用dbv之前。 我想稍微谈一下要跟踪的更改。 在数据库世界中,几乎所有更改都可以放入源代码管理中。 其中包括以下内容:

  • 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工作流程 (DBV Workflow)

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

您可以通过访问以下URL从浏览器启动dbv:

http://localhost/your_project/dbv

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

或者,如果您定义了虚拟主机,则通过访问其URL。

This will give you an interface similar to the following:

这将为您提供类似于以下内容的界面:

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.

从上面的屏幕截图中,您将看到前面在config.php提供的数据库中当前存在的表。 还有一个In DB字段,用于显示数据库中当前是否存在特定的表,而On disk字段则显示当前表是否已保存在文件系统中。 通过此信息,您几乎可以知道当前是否拥有数据库的最新副本。

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.

使用dbv时要记住的重要一点是,对数据库的本地副本所做的任何更改都应具有可提交到源代码管理中的本地副本。

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:

这意味着,如果您在数据库中创建新表,则必须将其导出到磁盘。 所有导出到磁盘的表都保存在dbv安装的data/schema目录中。 您可以从下面的屏幕快照中看到,当前文件系统中没有tbl_leadinfo表:

disk copy

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

将新创建的表导出到磁盘后,必须将其提交到版本控制中:

git add data/schema/tbl_tasks.sql
git commit -m "add tbl_task table"

Then you can just push it to your central repository:

然后,您可以将其推送到中央存储库:

git push

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.

此时,您可以告诉队友您已经在数据库中创建了一个新表,并且已经将其推送到中央存储库中。 现在,他们可以pull其下拉至本地副本。

git pull

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:

接下来,告诉您的队友访问dbv页面( http://localhost/your_project/dbv )。 您的队友的屏幕可能类似于以下内容:

dbv team mate

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.

此时,他只需勾选tbl_tasks表旁边的复选框,然后单击“推送到数据库”按钮即可。 这将在数​​据库中创建tbl_tasks表。

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.

这就是使用dbv的工作流程。 很简单,不是吗? 但是,如果您需要更改当前数据库架构该怎么办? 也许您错过了一个字段,或者忘记了将其添加到特定的表上,然后再分配给团队。 这就是修订的地方。

修订版 (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.

如果您像我一样,并且尝试为特定表更新模式(添加了新字段,删除了字段,更新了数据类型等),您可能已经注意到dbv默认情况下不知道它。 对于这些更改,您需要创建一个修订文件。 您可以通过在dbv安装的data/revisions目录中创建一个新文件夹来做到这一点。 命名文件夹的惯例是使用数字。 因此,第一次进行修订时,文件夹名称将为1 ,然后下次将其命名为2 ,依此类推。 请注意,修订是可以应用于整个数据库的更改。 这意味着您可以自由修改多个表的结构,但是它的优良作法是仅对单个表进行更改并对其进行修改。 这使您和您的团队可以轻松地管理更改并在以后加以利用。 这种做法的唯一例外是更改之间相互关联。 在这种情况下,将这些更改放在一起进行修订是有意义的。

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

让我们尝试在tbl_users表中创建一个新字段并将其命名为email

ALTER TABLE `tbl_users`
ADD `email` varchar(160) COLLATE 'latin1_swedish_ci' NOT NULL

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.

接下来,在dbv安装的data/revisions/1目录中创建一个新文件,并将刚刚执行的查询作为内容。 将文件命名为tbl_users.sql 。 这里的约定使用修改后的表的名称作为修订文件的名称。 如果要对多个表进行修改,请为每个表创建一个单独的文件。

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

之后,您可以将新文件提交到源代码管理中:

git add data/revisions/1/tbl_users.sql
git commit -m "add email field to tbl_users"

And then push it to your central repository:

然后将其推送到中央存储库:

git push

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:

现在,如果他们从浏览器访问dbv,他们现在可以看到修订。 他们现在要做的就是勾选修订旁边的复选框,然后单击“运行选定的修订”按钮。 这会将您的更改提交到其本地数据库副本:

dbv revisions

结论 (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.

DBV是轻松管理数据库版本控制需求的好方法。 它使您和您的团队可以轻松地跟踪数据库中所做的更改。 它还可以通过使用Git与其他团队轻松共享您的更改。 这样可以确保每个人始终拥有数据库的最新副本。

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!

在本文中,我们研究了如何将DBV与Git一起使用,但是您几乎可以选择使用任何版本控制系统。 反馈? 请在下面的评论中留下!

翻译自: https://www.sitepoint.com/database-versioning-dbv/

dbv数据库乱码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值