5个不稳定版本

0.3.0 2024年8月2日
0.2.0 2023年7月20日
0.2.0-rc.22023年5月18日
0.2.0-rc.12023年3月22日
0.1.0 2021年4月8日

#25 in #schema-definition

Download history 26116/week @ 2024-05-03 30377/week @ 2024-05-10 29988/week @ 2024-05-17 29115/week @ 2024-05-24 30318/week @ 2024-05-31 38859/week @ 2024-06-07 27123/week @ 2024-06-14 28133/week @ 2024-06-21 33194/week @ 2024-06-28 32458/week @ 2024-07-05 26705/week @ 2024-07-12 28303/week @ 2024-07-19 27720/week @ 2024-07-26 31438/week @ 2024-08-02 37027/week @ 2024-08-09 39355/week @ 2024-08-16

142,053 每月下载量
79 个crate中使用了(通过 sea-schema

MIT/Apache

7KB
124

SeaSchema

🌿 SQL模式定义和发现

crate docs build status

关于

SeaSchema是一个帮助您管理MySQL、Postgres和SQLite数据库模式的库。它提供1)用于表示数据库模式映射的类型定义,每个数据库都与之紧密相连;2)用于发现它们的实用工具。

GitHub stars 如果您喜欢我们所做的工作,请考虑为我们点赞、评论、分享和贡献!

Discord 加入我们的Discord服务器,与其他SeaQL社区成员聊天!

架构

该crate被分为不同的模块

  • def:类型定义
  • queryparser:用于查询和解析information_schema
  • discovery:连接到实时数据库并发现一个Schema
  • writer:用于将Schema导出到SeaQuery和SQL语句

可以通过with-serde启用类型定义上的JSON序列化和反序列化。

模式发现

以MySQL Sakila示例数据库为例,给定以下表

CREATE TABLE film_actor (
  actor_id SMALLINT UNSIGNED NOT NULL,
  film_id SMALLINT UNSIGNED NOT NULL,
  last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY  (actor_id,film_id),
  KEY idx_fk_film_id (`film_id`),
  CONSTRAINT fk_film_actor_actor FOREIGN KEY (actor_id) REFERENCES actor (actor_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT fk_film_actor_film FOREIGN KEY (film_id) REFERENCES film (film_id) ON DELETE RESTRICT ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

发现的模式结果

TableDef {
    info: TableInfo {
        name: "film_actor",
        engine: InnoDb,
        auto_increment: None,
        char_set: Utf8Mb4,
        collation: Utf8Mb40900AiCi,
        comment: "",
    },
    columns: [
        ColumnInfo {
            name: "actor_id",
            col_type: SmallInt(
                NumericAttr {
                    maximum: None,
                    decimal: None,
                    unsigned: Some(true),
                    zero_fill: None,
                },
            ),
            null: false,
            key: Primary,
            default: None,
            extra: ColumnExtra {
                auto_increment: false,
                on_update_current_timestamp: false,
                generated: false,
                default_generated: false,
            },
            expression: None,
            comment: "",
        },
        ColumnInfo {
            name: "film_id",
            col_type: SmallInt(
                NumericAttr {
                    maximum: None,
                    decimal: None,
                    unsigned: Some(true),
                    zero_fill: None,
                },
            ),
            null: false,
            key: Primary,
            default: None,
            extra: ColumnExtra {
                auto_increment: false,
                on_update_current_timestamp: false,
                generated: false,
                default_generated: false,
            },
            expression: None,
            comment: "",
        },
        ColumnInfo {
            name: "last_update",
            col_type: Timestamp(TimeAttr { fractional: None }),
            null: false,
            key: NotKey,
            default: Some(ColumnDefault::CurrentTimestamp),
            extra: ColumnExtra {
                auto_increment: false,
                on_update_current_timestamp: true,
                generated: false,
                default_generated: true,
            },
            expression: None,
            comment: "",
        },
    ],
    indexes: [
        IndexInfo {
            unique: false,
            name: "idx_fk_film_id",
            parts: [
                IndexPart {
                    column: "film_id",
                    order: Ascending,
                    sub_part: None,
                },
            ],
            nullable: false,
            idx_type: BTree,
            comment: "",
            functional: false,
        },
        IndexInfo {
            unique: true,
            name: "PRIMARY",
            parts: [
                IndexPart {
                    column: "actor_id",
                    order: Ascending,
                    sub_part: None,
                },
                IndexPart {
                    column: "film_id",
                    order: Ascending,
                    sub_part: None,
                },
            ],
            nullable: false,
            idx_type: BTree,
            comment: "",
            functional: false,
        },
    ],
    foreign_keys: [
        ForeignKeyInfo {
            name: "fk_film_actor_actor",
            columns: [ "actor_id" ],
            referenced_table: "actor",
            referenced_columns: [ "actor_id" ],
            on_update: Cascade,
            on_delete: Restrict,
        },
        ForeignKeyInfo {
            name: "fk_film_actor_film",
            columns: [ "film_id" ],
            referenced_table: "film",
            referenced_columns: [ "film_id" ],
            on_update: Cascade,
            on_delete: Restrict,
        },
    ],
}

许可证

许可协议为以下之一

任选其一。

贡献

除非您明确声明,否则您提交给作品以包含在内的任何贡献,根据Apache-2.0许可证定义,应双重许可如上所述,不得附加任何额外条款或条件。

SeaSchema 是一个社区驱动的项目。我们欢迎您参与、贡献,共同为 Rust 的未来贡献力量。

向我们的贡献者致以崇高的敬意

Contributors

依赖项

~310–760KB
~18K SLoC