ruby-on-rails - 在哪里更改活动模型 YAML 表示的格式?
问题描述
我YAML::dump
过去有一种(或多或少)简单的方法来跟踪 git 存储库中数据库的更改。实际上,这意味着我将数据库的一部分“转储”到一组 YAML 文件中,提交它们,然后可以很容易地跟踪它们。总的来说,这很好用,我对整个过程很满意。
唯一的问题是嵌入式JSON
文档。这些存储数据的各个方面在关系模型中根本无法很好地工作,并且有时非常复杂。YAML::dump
将它们序列化为单行字符串(从存储的角度来看这似乎是最佳的),但对于 diffing 来说是可怕的。以下示例显示了我当前的输出:
--- !ruby/object:Grammar
concise_attributes:
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: name
value_before_type_cast: Dynamisches XML
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: slug
value_before_type_cast: dxml
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: id
value_before_type_cast: 7e333dff-6d1c-4042-aaa5-0cdf2cfeed7e
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: model
value_before_type_cast: '{"root": "element", "types": {"if": {"type": "concrete",
"attributes": [{"name": "condition", "type": "allowed", "nodeTypes": ["expr"]},
{"name": "body", "type": "allowed", "nodeTypes": [{"occurs": "*", "nodeType":
"element"}, {"occurs": "*", "nodeType": "text"}, {"occurs": "*", "nodeType": "interpolate"},
{"occurs": "*", "nodeType": "if"}]}]}, "expr": {"type": "oneOf", "oneOf": ["exprVar",
"exprConst", "exprBinary"]}, "text": {"type": "concrete", "attributes": [{"base":
"string", "name": "value", "type": "property"}]}, "element": {"type": "concrete",
"attributes": [{"name": "tag-open-begin", "type": "terminal", "symbol": "<"},
{"base": "string", "name": "name", "type": "property"}, {"name": "attributes",
"type": "allowed", "nodeTypes": [{"occurs": "*", "nodeType": "attribute"}]}, {"name":
"tag-open-end", "type": "terminal", "symbol": ">"}, {"name": "elements", "type":
"allowed", "nodeTypes": [{"occurs": "*", "nodeType": "element"}, {"occurs": "*",
"nodeType": "text"}, {"occurs": "*", "nodeType": "interpolate"}, {"occurs": "*",
"nodeType": "if"}]}, {"name": "tag-close", "type": "terminal", "symbol": "<ende/>"}]},
"exprVar": {"type": "concrete", "attributes": [{"base": "string", "name": "name",
"type": "property"}]}, "attribute": {"type": "concrete", "attributes": [{"base":
"string", "name": "name", "type": "property"}, {"name": "equals", "type": "terminal",
"symbol": "="}, {"name": "quot-begin", "type": "terminal", "symbol": "\""}, {"name":
"value", "type": "allowed", "nodeTypes": [{"occurs": "*", "nodeType": "text"},
{"occurs": "*", "nodeType": "interpolate"}]}, {"name": "quot-end", "type": "terminal",
"symbol": "\""}]}, "exprConst": {"type": "concrete", "attributes": [{"base": "string",
"name": "name", "type": "property"}]}, "exprBinary": {"type": "concrete", "attributes":
[{"name": "lhs", "type": "allowed", "nodeTypes": ["expr"]}, {"name": "operator",
"type": "allowed", "nodeTypes": ["binaryOperator"]}, {"name": "rhs", "type": "allowed",
"nodeTypes": ["expr"]}]}, "interpolate": {"type": "concrete", "attributes": [{"name":
"expr", "type": "allowed", "nodeTypes": ["expr"]}]}, "binaryOperator": {"type":
"concrete", "attributes": [{"base": "string", "name": "operator", "type": "property"}]}}}'
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: created_at
value_before_type_cast: '2018-05-15 18:48:12.004676'
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: updated_at
value_before_type_cast: '2018-12-20 15:39:19.237639'
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: programming_language_id
value_before_type_cast: dxml-eruby
- !ruby/object:ActiveModel::Attribute::FromDatabase
name: technical_name
value_before_type_cast: dxml
new_record: false
active_record_yaml_version: 2
一个“典型的”差异(即使只有微小的变化),看起来像这样:
因此,我宁愿存储格式良好的model
-attribute 字符串表示形式。
我怀疑在幕后某处发生了一些“Rails 魔法”。我确实希望有一个 API 可以影响格式。但到目前为止,以下事情对我来说并不奏效:
*_before_type_cast
在我的模型类上定义一个匹配方法。不调用此方法YAML::dump
。to_yaml
在我的模型类上定义。这个方法也没有被调用。- 我发现
ActiveModel::AttributeSet::YAMLEncoder
但无法弄清楚应该如何/在哪里使用它。或者就此而言:最终如何YAML::dump
以某种方式使用它。它似乎对我看到的一般格式负责。
解决方案
推荐阅读
- swift - Xcode Wkwebview 未加载
- tensorflow - Tensorflow 2.0 将 nlp 的预处理 tonkezier 保存到 tensorflow 服务器中
- python - Сan你解释为什么我得到一个错误的全球化变量?
- javascript - 如何编写一个Thunderbird扩展(webextension)来修改消息显示?
- javascript - 内容变化的平滑过渡
- python - VSCode Python 测试失败错误:找不到文件:./test_mything_plugin.py::test_get_conn
- javascript - 如何在Vue中显示带尖括号的字符串?
- php - 如何从 csv 数据中对大型多维数组进行排序?
- c# - 使 VS Code 自动将大括号移动到下一行
- python - 如何使用 FunctionType 定义带有位置参数的函数?