Skip to content

Commit

Permalink
Provide better messaging when renaming a table (#10600)
Browse files Browse the repository at this point in the history
* Provide better messaging when renaming a table

* Update based on review
  • Loading branch information
matthewp authored Apr 8, 2024
1 parent fa0f593 commit 28e7535
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-planes-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/db": patch
---

Provide better messaging when renaming a table
13 changes: 13 additions & 0 deletions packages/db/src/core/cli/commands/push/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
getMigrationQueries,
getProductionCurrentSnapshot,
} from '../../migration-queries.js';
import prompts from 'prompts';

export async function cmd({
dbConfig,
Expand Down Expand Up @@ -41,6 +42,18 @@ export async function cmd({
}

if (isForceReset) {
const { begin } = await prompts({
type: "confirm",
name: "begin",
message: `Reset your database? All of your data will be erased and your schema created from scratch.`,
initial: false
});

if(!begin) {
console.log("Canceled.");
process.exit(0);
}

console.log(`Force-pushing to the database. All existing data will be erased.`);
} else if (confirmations.length > 0) {
console.log('\n' + formatDataLossMessage(confirmations) + '\n');
Expand Down
6 changes: 3 additions & 3 deletions packages/db/src/core/cli/migration-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export async function getMigrationQueries({
Object.entries(droppedTables).filter(([, table]) => !table.deprecated)
);
if (!isEmpty(addedTables) && !isEmpty(notDeprecatedDroppedTables)) {
throw new Error(
RENAME_TABLE_ERROR(Object.keys(addedTables)[0], Object.keys(notDeprecatedDroppedTables)[0])
);
const oldTable = Object.keys(notDeprecatedDroppedTables)[0];
const newTable = Object.keys(addedTables)[0];
throw new Error(RENAME_TABLE_ERROR(oldTable, newTable));
}

for (const [tableName, table] of Object.entries(addedTables)) {
Expand Down
10 changes: 7 additions & 3 deletions packages/db/src/core/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ export const MISSING_EXECUTE_PATH_ERROR = `${red(

export const RENAME_TABLE_ERROR = (oldTable: string, newTable: string) => {
return (
red('▶ Potential table rename detected: ' + oldTable + ', ' + newTable) +
`\n You cannot add and remove tables in the same schema update batch.` +
`\n To resolve, add a 'deprecated: true' flag to '${oldTable}' instead.`
red("\u25B6 Potential table rename detected: " + oldTable + " -> " + newTable) + `
You cannot add and remove tables in the same schema update batch.
1. Use "deprecated: true" to deprecate a table before renaming.
2. Use "--force-reset" to ignore this warning and reset the database (deleting all of your data).
Visit https://docs.astro.build/en/guides/astro-db/#renaming-tables to learn more.`
);
};

Expand Down

0 comments on commit 28e7535

Please sign in to comment.