Skip to content

Commit

Permalink
test: add column-queries data loss tests
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j committed Mar 12, 2024
1 parent f9811da commit 35761b2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
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 @@ -76,7 +75,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
71 changes: 71 additions & 0 deletions packages/db/test/unit/column-queries.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import * as color from 'kleur/colors';
import {
getCollectionChangeQueries,
getMigrationQueries,
Expand Down Expand Up @@ -106,6 +107,76 @@ 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).to.deep.equal([
`Updating existing column ${color.bold(
`${TABLE_NAME}.date`
)} to a new type that cannot be handled automatically.`,
]);
});

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).to.deep.equal([
`You added new required column '${color.bold(
`${TABLE_NAME}.date`
)}' with no default value.` + '\n This cannot be executed on an existing table.',
]);
});

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).to.deep.equal([
`You added new required column '${color.bold(`${TABLE_NAME}.id`)}' with no default value.` +
'\n This cannot be executed on an existing table.',
]);
});

it('should be empty when type updated to same underlying SQL type', async () => {
const blogInitial = tableSchema.parse({
...userInitial,
Expand Down

0 comments on commit 35761b2

Please sign in to comment.