0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-17 22:44:24 -05:00

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", "async-listen": "^3.0.1",
"deep-diff": "^1.0.2", "deep-diff": "^1.0.2",
"drizzle-orm": "^0.30.2", "drizzle-orm": "^0.30.2",
"github-slugger": "^2.0.0",
"kleur": "^4.1.5", "kleur": "^4.1.5",
"nanoid": "^5.0.1", "nanoid": "^5.0.1",
"open": "^10.0.3", "open": "^10.0.3",

View file

@ -7,7 +7,6 @@ import { hasPrimaryKey } from '../../runtime/index.js';
import { import {
getCreateIndexQueries, getCreateIndexQueries,
getCreateTableQuery, getCreateTableQuery,
getDropTableIfExistsQuery,
getModifiers, getModifiers,
getReferencesConfig, getReferencesConfig,
hasDefault, hasDefault,
@ -77,7 +76,7 @@ export async function getMigrationQueries({
const addedColumns = getAdded(oldCollection.columns, newCollection.columns); const addedColumns = getAdded(oldCollection.columns, newCollection.columns);
const droppedColumns = getDropped(oldCollection.columns, newCollection.columns); const droppedColumns = getDropped(oldCollection.columns, newCollection.columns);
const notDeprecatedDroppedColumns = Object.fromEntries( 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)) { if (!isEmpty(addedColumns) && !isEmpty(notDeprecatedDroppedColumns)) {
throw new Error( throw new Error(

View file

@ -34,7 +34,7 @@ function generateTableType(name: string, collection: DBTable): string {
const sanitizedColumnsList = Object.entries(collection.columns) const sanitizedColumnsList = Object.entries(collection.columns)
// Filter out deprecated columns from the typegen, so that they don't // Filter out deprecated columns from the typegen, so that they don't
// appear as queryable fields in the generated types / your codebase. // 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); const sanitizedColumns = Object.fromEntries(sanitizedColumnsList);
let tableType = ` export const ${name}: import(${RUNTIME_IMPORT}).Table< let tableType = ` export const ${name}: import(${RUNTIME_IMPORT}).Table<
${JSON.stringify(name)}, ${JSON.stringify(name)},

View file

@ -105,6 +105,65 @@ describe('column queries', () => {
expect(queries).to.deep.equal([]); 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 () => { it('should be empty when type updated to same underlying SQL type', async () => {
const blogInitial = tableSchema.parse({ const blogInitial = tableSchema.parse({
...userInitial, ...userInitial,

3
pnpm-lock.yaml generated
View file

@ -3844,6 +3844,9 @@ importers:
drizzle-orm: drizzle-orm:
specifier: ^0.30.2 specifier: ^0.30.2
version: 0.30.2(@libsql/client@0.5.6) version: 0.30.2(@libsql/client@0.5.6)
github-slugger:
specifier: ^2.0.0
version: 2.0.0
kleur: kleur:
specifier: ^4.1.5 specifier: ^4.1.5
version: 4.1.5 version: 4.1.5