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
This commit is contained in:
James Garbutt 2024-03-19 17:10:33 +00:00 committed by GitHub
parent 4268d389fc
commit 2ebcf94d0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 70 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---
Added github-slugger as a direct dependency

View file

@ -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",

View file

@ -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(

View file

@ -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)},

View file

@ -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,

3
pnpm-lock.yaml generated
View file

@ -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