diff options
author | 2024-03-19 17:10:33 +0000 | |
---|---|---|
committer | 2024-03-19 17:10:33 +0000 | |
commit | 2ebcf94d0af5ac789c61b4190dea0ad6a402a6ea (patch) | |
tree | ec1e31d444737ea0465f1d812ad9cace638ce8cb | |
parent | 4268d389fcb4de852bf7bcbb65fabceec395a849 (diff) | |
download | astro-2ebcf94d0af5ac789c61b4190dea0ad6a402a6ea.tar.gz astro-2ebcf94d0af5ac789c61b4190dea0ad6a402a6ea.tar.zst astro-2ebcf94d0af5ac789c61b4190dea0ad6a402a6ea.zip |
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
-rw-r--r-- | .changeset/yellow-ducks-greet.md | 5 | ||||
-rw-r--r-- | packages/db/package.json | 1 | ||||
-rw-r--r-- | packages/db/src/core/cli/migration-queries.ts | 3 | ||||
-rw-r--r-- | packages/db/src/core/integration/typegen.ts | 2 | ||||
-rw-r--r-- | packages/db/test/unit/column-queries.test.js | 59 | ||||
-rw-r--r-- | pnpm-lock.yaml | 3 |
6 files changed, 70 insertions, 3 deletions
diff --git a/.changeset/yellow-ducks-greet.md b/.changeset/yellow-ducks-greet.md new file mode 100644 index 000000000..167c54305 --- /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 c9f42086a..7196d3f4c 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 13d296ce5..8a3106b1f 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 ad0cc915f..d6551da7f 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 a26425817..0d0dbb0f4 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 d1b395f8e..0c2e83a4a 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 |