Skip to content

Commit 0a1c300

Browse files
committed
Update DDL logic and bump hub dependency to v0.4.0
Improved schema outdated check in DDL by returning specific outdated class names and added documentation for schema generation. Updated the `hub` dependency to version 0.4.0 and adjusted tests to reflect changes. Also included minor refinements to comments and types.
1 parent 490694e commit 0a1c300

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"@std/fmt": "jsr:@std/fmt@^1",
1818
"@std/fs": "jsr:@std/fs@^1",
1919
"@std/http": "jsr:@std/http@^1",
20-
"hub": "jsr:@acr/hub@^0"
20+
"hub": "jsr:@acr/hub@^0.4.0"
2121
},
2222
"publish": {
2323
"include": [

deno.lock

Lines changed: 12 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ddl.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,23 @@ export class DDL {
6464
enhance = false,
6565
schemasFile?: string,
6666
): Promise<Record<string, Schema>> {
67-
const outdated = !schemas ? true : await DDL.outdatedSchemas(schemas, base);
68-
if (!outdated) return schemas;
67+
const outdated = !schemas ? undefined : await DDL.outdatedSchemas(schemas, base);
68+
if (!outdated || outdated.length > 0) return schemas;
6969

7070
// Generate and save
7171
schemas = await DDL.generateSchemas(classFiles, base, enhance);
7272
if (schemasFile) await Deno.writeTextFile(schemasFile, JSON.stringify(schemas, null, 2));
7373
return schemas;
7474
}
7575

76+
/**
77+
* Generate schemas from class files
78+
*
79+
* @param classFiles - a map of class names to file paths
80+
* @param base - the base directory where the files are located, needed for relative URLs in schema
81+
* @param enhance - if true schemas will be enhanced with standard properties
82+
* @returns a map of class names to schemas
83+
*/
7684
static async generateSchemas(classFiles: Record<string, string>, base?: string, enhance?: boolean): Promise<Record<string, Schema>> {
7785
// If DDL has no generator, throw an error
7886
if (!DDL.generator) throw new Error("DDL.generator must be set to a function that generates schemas from class files");
@@ -117,12 +125,13 @@ export class DDL {
117125
return schema;
118126
}
119127

120-
static async outdatedSchemas(schemas: Record<string, Schema> | Schema[], base = ""): Promise<boolean> {
121-
for (const schema of Array.isArray(schemas) ? schemas : Object.values(schemas)) {
122-
const outdated = await DDL.outdatedSchema(schema, base);
123-
if (outdated) return true;
128+
static async outdatedSchemas(schemas: Record<string, Schema>, base = ""): Promise<string[]> {
129+
const outdated: string[] = [];
130+
for (const [c, s] of Object.entries(schemas)) {
131+
if (!(await DDL.outdatedSchema(s, base))) continue;
132+
outdated.push(c);
124133
}
125-
return false;
134+
return outdated;
126135
}
127136

128137
/**
@@ -162,7 +171,7 @@ export class DDL {
162171
static #defaultValue(column: Column, dbType: string) {
163172
const cd = column.default;
164173

165-
// Auto inserted/updated values
174+
// Automatically inserted/updated values
166175
if (column.dateOn === "insert") return "CURRENT_TIMESTAMP";
167176
if (column.dateOn === "update") return "CURRENT_TIMESTAMP" + ((dbType !== DB.Provider.MYSQL) ? "" : " ON UPDATE CURRENT_TIMESTAMP");
168177

test/ddl.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import { assertEquals, assertExists, assertNotEquals } from "@std/assert";
44
import { delay } from "@std/async";
5+
import { hub } from "hub";
56
import { DDL } from "../src/ddl.ts";
67
import type { Schema } from "../src/types.ts";
78
import { createTables, dbInit, getProvider } from "./helpers.ts";
89

10+
// deno-lint-ignore no-global-assign
11+
console = hub("*", "debug", { fileLine: true });
12+
913
// See https://github.com/denoland/deno_std/blob/main/testing/_diff_test.ts
1014

1115
const CI = Deno.env.has("CI");
@@ -24,6 +28,7 @@ DDL.generator = async function (classFiles: Record<string, string>, base?: strin
2428
import staticSchema from "../resources/account.json" with { type: "json" };
2529

2630
// Generate dynamic schema to make sure it's the same result
31+
// const classFiles = { "Account": "resources/account.ts", "Point": "resources/point.ts" };
2732
const classFiles = { "Account": "resources/account.ts" };
2833

2934
// Track bug https://github.com/denoland/deno/issues/28206
@@ -166,8 +171,6 @@ Deno.test("Actual Table", async function () {
166171

167172
// Execute the table creation on the provided platform
168173
Deno.test("Schema Generation", async function () {
169-
if (CI) return; // TODO: remove once https://github.com/denoland/deno/issues/28206 is fixed
170-
171174
// Wait until the top of the second so that it runs within the same second
172175
await delay(1000 - (new Date()).getMilliseconds());
173176

0 commit comments

Comments
 (0)