Databases 5 min read

Using topthink/think-migration for Database Migrations in ThinkPHP

This guide explains how to install and use the topthink/think-migration package with ThinkPHP to create, run, and roll back database migration scripts, covering version compatibility, command‑line operations, migration class creation, and practical examples for creating, updating, and deleting tables and columns.

php Courses
php Courses
php Courses
Using topthink/think-migration for Database Migrations in ThinkPHP

ThinkPHP provides a database migration tool called topthink/think-migration that helps manage schema changes through versioned migration classes.

1. Install topthink/think-migration

Make sure the package version matches your ThinkPHP version (e.g., ThinkPHP 5.1 works with the 2.0 series). Install it via Composer: composer require topthink/think-migration=2.0.* After installation, verify it by running the ThinkPHP command line tool: php think If the migration commands appear, the installation succeeded.

2. Create a migration class

Generate a new migration file with: php think migrate:create CreateUser The command creates a file under ./database/migrations (note the original typo in the source).

3. Implement migration logic

Each migration class can define three methods: up (executed on migrate:run), down (executed on migrate:rollback), and change (executed on both run and rollback if present). The author prefers removing change and using up for applying changes and down for reverting them.

4. Example: Create a new table

The up method can create a user table with columns, indexes, and timestamps:

// create the table
$table = $this->table('user', ['id' => 'user_id', 'comment' => '用户表', 'engine' => 'MyISAM', '']);
$table->addColumn('user_name', 'string', ['limit' => 15, 'default' => '', 'comment' => '用户名'])
    ->addColumn('password', 'string', ['limit' => 15, 'default' => '', 'comment' => '密码'])
    ->addColumn('status', 'boolean', ['limit' => 1, 'default' => 0, 'comment' => '状态'])
    ->addIndex(['user_name'], ['unique' => true]) // unique index
    ->addTimestamps() // creates create_time and update_time
    ->create();

5. Example: Update an existing table

Add a new column to the user table:

$this->table('user')
    ->addColumn('test', 'string', ['limit' => 15, 'default' => '', 'comment' => '测试']) // add test field
    ->update();

6. Example: Drop a table $this->table('user')->drop(); 7. Example: Drop a column

$this->table('user')
    ->removeColumn('test') // remove test field
    ->save();

8. Common migration commands

Three frequently used commands are:

php think migrate:create CreateUser   # create a migration class
php think migrate:run               # execute migrations
php think migrate:rollback          # roll back the last batch

For the full tutorial and additional screenshots, refer to the original article linked below.

Read the original article

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIPHPdatabase migrationComposerThinkPHP
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.