From 2ebcf94d0af5ac789c61b4190dea0ad6a402a6ea Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Tue, 19 Mar 2024 17:10:33 +0000 Subject: [PATCH] chore(db): Add missing github-slugger dependency & tests (#10405) * chore: add missing github-slugger dependency * test: add column-queries data loss tests * chore: remove unused vars * test: assert length rather than message content --- .changeset/yellow-ducks-greet.md | 5 ++ packages/db/package.json | 1 + packages/db/src/core/cli/migration-queries.ts | 3 +- packages/db/src/core/integration/typegen.ts | 2 +- packages/db/test/unit/column-queries.test.js | 59 +++++++++++++++++++ pnpm-lock.yaml | 3 + 6 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 .changeset/yellow-ducks-greet.md diff --git a/.changeset/yellow-ducks-greet.md b/.changeset/yellow-ducks-greet.md new file mode 100644 index 000000000000..167c54305b17 --- /dev/null +++ b/.changeset/yellow-ducks-greet.md @@ -0,0 +1,5 @@ +--- +"@astrojs/db": patch +--- + +Added github-slugger as a direct dependency diff --git a/packages/db/package.json b/packages/db/package.json index c9f42086a7dd..7196d3f4c10c 100644 --- a/packages/db/package.json +++ b/packages/db/package.json @@ -67,6 +67,7 @@ "async-listen": "^3.0.1", "deep-diff": "^1.0.2", "drizzle-orm": "^0.30.2", + "github-slugger": "^2.0.0", "kleur": "^4.1.5", "nanoid": "^5.0.1", "open": "^10.0.3", diff --git a/packages/db/src/core/cli/migration-queries.ts b/packages/db/src/core/cli/migration-queries.ts index 13d296ce5896..8a3106b1f9e8 100644 --- a/packages/db/src/core/cli/migration-queries.ts +++ b/packages/db/src/core/cli/migration-queries.ts @@ -7,7 +7,6 @@ import { hasPrimaryKey } from '../../runtime/index.js'; import { getCreateIndexQueries, getCreateTableQuery, - getDropTableIfExistsQuery, getModifiers, getReferencesConfig, hasDefault, @@ -77,7 +76,7 @@ export async function getMigrationQueries({ const addedColumns = getAdded(oldCollection.columns, newCollection.columns); const droppedColumns = getDropped(oldCollection.columns, newCollection.columns); const notDeprecatedDroppedColumns = Object.fromEntries( - Object.entries(droppedColumns).filter(([key, col]) => !col.schema.deprecated) + Object.entries(droppedColumns).filter(([, col]) => !col.schema.deprecated) ); if (!isEmpty(addedColumns) && !isEmpty(notDeprecatedDroppedColumns)) { throw new Error( diff --git a/packages/db/src/core/integration/typegen.ts b/packages/db/src/core/integration/typegen.ts index ad0cc915fa85..d6551da7f142 100644 --- a/packages/db/src/core/integration/typegen.ts +++ b/packages/db/src/core/integration/typegen.ts @@ -34,7 +34,7 @@ function generateTableType(name: string, collection: DBTable): string { const sanitizedColumnsList = Object.entries(collection.columns) // Filter out deprecated columns from the typegen, so that they don't // appear as queryable fields in the generated types / your codebase. - .filter(([key, val]) => !val.schema.deprecated); + .filter(([, val]) => !val.schema.deprecated); const sanitizedColumns = Object.fromEntries(sanitizedColumnsList); let tableType = ` export const ${name}: import(${RUNTIME_IMPORT}).Table< ${JSON.stringify(name)}, diff --git a/packages/db/test/unit/column-queries.test.js b/packages/db/test/unit/column-queries.test.js index a264258175c6..0d0dbb0f43fc 100644 --- a/packages/db/test/unit/column-queries.test.js +++ b/packages/db/test/unit/column-queries.test.js @@ -105,6 +105,65 @@ describe('column queries', () => { expect(queries).to.deep.equal([]); }); + it('should return warning if column type change introduces data loss', async () => { + const blogInitial = tableSchema.parse({ + ...userInitial, + columns: { + date: column.text(), + }, + }); + const blogFinal = tableSchema.parse({ + ...userInitial, + columns: { + date: column.date(), + }, + }); + const { queries, confirmations } = await userChangeQueries(blogInitial, blogFinal); + expect(queries).to.deep.equal([ + 'DROP TABLE "Users"', + 'CREATE TABLE "Users" (_id INTEGER PRIMARY KEY, "date" text NOT NULL)', + ]); + expect(confirmations.length).to.equal(1); + }); + + it('should return warning if new required column added', async () => { + const blogInitial = tableSchema.parse({ + ...userInitial, + columns: {}, + }); + const blogFinal = tableSchema.parse({ + ...userInitial, + columns: { + date: column.date({ optional: false }), + }, + }); + const { queries, confirmations } = await userChangeQueries(blogInitial, blogFinal); + expect(queries).to.deep.equal([ + 'DROP TABLE "Users"', + 'CREATE TABLE "Users" (_id INTEGER PRIMARY KEY, "date" text NOT NULL)', + ]); + expect(confirmations.length).to.equal(1); + }); + + it('should return warning if non-number primary key with no default added', async () => { + const blogInitial = tableSchema.parse({ + ...userInitial, + columns: {}, + }); + const blogFinal = tableSchema.parse({ + ...userInitial, + columns: { + id: column.text({ primaryKey: true }), + }, + }); + const { queries, confirmations } = await userChangeQueries(blogInitial, blogFinal); + expect(queries).to.deep.equal([ + 'DROP TABLE "Users"', + 'CREATE TABLE "Users" ("id" text PRIMARY KEY)', + ]); + expect(confirmations.length).to.equal(1); + }); + it('should be empty when type updated to same underlying SQL type', async () => { const blogInitial = tableSchema.parse({ ...userInitial, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index d1b395f8ec61..0c2e83a4aeac 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3844,6 +3844,9 @@ importers: drizzle-orm: specifier: ^0.30.2 version: 0.30.2(@libsql/client@0.5.6) + github-slugger: + specifier: ^2.0.0 + version: 2.0.0 kleur: specifier: ^4.1.5 version: 4.1.5