简体   繁体   English

MySQL Workbench 文件上的 hg 差异

[英]hg diff on MySQL Workbench files

I'm posting this as a Q&A to document a workaround for a problem that seems to come up frequently—how to put MySQL Workbench files under version control—but for which I have been unable to find any solutions.将此作为问答发布,以记录似乎经常出现的问题的解决方法 - 如何将 MySQL Workbench 文件置于版本控制之下 - 但我一直无法找到任何解决方案。 Feedback is welcome!欢迎反馈!

How can I tell Mercurial to diff the contents of a zipped archive and ignore some of the changes to those contents?我如何告诉 Mercurial diff压缩存档的内容并忽略对这些内容的一些更改? Specifically, how can I use hg to diff the contents of a MySQL Workbench ( .mwb ) file, ignoring the many unimportant changes that MySQL Workbench makes every time the file is opened?具体来说,我如何使用hg.mwb MySQL Workbench ( .mwb ) 文件的内容,忽略 MySQL Workbench 每次打开文件时所做的许多不重要的更改? Can I use a custom script that ignores certain irrelevant changes?我可以使用忽略某些不相关更改的自定义脚本吗?

Background背景

I am trying to diff a file in an hg repository.我正在尝试diff hg 存储库中的文件。 The file, document.mwb.xml , is an XML document extracted from a .mwb file (a MySQL Workbench model file).文件document.mwb.xml是从.mwb文件(MySQL Workbench 模型文件)中提取的 XML 文档。 Basically, I am looking to keep the model's contents—the table structure, visual model, etc.—under version control, but not commit the .mwb file itself, which is a zip archive and thus a binary file.基本上,我希望将模型的内容(表结构、可视模型等)置于版本控制之下,但不提交.mwb文件本身,它是一个 zip 存档,因此是一个二进制文件。

Anytime I save the .mwb file, I unzip it. .mwb我保存.mwb文件时,我都会解压它。 I keep the unzipped contents in my repository, and just zip them up again when I need to work with the .mwb in MySQL.我将解压缩的内容保存在我的存储库中,当我需要使用 MySQL 中的.mwb时,只需再次压缩它们。

The XML in question looks like this:有问题的 XML 如下所示:

<?xml version="1.0"?>
<data grt_format="2.0" document_type="MySQL Workbench Model" version="1.4.4">
  <value type="object" struct-name="workbench.Document" id="8551CCFA-3AD0-4207-BC76-15ED589CF22C" struct-checksum="0x7131bf99">
    <value type="object" struct-name="workbench.logical.Model" id="B48E1CD2-3386-40B7-8E59-AA191598F667" struct-checksum="0xf4220370" key="logicalModel">
      <value _ptr_="0x7fbcd1cc3270" type="list" content-type="object" content-struct-name="workbench.logical.Diagram" key="diagrams"/>
      <value _ptr_="0x7fbcd1cc3210" type="dict" key="customData"/>
      <value _ptr_="0x7fbcd1cc32d0" type="list" content-type="object" content-struct-name="model.Marker" key="markers"/>
      <value _ptr_="0x7fbcd1cc3330" type="dict" key="options"/>
      <value type="string" key="name"></value>
      <link type="object" struct-name="GrtObject" key="owner">8551CCFA-3AD0-4207-BC76-15ED589CF22C</link>
    </value>
    <value _ptr_="0x7fbcd1cc2b70" type="list" content-type="object" content-struct-name="workbench.OverviewPanel" key="overviewPanels"/>
    <value _ptr_="0x7fbcd1cc2c00" type="list" content-type="object" content-struct-name="workbench.physical.Model" key="physicalModels">
      <value type="object" struct-name="workbench.physical.Model" id="34B9E967-5C9B-4D1B-8759-C417F6C33AA3" struct-checksum="0x5f896d18">
...

The problem is all of those _ptr_ attributes: there are literally thousands of them in this file, and every one of them changes every single time the file is saved, even if nothing is modified.问题在于所有这些_ptr_属性:这个文件中实际上有数千个属性,并且每次保存文件时,每个属性都会更改,即使没有进行任何修改。 As a result, the repository can rapidly get cluttered with completely meaningless "changes" to this file.因此,存储库可能会因对该文件的完全无意义的“更改”而迅速变得混乱。

Is there a way to use a custom diff routine to ignore these irrelevant changes?有没有办法使用自定义diff例程来忽略这些不相关的更改?

I have not found a true solution, but I have developed a satisfactory workaround, inspired by this mwb-diff gist .我还没有找到真正的解决方案,但我开发了一个令人满意的解决方法,灵感来自这个mwb-diff gist This allows me to unzip and diff the .mwb file's contents, commit those contents and their changes to the repository, and use the .mwb normally when necessary.这允许我解压缩和比较.mwb文件的内容,将这些内容及其更改提交到存储库,并在必要时正常使用.mwb

