What You’ll Learn
- How to set up a PostgreSQL environment with Node.js in Vers
- The power of branching database states at critical decision points
- Parallel testing of different database schemas and features
- Real-world time savings compared to traditional database testing
Prerequisites
- Vers CLI installed and authenticated
- Basic familiarity with PostgreSQL and Node.js
- Understanding of database schema design concepts
Project Overview
We’ll test different database features and schema changes on an e-commerce system. Instead of resetting the database and rebuilding test data for each scenario, we’ll:- Set up once: Create a VM with PostgreSQL, Node.js, and base schema
- Branch at decision points: Capture database state before major changes
- Test in parallel: Run different feature implementations simultaneously
- Compare results: Analyze different approaches without data loss
Step 1: Project Setup
Initialize the Project
Configure the Environment
Edit the generatedvers.toml to allocate sufficient resources for PostgreSQL:
Create the Dockerfile
Create aDockerfile in your project root. This file defines a custom Ubuntu-based environment with PostgreSQL 15, Node.js 18, and all the infrastructure needed for Vers to function properly. The Dockerfile includes SSH server setup, PostgreSQL configuration, and an automated startup script:
Step 2: Launch and Set Up the Environment
Start the Cluster
vers up command will:
- Build your custom rootfs with PostgreSQL and Node.js
- Start a cluster with your new environment
- Create the root VM with database services
Connect and Initialize Database
Set Up Node.js Dependencies
Create apackage.json file in your project root to define your Node.js project dependencies and metadata.
For your convenience, we’ve pre-installed emacs and vim for file editing.
This file tells npm what packages to install for database connectivity:
Step 3: Create the Base Schema and Test Infrastructure
Build the Database Schema
Create a file calledschema.sql containing a comprehensive e-commerce database schema with sample data. This SQL file will set up all the tables and relationships needed for our testing scenarios:
Create the Base Test Class
Create a file calleddb-test.js that contains a reusable database testing class. This class handles PostgreSQL connections, displays database state, and provides common functionality that all your tests will inherit:
Step 4: Save Your State
Similar to git, Vers allows you to create snapshots of your VM instances for reference. When you commit, Vers captures:- Complete filesystem state
- Memory state
- Running processes
- Network configuration
Step 5: Branch for Different Feature Implementations
This is where Vers shines! Instead of rebuilding database state for each feature test, we’ll branch from this established baseline. Our new branches will inherit the same state data we committed:- Complete filesystem state
- Memory state
- Running processes
- Network configuration
Create Premium Features Branch
vers branch command.
Implement Premium Features
Create a file calledpremium-test.js that extends the base test class to add premium user functionality. This test demonstrates how to add new database tables and modify existing data to implement premium features:
Create Inventory Management Branch
Implement Inventory Management
Create a file calledinventory-test.js that extends the base test class to add comprehensive inventory tracking. This test shows how to implement inventory movements, stock alerts, and automated stock level monitoring:
Step 6: Visualize and Execute Parallel Tests
View Your VM Tree
- Root VM: Paused parent with the base database state
- Premium Features VM: Running child with premium user features
- Inventory Management VM: Running child with inventory tracking
Execute Tests Non-Interactively
Parallel Testing Workflow
Open multiple terminals to run tests simultaneously: Terminal 1:Commit Test Results
Key Benefits Demonstrated
Database State Preservation
- Complex schemas: Fully populated database states are captured and reusable
- Data integrity: No risk of losing reference data during experimentation
- Transaction history: Each branch maintains its own transaction log
Parallel Development
- Schema experiments: Test different database designs simultaneously
- Feature testing: Implement competing approaches without conflicts
- Performance comparison: Benchmark different indexing or query strategies
Time Savings
- Traditional approach: Reset database → rebuild schema → reload data for each test
- Vers approach: Branch once → test multiple scenarios in parallel
- Compound benefits: Time savings increase exponentially with schema complexity
Risk Reduction
- Safe experimentation: Test destructive operations without data loss
- Easy rollback: Return to any previous database state instantly
- Parallel validation: Compare results across different implementations
Real-World Applications
This database branching pattern excels for:Schema Evolution Testing
- Test different migration strategies from the same starting point
- Compare performance impacts of different indexing approaches
- Validate data integrity across different schema versions
Feature Development
- Implement competing feature designs in parallel
- Test different user permission models
- Compare storage requirements for alternative approaches
Performance Optimization
- Test different query optimization strategies
- Compare indexing approaches with identical data sets
- Benchmark competing database configurations
Data Migration Validation
- Test migration scripts against identical production-like data
- Validate data transformation logic across multiple scenarios
- Compare migration performance with different approaches
Common Issues and Solutions
PostgreSQL Connection Issues
If you encounter connection problems:Permission Problems
For database permission issues:Network Configuration
If Node.js can’t connect to PostgreSQL:Summary
Vers transforms database development by enabling:- Instant state capture: Preserve complex database states at any point
- Parallel experimentation: Test multiple approaches simultaneously
- Risk-free changes: Experiment without fear of data loss
- Efficient workflows: Eliminate repetitive setup and data loading
Next Steps
- Explore more complex branching scenarios with multiple decision points
- Test database migration strategies using the same base data
- Experiment with different PostgreSQL configurations in parallel branches
- Integrate database testing workflows into your CI/CD pipeline
- Try the approach with other databases like MySQL or MongoDB