Skip to content

Commit

Permalink
chore(db): Add missing github-slugger dependency & tests (#10405)
Browse files Browse the repository at this point in the history
* chore: add missing github-slugger dependency

* test: add column-queries data loss tests

* chore: remove unused vars

* test: assert length rather than message content
  • Loading branch information
43081j authored Mar 19, 2024
1 parent 4268d38 commit 2ebcf94
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-ducks-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Added github-slugger as a direct dependency
1 change: 1 addition & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions packages/db/src/core/cli/migration-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { hasPrimaryKey } from '../../runtime/index.js';
import {
getCreateIndexQueries,
getCreateTableQuery,
getDropTableIfExistsQuery,
getModifiers,
getReferencesConfig,
hasDefault,
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion packages/db/src/core/integration/typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
Expand Down
59 changes: 59 additions & 0 deletions packages/db/test/unit/column-queries.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ebcf94

Please sign in to comment.