首页 > 解决方案 > 在哪里更改活动模型 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 可以影响格式。但到目前为止,以下事情对我来说并不奏效:

标签: ruby-on-railsrubyactive-model-serializers

解决方案


推荐阅读