我正在使用YAML::dump
(或多或少)来跟踪git存储库中对数据库的更改的简单方法。实际上,这意味着我将数据库的某些部分“转储”到一组YAML文件中,提交它们,然后可以非常轻松地跟踪它们。总的来说,这很好,我对整个过程感到满意。
唯一的问题是嵌入的JSON
文档。这些存储的数据方面在关系模型中根本无法很好地完成,有时非常复杂。 YAML::dump
将它们序列化为单行字符串(从存储角度看,这似乎是最佳选择),但对进行区分很可怕。以下示例显示了我当前的输出:
--- !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
属性的格式正确的字符串表示形式。
我怀疑幕后某个地方正在发生“铁轨魔术”。我确实希望有一个API可以影响格式。但是,到目前为止,以下事情对我来说还行不通:
*_before_type_cast
方法。 YAML::dump
不会调用此方法。to_yaml
。也不调用此方法。ActiveModel::AttributeSet::YAMLEncoder
的信息,但不知道应如何/在何处使用它。或就此而言:YAML::dump
最终以某种方式使用了它。它似乎是我所看到的一般格式的原因。