Project Structure项目结构

My project is set up like this:我的项目是这样设置的:

project_root
    /dist
    /schema
    /src
    /test

I save the .mwb file - call it MyModel.mwb - in project_root/schema .我在project_root/schema保存.mwb文件 - 将其MyModel.mwb Obviously, you can use a different structure, but you will need to modify the instructions below accordingly.显然,您可以使用不同的结构,但您需要相应地修改下面的说明。

The Scripts脚本

I created the following scripts and saved them in project_root/schema :我创建了以下脚本并将它们保存在project_root/schema

unpack.sh解压.sh

#!/bin/bash

# Unzip the model (MyModel.mwb) into a particular directory (project_root/schema/MyModel)
unzip -o MyModel.mwb -d MyModel/

# Replace all _ptr_="...." attributes with _ptr_="xxx"
sed -i presed -E 's/_ptr_="0x[0-9a-f]+"/_ptr_="xxx"/g' MyModel/document.mwb.xml

pack.sh打包文件

#!/bin/bash

# This file goes into the directory containing the model contents, zips them up, and saves them as a .mwb model

cd MyModel/
zip -r ../MyModel.mwb ./* -x lock
cd ..

Getting Mercurial Ready to Rock让 Mercurial 准备好摇滚

We need to tell hg to ignore the model (and all other .mwb files).我们需要告诉hg忽略模型(以及所有其他.mwb文件)。 Also, when MySQL Workbench is open, it adds a lock file to the .mwb archive, which we need to ignore.此外,当 MySQL Workbench 打开时,它会向.mwb存档添加一个lock文件,我们需要忽略它。 So, add these lines to your .hgignore file:因此,将这些行添加到您的.hgignore文件中:

*.mwb
*.mwb.bak
schema/MyModel/lock

An Aside About the data.db File关于data.db文件的data.db

Optionally, also ignore the data.db file (a SQLite database) in the .mwb file.任选地,也忽略data.db在文件(SQLite数据库) .mwb文件。 It is a binary file that contains any INSERT s or other non-create SQL statements that are part of your model.它是一个二进制文件,其中包含作为模型一部分的任何INSERT或其他非创建 SQL 语句。 As a rule, I don't use MySQL Workbench for this stuff;通常,我不使用 MySQL Workbench 来处理这些东西; I use it only to create and edit tables, views, etc. So, I added this line to .hgignore :我仅使用它来创建和编辑表、视图等。因此,我将此行添加到.hgignore

 schema/MyModel/data.db

If you want to track changes to the data.db file, you may need to modify this workaround.如果要跟踪对data.db文件的更改,您可能需要修改此解决方法。

How to Use the Scripts如何使用脚本

When you want to modify the .mwb file, rebuild it from its components by running pack.sh above.当您想要修改.mwb文件时,请通过运行上面的pack.sh从其组件重建它。 This could be added as a hook to happen automatically when you hg pull , update, etc., but I haven't explored this, yet.这可以添加为一个钩子,当你hg pull 、 update 等时自动发生,但我还没有探索过这个。

When you are done editing the .mwb file and want to commit your changes, run the unpack.sh script.完成.mwb文件的编辑并想要提交更改后,请运行unpack.sh脚本。 If you want, you can set up a file-watching utility on your system to do this automatically when the file changes, but that's beyond the scope of this answer.如果需要,您可以在系统上设置文件监视实用程序,以便在文件更改时自动执行此操作,但这超出了本答案的范围。

The Results结果

Mercurial is now perfectly happy to track changes to the contents of your .mwb file without tracking thousands of apparently-useless _ptr_ attributes. Mercurial 现在非常乐意跟踪对.mwb文件内容的更改,而无需跟踪数千个明显无用的_ptr_属性。 Also, while I am using this with Mercurial, the basic logic (and the shell scripts) will work with git, SVN, etc.此外,当我将它与 Mercurial 一起使用时,基本逻辑(和 shell 脚本)将与 git、SVN 等一起使用。

IMPORTANT CAVEAT: As far as I can tell, the _ptr_ attributes are irrelevant.重要警告:据我所知, _ptr_属性无关紧要。 The scripts I have posted above actually replace the contents of those attributes.我上面发布的脚本实际上替换了这些属性的内容。 _ptr_="0x98a7b3e4" (or whatever) becomes _ptr_"xxx" . _ptr_="0x98a7b3e4" (或其他)变成_ptr_"xxx" Based on my testing, this doesn't matter, and MySQL Workbench will happily work with the reconstituted file, apparently disregarding the _ptr_ values.根据我的测试,这无关紧要,而且 MySQL Workbench 会很乐意处理重建的文件,显然会忽略_ptr_值。 I may be wrong, and these values may matter!我可能是错的,这些值可能很重要! You are strongly encouraged to test this for yourself before relying on my solution.强烈建议您在依赖我的解决方案之前自行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM