如何将 MySQL 代码纳入源代码管理?

发布于 2024-08-30 14:33:12 字数 111 浏览 6 评论 0原文

我知道我可以手动将所有 MySQL 代码复制到文件中,然后将这些文件放入源代码管理中。但是有什么办法可以自动执行此操作吗?

我想对存储过程执行此操作,但也对表/事件/触发器创建脚本执行此操作。

I know I can copy all my MySQL code manually to files and then put those files into source control. But is there any way to do this automatically?

I would like to do this to stored procedures, but also to table/event/trigger creation scripts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

留蓝 2024-09-06 14:33:12

您可以创建数据更改触发器,这会将更改自动存储到某些源代码管理中。但是,没有自动方法来跟踪结构更改(表、存储过程等)。因此,最好的方法可能是转储数据库并将这些转储存储在源代码管理中。您可以定期执行此操作以使事情自动化。

You can create triggers on data change, which would store the change automatically to some source control. However there is no automatic way to track structure changes (tables, stored procedures and so on) this way. So probably the best way is to dump database and store these dumps in source control. You can do this periodically to automate the things.

根据 Michal 的回答,我到目前为止使用的解决方案是:

#!/bin/bash
BACKUP_PATH=/root/database_name
DATABASE=database_name
PASSWORD=Password
rm -f "$BACKUP_PATH/*.sql"
mysqldump -p$PASSWORD --routines --skip-dump-date --no-create-info --no-data --skip-opt $DATABASE > $BACKUP_PATH/$DATABASE.sql
mysqldump -p$PASSWORD --tab=$BACKUP_PATH --skip-dump-date --no-data --skip-opt $DATABASE
hg commit -Am "automatic commit" $BACKUP_PATH

Based on Michal answer, the solution I am using so far is:

#!/bin/bash
BACKUP_PATH=/root/database_name
DATABASE=database_name
PASSWORD=Password
rm -f "$BACKUP_PATH/*.sql"
mysqldump -p$PASSWORD --routines --skip-dump-date --no-create-info --no-data --skip-opt $DATABASE > $BACKUP_PATH/$DATABASE.sql
mysqldump -p$PASSWORD --tab=$BACKUP_PATH --skip-dump-date --no-data --skip-opt $DATABASE
hg commit -Am "automatic commit" $BACKUP_PATH
生生漫 2024-09-06 14:33:12

不太明白你想做什么。

看看Liquibase,也许它会满足你的需要......

Don't really understand what you'r trying to do.

Look at Liquibase, perhaps it will do what you need...

分开我的手 2024-09-06 14:33:12

PHP

如果您处于 PHP 环境中,此脚本会将过程和函数存储在单独的 (PHP) 文件中,这些文件可用于生成它们并存储在存储库中。

<?php declare(strict_types=1);

// logon the db user, I'm using a mysqli version here
// PDO will need small adjustments.
require "../inc/config.inc.php";

function show_routine(string $type, string $name) {
  global $db;
  $query = "SHOW CREATE $type `$name`";
  $result = $db->query( $query );
  foreach( $result as $row ) {
    echo "<h1>$name</h1>";
    if( is_null($row["Create $type"]) ) {
      echo "<p>Not found</p>";
      continue;
    }
    $proc   = $row[$type];
    $source = $row["Create $type"];
    $source = preg_replace( "/(DEFINER=`\w*`@`\w*`)/", "/* $1 */", $source );
    echo "<pre>$source</pre>";
    $fh = fopen( "./$type/$name.sql", "w" );
    fwrite( $fh, "DROP $type IF EXISTS `$name`;" . PHP_EOL . PHP_EOL );
    fwrite( $fh, "DELIMITER $" . PHP_EOL );
    fwrite( $fh, $source );
    fwrite( $fh, "$" . PHP_EOL );
    fwrite( $fh, "DELIMITER ;" . PHP_EOL );
    fclose( $fh );
  }
}


foreach( $db->query("show procedure status where db = '<your db-scheme>'") as $row ) {
  $name = $row["Name"];
  show_routine("Procedure", $name);
}


foreach( $db->query("show function status where db = '<your-db-scheme>'") as $row ) {
  $name = $row["Name"];
  show_routine("Function", $name);
}

确保 db 用户具有适当的权限。

PHP

If you are in a PHP environment this script will store the procedures and function in separate (PHP) files which can be use to generated them and stored in a repo.

<?php declare(strict_types=1);

// logon the db user, I'm using a mysqli version here
// PDO will need small adjustments.
require "../inc/config.inc.php";

function show_routine(string $type, string $name) {
  global $db;
  $query = "SHOW CREATE $type `$name`";
  $result = $db->query( $query );
  foreach( $result as $row ) {
    echo "<h1>$name</h1>";
    if( is_null($row["Create $type"]) ) {
      echo "<p>Not found</p>";
      continue;
    }
    $proc   = $row[$type];
    $source = $row["Create $type"];
    $source = preg_replace( "/(DEFINER=`\w*`@`\w*`)/", "/* $1 */", $source );
    echo "<pre>$source</pre>";
    $fh = fopen( "./$type/$name.sql", "w" );
    fwrite( $fh, "DROP $type IF EXISTS `$name`;" . PHP_EOL . PHP_EOL );
    fwrite( $fh, "DELIMITER $" . PHP_EOL );
    fwrite( $fh, $source );
    fwrite( $fh, "$" . PHP_EOL );
    fwrite( $fh, "DELIMITER ;" . PHP_EOL );
    fclose( $fh );
  }
}


foreach( $db->query("show procedure status where db = '<your db-scheme>'") as $row ) {
  $name = $row["Name"];
  show_routine("Procedure", $name);
}


foreach( $db->query("show function status where db = '<your-db-scheme>'") as $row ) {
  $name = $row["Name"];
  show_routine("Function", $name);
}

Make sure the db user has the proper rights.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文