PHP
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Object-Oriented PHP
-
Section 5: Working with Data
-
Section 6: Modern PHP (PHP 8)
-
Section 7: Working with Files and Networking
-
Section 8: Common PHP Frameworks Overview
-
Section 9: Tooling and Ecosystem
-
Section 10: Practical Projects
-
Section 11: Interview Practice
-
Section 12: More Standard Library
-
Section 13: Data Structures and Algorithms in PHP
-
Section 14: More Practice Exercises
-
Section 15: More Security and Best Practices
-
Section 16: More Testing and Tooling
-
Section 17: WordPress-Style CMS Concepts
-
Section 18: Advanced OOP Practice
-
Section 19: More Web Fundamentals
-
Section 20: Database Practice
-
Section 21: PHP Manual: Array Functions
-
Section 22: PHP Manual: Date and Calendar Functions
-
Section 23: PHP Manual: Filesystem and Directory Functions
-
Section 24: PHP Manual: Filter and Var Handling
-
Section 25: PHP Manual: Math Functions
-
Section 26: PHP Manual: JSON and XML
-
Section 27: PHP Manual: Network and Stream Functions
-
Section 28: PHP Manual: Error and Exception Handling
-
Section 29: PHP Manual: Output Control and Misc
-
Section 30: PHP Manual: FTP, Zip, and Mail
-
Section 31: Modern PHP Frameworks Deep Dive
-
Section 32: PHP Design Patterns
-
Section 33: More Practice Exercises
-
Section 34: PHP Performance and Deployment
-
Section 35: More Interview Practice
-
Section 36: More PHP Standard Library
-
Section 37: PHP Concurrency and Async
-
Section 38: More Web Development Practice
-
Section 39: PHP Testing Deep Dive
-
Section 40: Composer and Package Development
-
Section 41: PHP Security Deep Dive
-
Section 42: More Practical Projects
-
Section 43: Legacy PHP Maintenance
-
Section 44: More Algorithm Practice
-
Section 45: Final Practice and Review
-
Section 46: PHP for E-Commerce Patterns
-
Section 47: PHP API Design Deep Dive
-
Section 48: PHP Caching Strategies
-
Section 49: PHP Queue and Background Jobs
-
Section 50: PHP Multi-Tenancy Patterns
-
Section 51: PHP Real-Time Features
-
Section 52: PHP CMS and Content Modeling
-
Section 53: PHP Internationalization
-
Section 54: More Framework-Specific Practice
-
Section 55: PHP Legacy Code Refactoring
-
Section 56: More Practice Projects Round 2
-
Section 57: PHP Command-Line Applications
-
Section 58: PHP and Microservices
-
Section 59: More Interview and Review Round 2
88: Database Migrations Without a Framework
You might have noticed that every modern PHP framework comes with a migration system. Because of this, a lot of developers fall into the trap of thinking that "migrations" are a specific feature of a framework like Laravel or Symfony. They think it's some magic black box that manages their database.
Migrations aren't a framework feature; they're just version control for your schema
Here is why that misconception is dangerous: it makes you feel like you can't manage a professional database unless you're using a heavy framework. Let's look at a real scenario. Imagine you're working on a project with two other developers. You decide the users table needs a last_login_at timestamp. You run ALTER TABLE users ADD COLUMN last_login_at DATETIME; directly in your database GUI, update your PHP code to use that column, and push your changes to GitHub.
Your teammates pull the code. Immediately, their local environments crash. Why? Because their local databases don't have that column. You send them the SQL snippet over Slack, and they run it manually. Then, you deploy to production and forget to run that same snippet. The site goes down. This manual "hand-off" of SQL is fragile and prone to human error.
A migration is simply a way to track which SQL scripts have already been executed against a specific database instance. It's exactly like Git, but for your table structures. Instead of sharing SQL snippets, you share a folder of scripts and a small piece of code that knows how to run them in order.
Moving from manual SQL to a tracked migration system
To do this without a framework, you only need two things: a table to track history and a directory of scripts. First, you create a table called migrations. It only needs two columns: an id and a migration name (the filename). This table acts as the "source of truth" for the current state of the database.
I prefer keeping my migrations as plain .sql files in a folder, numbered sequentially. For example:
001_create_users_table.sql002_add_last_login_to_users.sql003_create_posts_table.sql
Now, instead of running these manually, you write a simple PHP script (the "Runner") that follows this logic:
1. Scan the migrations/ folder for all files.
2. Query the migrations table to see which files have already been recorded.
3. Compare the two lists.
4. Execute the SQL inside any file that exists in the folder but not in the table.
5. After each successful execution, insert that filename into the migrations table.
// A simplified glimpse at the runner logic
$files = glob('migrations/*.sql');
$executed = $pdo->query("SELECT migration FROM migrations")->fetchAll(PDO::FETCH_COLUMN);
foreach ($files as $file) {
$filename = basename($file);
if (!in_array($filename, $executed)) {
$sql = file_get_contents($file);
$pdo->exec($sql);
$stmt = $pdo->prepare("INSERT INTO migrations (migration) VALUES (?)");
$stmt->execute([$filename]);
echo "Applied: $filename\n";
}
}
I've used this exact pattern in legacy projects where adding a 50MB framework just to handle database updates would have been overkill. It's transparent, it's fast, and it ensures that every single environment—from your laptop to the production server—is running the exact same schema.
📋 Practical Task
Build a Schema Versioning System for a Blog Database
Your goal is to create a functional, framework-less migration system for a simple blog. Complete the following steps:
- The Tracking Table: Write the SQL to create a
migrationstable that stores the filename of executed scripts. - The Migration Files: Create a directory named
migrations/. Inside, create two files:001_create_posts.sql(Create apoststable withid,title, andcontent).002_add_status_to_posts.sql(Add astatusVARCHAR column to thepoststable).
- The Runner Script: Write a PHP script that uses PDO to connect to your database, reads the
migrations/folder, and executes any pending SQL files while recording their completion in themigrationstable.
Test your system: Run the script once to apply both migrations. Run it a second time to verify that no SQL is executed twice and no duplicate entries are added to your tracking table.
There are no comments for now.