adds schema view and schema generating command
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import Foundation
|
||||
import HelderCore
|
||||
import Testing
|
||||
|
||||
@Suite struct SchemaDumpTests {
|
||||
private func decode(_ json: String) throws -> SchemaDump {
|
||||
try SchemaDump.decode(Data(json.utf8))
|
||||
}
|
||||
|
||||
// MARK: decoding
|
||||
|
||||
@Test func theFullContractDecodes() throws {
|
||||
let dump = try decode(SchemaFixtures.contract)
|
||||
#expect(dump.version == 1)
|
||||
#expect(dump.connection == "mysql")
|
||||
#expect(dump.driver == "mysql")
|
||||
#expect(dump.database == "odynt")
|
||||
#expect(dump.generatedAt == "2026-11-02T14:32:05+01:00")
|
||||
#expect(dump.tables.map(\.name) == ["users", "messages", "conversations"])
|
||||
|
||||
let users = try #require(dump.tables.first)
|
||||
#expect(users.rows == 1284)
|
||||
#expect(users.columns[0] == SchemaDump.Column(name: "id", typeName: "bigint", type: "bigint unsigned", autoIncrement: true))
|
||||
#expect(users.indexes[1] == SchemaDump.Index(name: "users_email_unique", columns: ["email"], type: "btree", unique: true))
|
||||
|
||||
let messages = dump.tables[1]
|
||||
#expect(messages.columns[2].comment == "the body")
|
||||
#expect(messages.foreignKeys == [SchemaDump.ForeignKey(
|
||||
name: "messages_conversation_id_foreign", columns: ["conversation_id"], foreignSchema: "odynt",
|
||||
foreignTable: "conversations", foreignColumns: ["id"], onUpdate: "no action", onDelete: "cascade")])
|
||||
|
||||
#expect(dump.tables[2].rows == nil)
|
||||
#expect(dump.tables[2].columns[2].defaultValue == "0")
|
||||
}
|
||||
|
||||
@Test func theOlderPrototypeShapeDecodes() throws {
|
||||
let dump = try decode("""
|
||||
{"connection": "mysql", "generated_at": "2026-10-01T09:00:00+00:00",
|
||||
"tables": [{"name": "users", "columns": [{"name": "id", "type_name": "bigint", "type": "bigint", "nullable": false}],
|
||||
"indexes": [{"name": "primary", "columns": ["id"], "unique": true, "primary": true}], "foreign_keys": []}]}
|
||||
""")
|
||||
#expect(dump.version == nil)
|
||||
#expect(dump.driver == nil)
|
||||
#expect(dump.database == nil)
|
||||
#expect(dump.tables.count == 1)
|
||||
#expect(dump.tables[0].rows == nil)
|
||||
#expect(SchemaModel(dump: dump).connectionLabel == "mysql")
|
||||
}
|
||||
|
||||
@Test func missingKeysGetTheirDefaults() throws {
|
||||
let dump = try decode(#"{"tables": [{"name": "t", "columns": [{"name": "a"}], "indexes": [{"columns": ["a"], "primary": true}]}]}"#)
|
||||
let table = try #require(dump.tables.first)
|
||||
#expect(table.foreignKeys == [])
|
||||
#expect(table.columns == [SchemaDump.Column(name: "a")])
|
||||
#expect(table.indexes[0].unique)
|
||||
#expect(try decode("{}") == SchemaDump(version: nil))
|
||||
}
|
||||
|
||||
@Test func unknownKeysAndBrokenItemsAreSkipped() throws {
|
||||
let dump = try decode("""
|
||||
{"version": "1", "extra": {"x": 1}, "tables": [
|
||||
{"columns": []},
|
||||
"not a table",
|
||||
{"name": "t", "rows": "12", "columns": [{"type": "int"}, {"name": "a", "nullable": 1, "default": 5, "auto_increment": "1"}],
|
||||
"foreign_keys": [{"columns": ["a"]}, {"columns": ["a"], "foreign_table": "u"}]}
|
||||
]}
|
||||
""")
|
||||
#expect(dump.version == 1)
|
||||
#expect(dump.tables.map(\.name) == ["t"])
|
||||
let table = dump.tables[0]
|
||||
#expect(table.rows == 12)
|
||||
#expect(table.columns == [SchemaDump.Column(name: "a", nullable: true, defaultValue: "5", autoIncrement: true)])
|
||||
#expect(table.foreignKeys == [SchemaDump.ForeignKey(columns: ["a"], foreignTable: "u")])
|
||||
}
|
||||
|
||||
@Test("corrupt data throws a readable error", arguments: ["", "{\"tables\": [", "[1, 2]", "not json"])
|
||||
func corrupt(json: String) {
|
||||
#expect {
|
||||
try decode(json)
|
||||
} throws: { error in
|
||||
guard let error = error as? SchemaDumpError, case .corrupt = error else { return false }
|
||||
return error.localizedDescription.hasPrefix("database/helder/schema.json is not a valid schema dump:")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user