adds schema view and schema generating command

This commit is contained in:
2026-11-03 09:35:41 +01:00
parent cc41decccb
commit 600d4874d2
62 changed files with 4416 additions and 105 deletions
@@ -0,0 +1,80 @@
import Foundation
import HelderCore
enum SchemaFixtures {
/// A version 1 dump as the command writes it: users ← conversations ← messages, plus a unique email.
static let contract = #"""
{
"version": 1,
"connection": "mysql",
"driver": "mysql",
"database": "odynt",
"generated_at": "2026-11-02T14:32:05+01:00",
"tables": [
{
"name": "users",
"rows": 1284,
"columns": [
{"name": "id", "type_name": "bigint", "type": "bigint unsigned", "collation": null, "nullable": false, "default": null, "auto_increment": true, "comment": null, "generation": null},
{"name": "email", "type_name": "varchar", "type": "varchar(255)", "nullable": false, "default": null, "auto_increment": false, "comment": null},
{"name": "email_verified_at", "type_name": "timestamp", "type": "timestamp", "nullable": true, "default": null, "auto_increment": false, "comment": null}
],
"indexes": [
{"name": "primary", "columns": ["id"], "type": "btree", "unique": true, "primary": true},
{"name": "users_email_unique", "columns": ["email"], "type": "btree", "unique": true, "primary": false}
],
"foreign_keys": []
},
{
"name": "messages",
"rows": 214330,
"columns": [
{"name": "id", "type_name": "bigint", "type": "bigint unsigned", "nullable": false, "default": null, "auto_increment": true, "comment": null},
{"name": "conversation_id", "type_name": "bigint", "type": "bigint unsigned", "nullable": false, "default": null, "auto_increment": false, "comment": null},
{"name": "content", "type_name": "longtext", "type": "longtext", "nullable": false, "default": null, "auto_increment": false, "comment": "the body"}
],
"indexes": [
{"name": "primary", "columns": ["id"], "type": "btree", "unique": true, "primary": true}
],
"foreign_keys": [
{"name": "messages_conversation_id_foreign", "columns": ["conversation_id"], "foreign_schema": "odynt", "foreign_table": "conversations", "foreign_columns": ["id"], "on_update": "no action", "on_delete": "cascade"}
]
},
{
"name": "conversations",
"rows": null,
"columns": [
{"name": "id", "type_name": "bigint", "type": "bigint unsigned", "nullable": false, "default": null, "auto_increment": true, "comment": null},
{"name": "user_id", "type_name": "bigint", "type": "bigint unsigned", "nullable": true, "default": null, "auto_increment": false, "comment": null},
{"name": "archived", "type_name": "tinyint", "type": "tinyint(1)", "nullable": false, "default": "0", "auto_increment": false, "comment": null},
{"name": "ghost_id", "type_name": "bigint", "type": "bigint", "nullable": true, "default": null, "auto_increment": false, "comment": null}
],
"indexes": [
{"name": "primary", "columns": ["id"], "type": "btree", "unique": true, "primary": true}
],
"foreign_keys": [
{"name": "conversations_user_id_foreign", "columns": ["user_id"], "foreign_schema": "odynt", "foreign_table": "users", "foreign_columns": ["id"], "on_update": "no action", "on_delete": "set null"},
{"name": "conversations_ghost_id_foreign", "columns": ["ghost_id"], "foreign_schema": "odynt", "foreign_table": "ghosts", "foreign_columns": ["id"], "on_update": "no action", "on_delete": "cascade"}
]
}
]
}
"""#
static var contractModel: SchemaModel {
SchemaModel(dump: try! SchemaDump.decode(Data(contract.utf8)))
}
/// A table with `count` plain columns, the first one primary.
static func table(_ name: String, columns count: Int = 2) -> SchemaTable {
SchemaTable(name: name, columns: (0..<count).map { SchemaColumn(name: "c\($0)", type: "int", isPrimary: $0 == 0) })
}
static func edge(_ from: String, _ to: String, column: String = "c1", index: Int = 1) -> SchemaEdge {
SchemaEdge(from: from, column: column, columnIndex: index, to: to, targetColumnIndex: 0)
}
static func model(_ names: [String], edges: [SchemaEdge], columns: Int = 2) -> SchemaModel {
SchemaModel(connectionLabel: "test", tables: names.sorted().map { table($0, columns: columns) }, edges: edges)
}
}