Initial commit
This commit is contained in:
77
skills/neon-drizzle/scripts/generate-schema.ts
Normal file
77
skills/neon-drizzle/scripts/generate-schema.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* 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);
|
||||
});
|
||||
133
skills/neon-drizzle/scripts/run-migration.ts
Normal file
133
skills/neon-drizzle/scripts/run-migration.ts
Normal file
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Run Migration Script
|
||||
*
|
||||
* Applies pending Drizzle migrations to your Neon database.
|
||||
* Run with: npx ts-node run-migration.ts
|
||||
*
|
||||
* This script will:
|
||||
* 1. Connect to your Neon database
|
||||
* 2. Apply all pending migrations
|
||||
* 3. Report success or failure
|
||||
*/
|
||||
|
||||
import { drizzle } from 'drizzle-orm/neon-http';
|
||||
import { migrate } from 'drizzle-orm/neon-http/migrator';
|
||||
import { neon } from '@neondatabase/serverless';
|
||||
|
||||
const DATABASE_URL = process.env.DATABASE_URL;
|
||||
|
||||
if (!DATABASE_URL) {
|
||||
console.error('❌ DATABASE_URL environment variable is not set');
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function runMigrations() {
|
||||
console.log('🔄 Running Drizzle migrations...\n');
|
||||
|
||||
try {
|
||||
// Create SQL client
|
||||
const sql = neon(DATABASE_URL);
|
||||
|
||||
// Create Drizzle instance
|
||||
const db = drizzle(sql);
|
||||
|
||||
// Run migrations
|
||||
console.log('⏳ Applying migrations...');
|
||||
await migrate(db, {
|
||||
migrationsFolder: './src/db/migrations',
|
||||
});
|
||||
|
||||
console.log('✅ All migrations applied successfully!\n');
|
||||
|
||||
// Show migration status
|
||||
console.log('📊 Migration Summary:');
|
||||
console.log(' Database: ' + new URL(DATABASE_URL).pathname.slice(1));
|
||||
console.log(' Migrations folder: ./src/db/migrations');
|
||||
console.log(' Status: Up to date\n');
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('❌ Migration failed');
|
||||
console.error((error as any).message);
|
||||
|
||||
console.log('\n💡 Troubleshooting:');
|
||||
console.log(' • Ensure ./src/db/migrations directory exists');
|
||||
console.log(' • Verify DATABASE_URL is correct');
|
||||
console.log(' • Check that migrations are properly formatted SQL files');
|
||||
console.log(' • Try running: npx drizzle-kit generate first');
|
||||
console.log(' • Review guides/troubleshooting.md for common migration errors');
|
||||
console.log(' • See references/migrations.md for detailed migration guide');
|
||||
|
||||
const errorMessage = (error as any).message.toLowerCase();
|
||||
|
||||
if (errorMessage.includes('connect') || errorMessage.includes('connection')) {
|
||||
console.log('\n⚠️ Connection issue detected:');
|
||||
console.log(' • Verify DATABASE_URL format: postgresql://user:pass@host/db?sslmode=require');
|
||||
console.log(' • Ensure database is accessible');
|
||||
console.log(' • Check firewall/network settings');
|
||||
console.log(' • See guides/troubleshooting.md section: "Connection Errors"');
|
||||
}
|
||||
|
||||
if (errorMessage.includes('already exists') || errorMessage.includes('duplicate')) {
|
||||
console.log('\n⚠️ Migration conflict detected:');
|
||||
console.log(' • Migration may have been partially applied');
|
||||
console.log(' • Check database state: psql $DATABASE_URL -c "\\dt"');
|
||||
console.log(' • See references/migrations.md for handling conflicts');
|
||||
}
|
||||
|
||||
if (errorMessage.includes('not found') || errorMessage.includes('enoent')) {
|
||||
console.log('\n⚠️ Migrations folder missing:');
|
||||
console.log(' • Run: npx drizzle-kit generate');
|
||||
console.log(' • Ensure migrations folder path matches drizzle.config.ts');
|
||||
}
|
||||
|
||||
if (errorMessage.includes('syntax')) {
|
||||
console.log('\n⚠️ SQL syntax error:');
|
||||
console.log(' • Review generated migration files in ./src/db/migrations');
|
||||
console.log(' • Check for manually edited migrations');
|
||||
console.log(' • See references/migrations.md for safe editing practices');
|
||||
}
|
||||
|
||||
console.log('');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternative: Run migrations with WebSocket (for Node.js)
|
||||
* Uncomment below if using WebSocket connections
|
||||
*/
|
||||
|
||||
/*
|
||||
import { drizzle } from 'drizzle-orm/neon-serverless';
|
||||
import { migrate } from 'drizzle-orm/neon-serverless/migrator';
|
||||
import { Pool } from '@neondatabase/serverless';
|
||||
|
||||
async function runMigrationsWebSocket() {
|
||||
console.log('🔄 Running Drizzle migrations (WebSocket)...\n');
|
||||
|
||||
const pool = new Pool({ connectionString: DATABASE_URL });
|
||||
|
||||
try {
|
||||
const db = drizzle(pool);
|
||||
|
||||
console.log('⏳ Applying migrations...');
|
||||
await migrate(db, {
|
||||
migrationsFolder: './src/db/migrations',
|
||||
});
|
||||
|
||||
console.log('✅ All migrations applied successfully!\n');
|
||||
await pool.end();
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('❌ Migration failed:', (error as any).message);
|
||||
await pool.end();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Run migrations
|
||||
runMigrations().then((success) => {
|
||||
process.exit(success ? 0 : 1);
|
||||
});
|
||||
Reference in New Issue
Block a user