feat: add database test safety configuration
This commit is contained in:
+5
-3
@@ -7,16 +7,18 @@
|
|||||||
"build": "next build",
|
"build": "next build",
|
||||||
"start": "next start",
|
"start": "next start",
|
||||||
"lint": "eslint",
|
"lint": "eslint",
|
||||||
"test": "vitest",
|
"test": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" vitest",
|
||||||
"test:run": "vitest run",
|
"test:run": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" vitest run",
|
||||||
"test:acceptance": "playwright test src/__tests__/e2e/",
|
"test:acceptance": "playwright test src/__tests__/e2e/",
|
||||||
"test:acceptance:headed": "playwright test src/__tests__/e2e/ --headed",
|
"test:acceptance:headed": "playwright test src/__tests__/e2e/ --headed",
|
||||||
"db:switch": "node scripts/switch-database.js",
|
"db:switch": "node scripts/switch-database.js",
|
||||||
"db:setup-postgres": "node scripts/setup-postgres.js",
|
"db:setup-postgres": "node scripts/setup-postgres.js",
|
||||||
"db:setup-dev": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/setup-postgres.js",
|
"db:setup-dev": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/setup-postgres.js",
|
||||||
"db:setup-dev:clean": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/setup-postgres.js --drop",
|
"db:setup-dev:clean": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/setup-postgres.js --drop",
|
||||||
"db:reset-dev": "node scripts/reset-dev-db.js",
|
"db:reset-dev": "DEV_DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/reset-dev-db.js",
|
||||||
"db:use-dev": "node scripts/use-dev-db.js",
|
"db:use-dev": "node scripts/use-dev-db.js",
|
||||||
|
"db:cleanup-prod": "PROD_DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp\" node scripts/cleanup-prod-db.js",
|
||||||
|
"db:check-prod": "PROD_DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp\" node scripts/check-test-records.js",
|
||||||
"db:cleanup-prod": "node scripts/cleanup-prod-db.js",
|
"db:cleanup-prod": "node scripts/cleanup-prod-db.js",
|
||||||
"db:seed": "node scripts/seed.js",
|
"db:seed": "node scripts/seed.js",
|
||||||
"docker:up": "docker-compose up -d",
|
"docker:up": "docker-compose up -d",
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ export default defineConfig({
|
|||||||
],
|
],
|
||||||
// Run your local dev server before starting the tests
|
// Run your local dev server before starting the tests
|
||||||
webServer: {
|
webServer: {
|
||||||
command: 'npm run dev',
|
command: 'DATABASE_URL="postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev" npm run dev',
|
||||||
url: 'http://localhost:3000',
|
url: 'http://localhost:3000',
|
||||||
timeout: 120000,
|
timeout: 120000,
|
||||||
reuseExistingServer: !process.env.CI,
|
reuseExistingServer: !process.env.CI,
|
||||||
|
|||||||
@@ -7,8 +7,18 @@
|
|||||||
|
|
||||||
const { execSync } = require('child_process');
|
const { execSync } = require('child_process');
|
||||||
|
|
||||||
// Database connection string
|
// Database connection string - use environment variable or default to production
|
||||||
const DB_URL = 'postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp';
|
const DB_URL = process.env.PROD_DATABASE_URL ||
|
||||||
|
process.env.DATABASE_URL ||
|
||||||
|
'postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp';
|
||||||
|
|
||||||
|
// Validate that we're not accidentally using dev database
|
||||||
|
if (DB_URL.includes('_dev') || DB_URL.includes('_dev_')) {
|
||||||
|
console.error('❌ ERROR: This script should not be used with the development database!');
|
||||||
|
console.error(' Current DATABASE_URL:', DB_URL);
|
||||||
|
console.error(' Please set PROD_DATABASE_URL for production database operations.');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// Helper to run SQL and log results
|
// Helper to run SQL and log results
|
||||||
function runSQL(sql, description) {
|
function runSQL(sql, description) {
|
||||||
|
|||||||
@@ -10,8 +10,18 @@
|
|||||||
|
|
||||||
const { execSync } = require('child_process');
|
const { execSync } = require('child_process');
|
||||||
|
|
||||||
// Database connection string
|
// Database connection string - use environment variable or default to production
|
||||||
const DB_URL = 'postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp';
|
const DB_URL = process.env.PROD_DATABASE_URL ||
|
||||||
|
process.env.DATABASE_URL ||
|
||||||
|
'postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp';
|
||||||
|
|
||||||
|
// Validate that we're not accidentally using dev database
|
||||||
|
if (DB_URL.includes('_dev') || DB_URL.includes('_dev_')) {
|
||||||
|
console.error('❌ ERROR: This script should not be used with the development database!');
|
||||||
|
console.error(' Current DATABASE_URL:', DB_URL);
|
||||||
|
console.error(' Please set PROD_DATABASE_URL for production database operations.');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
// Helper to run SQL and log results
|
// Helper to run SQL and log results
|
||||||
function runSQL(sql, description) {
|
function runSQL(sql, description) {
|
||||||
|
|||||||
+20
-6
@@ -11,21 +11,35 @@ const { execSync } = require('child_process');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
// Load environment variables
|
// Load environment variables - prioritize explicit DEV_DATABASE_URL
|
||||||
|
// Falls back to DATABASE_URL, then .env.development
|
||||||
const envPath = path.resolve(__dirname, '..', '.env.development');
|
const envPath = path.resolve(__dirname, '..', '.env.development');
|
||||||
if (fs.existsSync(envPath)) {
|
if (fs.existsSync(envPath)) {
|
||||||
require('dotenv').config({ path: envPath });
|
require('dotenv').config({ path: envPath });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if DATABASE_URL is set
|
// Use DEV_DATABASE_URL if set, otherwise use DATABASE_URL
|
||||||
if (!process.env.DATABASE_URL) {
|
const databaseUrl = process.env.DEV_DATABASE_URL || process.env.DATABASE_URL;
|
||||||
console.error('❌ DATABASE_URL is not set');
|
|
||||||
console.error('Please set it in .env.development');
|
// Check if database URL is set
|
||||||
|
if (!databaseUrl) {
|
||||||
|
console.error('❌ No database URL set');
|
||||||
|
console.error('Please set DEV_DATABASE_URL or DATABASE_URL environment variable');
|
||||||
|
console.error('Or ensure .env.development file exists with DATABASE_URL');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate that we're using the dev database
|
||||||
|
if (!databaseUrl.includes('_dev') && !databaseUrl.includes('_dev_')) {
|
||||||
|
console.error('❌ ERROR: This script should only be used with the development database!');
|
||||||
|
console.error(' Current URL:', databaseUrl);
|
||||||
|
console.error(' Expected pattern: euchre_camp_dev');
|
||||||
|
console.error(' Please set DEV_DATABASE_URL for development database operations.');
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract database name from URL
|
// Extract database name from URL
|
||||||
const dbUrl = process.env.DATABASE_URL;
|
const dbUrl = databaseUrl;
|
||||||
const dbName = dbUrl.split('/').pop();
|
const dbName = dbUrl.split('/').pop();
|
||||||
|
|
||||||
console.log('🔄 Resetting development database...\n');
|
console.log('🔄 Resetting development database...\n');
|
||||||
|
|||||||
@@ -16,11 +16,34 @@ function isDevDatabase(): boolean {
|
|||||||
return dbUrl.includes('euchre_camp_dev');
|
return dbUrl.includes('euchre_camp_dev');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warn if not using dev database
|
function isProductionDatabase(): boolean {
|
||||||
|
const dbUrl = process.env.DATABASE_URL || '';
|
||||||
|
return dbUrl.includes('euchre_camp') && !dbUrl.includes('_dev');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strict check - fail if using production database
|
||||||
|
if (isProductionDatabase()) {
|
||||||
|
console.error('');
|
||||||
|
console.error('='.repeat(80));
|
||||||
|
console.error('CRITICAL ERROR: Tests are attempting to run against PRODUCTION database!');
|
||||||
|
console.error('='.repeat(80));
|
||||||
|
console.error('');
|
||||||
|
console.error('Current DATABASE_URL:', process.env.DATABASE_URL);
|
||||||
|
console.error('');
|
||||||
|
console.error('Tests MUST run against the development database (euchre_camp_dev)');
|
||||||
|
console.error('');
|
||||||
|
console.error('To fix this:');
|
||||||
|
console.error(' 1. Run: npm run test:acceptance');
|
||||||
|
console.error(' 2. Or set: DATABASE_URL="postgresql://euchre_camp:password@dhg.lol:5432/euchre_camp_dev"');
|
||||||
|
console.error('');
|
||||||
|
console.error('Aborting test execution to prevent data corruption.');
|
||||||
|
console.error('');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
if (!isDevDatabase()) {
|
if (!isDevDatabase()) {
|
||||||
console.warn('⚠️ WARNING: Not using dev database!');
|
console.warn('⚠️ WARNING: DATABASE_URL does not contain euchre_camp_dev');
|
||||||
console.warn(' Current DATABASE_URL:', process.env.DATABASE_URL);
|
console.warn(' Current DATABASE_URL:', process.env.DATABASE_URL);
|
||||||
console.warn(' Expected to contain: euchre_camp_dev');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async function globalSetup(config: FullConfig) {
|
export default async function globalSetup(config: FullConfig) {
|
||||||
|
|||||||
@@ -1 +1,45 @@
|
|||||||
import '@testing-library/jest-dom'
|
import '@testing-library/jest-dom'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Vitest Setup File
|
||||||
|
*
|
||||||
|
* This file runs before each test file in the Vitest environment.
|
||||||
|
* It validates that tests are running against the development database
|
||||||
|
* to prevent accidental data corruption.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Check if DATABASE_URL is set and points to dev database
|
||||||
|
const databaseUrl = process.env.DATABASE_URL;
|
||||||
|
|
||||||
|
if (databaseUrl) {
|
||||||
|
const isDevDatabase = databaseUrl.includes('euchre_camp_dev');
|
||||||
|
const isProductionDatabase =
|
||||||
|
databaseUrl.includes('euchre_camp') &&
|
||||||
|
!databaseUrl.includes('_dev') &&
|
||||||
|
!databaseUrl.includes('_dev_');
|
||||||
|
|
||||||
|
if (isProductionDatabase) {
|
||||||
|
console.error('');
|
||||||
|
console.error('='.repeat(80));
|
||||||
|
console.error('CRITICAL ERROR: Tests are attempting to run against PRODUCTION database!');
|
||||||
|
console.error('='.repeat(80));
|
||||||
|
console.error('');
|
||||||
|
console.error('Current DATABASE_URL:', databaseUrl);
|
||||||
|
console.error('');
|
||||||
|
console.error('Tests MUST run against the development database (euchre_camp_dev)');
|
||||||
|
console.error('');
|
||||||
|
console.error('To fix this:');
|
||||||
|
console.error(' 1. Run: npm run test:run');
|
||||||
|
console.error(' 2. Or set: DATABASE_URL="postgresql://euchre_camp:password@dhg.lol:5432/euchre_camp_dev"');
|
||||||
|
console.error('');
|
||||||
|
console.error('Aborting test execution to prevent data corruption.');
|
||||||
|
console.error('');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isDevDatabase) {
|
||||||
|
console.log('✓ Tests running against development database (euchre_camp_dev)');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
console.warn('⚠ No DATABASE_URL set - tests may fail or use unexpected database');
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user