Files
gh-neondatabase-labs-ai-rul…/skills/neon-drizzle/scripts/generate-schema.ts
2025-11-30 08:43:11 +08:00

78 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Generate Schema Script
*
* Generates Drizzle migration files based on schema changes.
* Run with: npx drizzle-kit generate
*
* This creates SQL migration files in the migrations directory
* based on differences between your schema.ts and the database.
*/
import { exec } from 'child_process';
import { promisify } from 'util';
const execAsync = promisify(exec);
async function generateSchema() {
console.log('🔄 Generating Drizzle migrations...\n');
try {
const { stdout, stderr } = await execAsync('npx drizzle-kit generate');
if (stdout) {
console.log('📝 Generated migrations:');
console.log(stdout);
}
if (stderr) {
console.warn('⚠️ Warnings:');
console.warn(stderr);
}
console.log('\n✅ Migration generation complete!');
console.log('\n📋 Next steps:');
console.log(' 1. Review the generated migration files in ./src/db/migrations');
console.log(' 2. Run: npx drizzle-kit migrate');
console.log(' 3. Test your application\n');
return true;
} catch (error) {
console.error('❌ Migration generation failed');
console.error((error as any).message);
console.log('\n💡 Troubleshooting:');
console.log(' • Ensure drizzle.config.ts is in your project root');
console.log(' • Check that DATABASE_URL is set correctly');
console.log(' • Verify your schema.ts file exists at the configured path');
console.log(' • Review guides/troubleshooting.md for common issues');
console.log(' • See references/migrations.md for migration patterns');
const errorMessage = (error as any).message.toLowerCase();
if (errorMessage.includes('url') || errorMessage.includes('undefined')) {
console.log('\n⚠ Environment variable issue detected:');
console.log(' • Ensure DATABASE_URL is loaded in drizzle.config.ts');
console.log(' • Add: import { config } from "dotenv"; config({ path: ".env.local" });');
console.log(' • See guides/troubleshooting.md section: "Error: url: undefined"');
}
if (errorMessage.includes('schema') || errorMessage.includes('not found')) {
console.log('\n⚠ Schema file issue detected:');
console.log(' • Verify schema path in drizzle.config.ts matches actual file location');
console.log(' • Default: ./src/db/schema.ts');
}
if (errorMessage.includes('enoent')) {
console.log('\n⚠ File/directory missing:');
console.log(' • Create migrations folder: mkdir -p src/db/migrations');
console.log(' • Ensure schema file exists: src/db/schema.ts');
}
return false;
}
}
generateSchema().then((success) => {
process.exit(success ? 0 : 1);
});