Module dbcrossbarlib::schema

source ·
Expand description

Our “interchange” format for database table schemas.

To convert table schemas between different databases, we have a choice:

  1. We can convert between each pair of schema formats directly, which would require 2*n*(n-1) conversions for n databases.
  2. We can define an “interchange” format, and then build n input conversions and n output conversions. This is much simpler.

A good interchange format should be rich enough to include the most common database types, including not just obvious things like text and integers, but also things like timestamps and geodata. But a good interchange format should also be as simple as possible, omitting details that generally don’t translate well.

Inevitably, this means that we’re going to wind up with a subjective and opinionated design.

We define our format using Rust data structures, which are serialized and deserialized using serde.

use dbcrossbarlib::schema::Schema;
use serde_json;

let json = r#"
{
  "named_data_types": [{
    "name": "color",
    "data_type": { "one_of": ["red", "green", "blue"] }
  }],
  "tables": [{
    "name": "example",
    "columns": [
      { "name": "a", "is_nullable": true,  "data_type": "text" },
      { "name": "b", "is_nullable": true,  "data_type": "int32" },
      { "name": "c", "is_nullable": false, "data_type": "uuid" },
      { "name": "d", "is_nullable": true,  "data_type": "date" },
      { "name": "e", "is_nullable": true,  "data_type": "float64" },
      { "name": "f", "is_nullable": true,  "data_type": { "array": "text" } },
      { "name": "g", "is_nullable": true,  "data_type": { "geo_json": 4326 } },
      { "name": "h", "is_nullable": true,  "data_type": { "struct": [
        { "name": "x", "data_type": "float64", "is_nullable": false },
        { "name": "y", "data_type": "float64", "is_nullable": false }
      ] } },
      { "name": "i", "is_nullable": false, "data_type": { "named": "color" }}
    ]
  }]
}
"#;

let schema = serde_json::from_str::<Schema>(json).expect("could not parse JSON");

Structs

  • Information about a column.
  • A named data type or type alias. This is used for things like named Postgres enums.
  • Information about about a table and any supporting types. This is the “top level” of our JSON schema format.
  • An SRID number specifying how to intepret geographical coordinates.
  • Information about a named field.
  • Information about a table.

Enums