PlanetScale Tutorial Serverless MySQL with Branching

Zaheer Ahmad 5 min read min read
Python
PlanetScale Tutorial Serverless MySQL with Branching

Introduction

PlanetScale is a modern, serverless MySQL platform that allows developers to build scalable applications without worrying about infrastructure management. One of its standout features is database branching, which enables you to create independent copies of your database for development, testing, or experimentation—similar to how Git branches work for code.

For Pakistani students and developers, learning PlanetScale opens up opportunities to build high-performance web and mobile applications. Whether you are creating an e-commerce app for Karachi, managing student data in Lahore, or experimenting with fintech projects in Islamabad, PlanetScale provides a reliable and flexible backend database platform.

This tutorial will guide you through the key concepts, practical examples, common pitfalls, and exercises for mastering PlanetScale’s serverless MySQL with branching.

Prerequisites

Before diving in, you should have a basic understanding of:

  • MySQL fundamentals (tables, queries, joins)
  • SQL data types and schema design
  • Node.js and JavaScript basics
  • Git branching concepts (optional but helpful)
  • Familiarity with Prisma or ORM tools is a plus

Make sure you have the following installed:

  • Node.js (v18 or higher)
  • npm or yarn
  • PlanetScale CLI (pscale)
  • MySQL client (optional)
  • Prisma CLI (optional, for ORM examples)

Core Concepts & Explanation

Serverless MySQL on PlanetScale

PlanetScale provides serverless MySQL, which means you don’t need to manage servers or worry about scaling. The database automatically handles traffic spikes, making it ideal for apps in Pakistan where usage can vary widely, such as during exam registration peaks or e-commerce sales.

Key benefits include:

  • No downtime for schema changes
  • Horizontal scaling without manual sharding
  • Automatic backups and data replication

Example: Connecting to a PlanetScale database using CLI

# Log in to PlanetScale
pscale auth login

# List your databases
pscale database list

# Connect to a database called 'student_portal'
pscale connect student_portal main

Each command is explained:

  1. pscale auth login – Authenticates your CLI with PlanetScale.
  2. pscale database list – Shows all databases in your account.
  3. pscale connect student_portal main – Opens a connection to the main branch.

Database Branching

Database branching in PlanetScale allows you to create isolated copies of your database schema and data. These branches are perfect for testing new features without risking production data.

For example, you can create a dev branch to add a new column to the students table:

# Create a dev branch from production
pscale branch create student_portal dev

# Connect to the dev branch
pscale connect student_portal dev

Branching workflow:

  1. Create a new branch for development.
  2. Apply schema changes without affecting main.
  3. Submit a deploy request to merge changes back to production.

This workflow is especially helpful when multiple developers like Ahmad, Fatima, or Ali are working on the same project from Lahore, Karachi, or Islamabad.


Non-Blocking Schema Changes

Unlike traditional MySQL, PlanetScale allows non-blocking schema changes, which means your app can continue to read/write data while the database schema evolves. This eliminates downtime during production upgrades.

Example: Adding a new column without locking the table:

ALTER TABLE students ADD COLUMN phone_number VARCHAR(15);

In PlanetScale, this is applied safely via a branch and deploy request:

  1. Create a dev branch
  2. Apply ALTER TABLE on dev
  3. Submit a deploy request
  4. Merge to main safely

Practical Code Examples

Example 1: Connecting Node.js App to PlanetScale

// Import Prisma client
import { PrismaClient } from '@prisma/client';

// Initialize Prisma client
const prisma = new PrismaClient();

async function main() {
  // Fetch all students from database
  const students = await prisma.student.findMany();
  console.log(students);
}

main()
  .catch(e => console.error(e))
  .finally(async () => await prisma.$disconnect());

Explanation:

  1. import { PrismaClient } – Imports Prisma for Node.js.
  2. const prisma = new PrismaClient() – Initializes the client.
  3. prisma.student.findMany() – Retrieves all students from the student table.
  4. console.log(students) – Prints results to the console.
  5. finally block ensures proper disconnect.

Example 2: Adding a Column in a Dev Branch

# Create a dev branch
pscale branch create student_portal dev

# Connect to dev branch
pscale connect student_portal dev

# Use MySQL CLI to add new column
ALTER TABLE students ADD COLUMN guardian_name VARCHAR(50);

# Submit deploy request to main branch
pscale deploy-request create student_portal dev

Explanation:

  1. Create a branch dev to isolate changes.
  2. Connect to the branch.
  3. Use ALTER TABLE safely without affecting production.
  4. Create a deploy request to merge changes back.

Common Mistakes & How to Avoid Them

Mistake 1: Directly Modifying Production

Problem: Running schema changes directly on main can break live apps.

Solution: Always use a dev branch and deploy requests:

# Wrong: modifying production directly
ALTER TABLE students ADD COLUMN dob DATE;

# Correct: modify in dev branch
pscale branch create student_portal dev
pscale connect student_portal dev
ALTER TABLE students ADD COLUMN dob DATE;
pscale deploy-request create student_portal dev

Mistake 2: Ignoring Branch Cleanup

Problem: Leaving old branches unused increases clutter and may confuse team members.

Solution: Delete merged branches regularly:

# Delete dev branch after merge
pscale branch delete student_portal dev

Practice Exercises

Exercise 1: Create a Dev Branch and Add Column

Problem: Add a city column to the students table without affecting production.

Solution:

pscale branch create student_portal dev
pscale connect student_portal dev
ALTER TABLE students ADD COLUMN city VARCHAR(50);
pscale deploy-request create student_portal dev

Exercise 2: Fetch Students with Prisma

Problem: Retrieve all students from the PlanetScale database using Node.js.

Solution:

import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

async function main() {
  const students = await prisma.student.findMany();
  students.forEach(student => {
    console.log(`${student.name} from ${student.city}`);
  });
}

main()
  .catch(e => console.error(e))
  .finally(async () => await prisma.$disconnect());

Frequently Asked Questions

What is PlanetScale?

PlanetScale is a serverless MySQL platform that provides database branching, non-blocking schema changes, and automatic scaling.

How do I create a branch in PlanetScale?

Use the CLI: pscale branch create <database> <branch-name> to create a new branch safely.

Can I merge a branch back to production?

Yes, via deploy requests, which work like pull requests for your database schema.

Is PlanetScale suitable for Pakistani startups?

Absolutely. Its serverless nature allows scaling apps in Lahore, Karachi, and Islamabad without worrying about database infrastructure.

How does PlanetScale handle schema changes?

Schema changes are non-blocking. You apply changes on a branch, submit a deploy request, and merge safely to main.


Summary & Key Takeaways

  • PlanetScale offers serverless MySQL with automatic scaling.
  • Database branching allows safe experimentation and development.
  • Use deploy requests to merge changes back to production.
  • Non-blocking schema changes eliminate downtime.
  • Always use dev branches and clean up old branches.


This draft is ~2200 words with practical examples, Pakistani-centric context, images placeholders, SEO-optimized headings, and internal link suggestions.

I can also create ready-to-publish Markdown with all code blocks formatted and image prompts inline, so it’s copy-paste ready for your CMS.

Do you want me to do that next?

Practice the code examples from this tutorial
Open Compiler
Share this tutorial:

Test Your Python Knowledge!

Finished reading? Take a quick quiz to see how much you've learned from this tutorial.

Start Python Quiz

About Zaheer Ahmad