Skip to content
forked from weng/msmt

a php cli script which copy the ruby on rails database schema migration function

Notifications You must be signed in to change notification settings

picasso250/msmt

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MSMT-->Mysql Schema Migration Tool


This is a simple php CLI script for mysql server database schema migration

The idea was inspired by Ruby on Rails schema migration prepare the environment: Ensure the php or php.exe is in you path so you can execute it

Usage:

    1. Edit msmt.php,set the database connection information.
    1. php -f msmt.php g or php -f msmt.php generate name

    The "name" shouble be a meaningful word.For example:add_index_to_post,create_account_table,etc.A sql file is created in the "migrations" directory.Fill native mysql statement in this sql file.

    1. php -f msmt.php migrate or php -f msmt.php m

    MSMT will execute all the sql files which was never executed on your mysql server.The execution order is from the oldest created to the newest created.

Tips:

  • You can create multiple migrations then execute migrate once.

  • This script support multi sql statement in a migration sql file while every single statement should be a new line.I don't recommand you to do this because DDL cannot be rallback .It means some statements can be executed while some others not,and there is no migration log created.This is actally enter a wrong state and you need to carefully clean the database by hand, by yourself.

  • A table schema__migrations is created in your current database.DON'T DROP IT.It was used to store the executed log.

这是一个简单的php控制台脚本,作用是管理mysql数据库结构的创建和更改。思路来自于Ruby on rails框架。 准备环境: 确保php或者php.exe这个可执行文件在你的path这个环境变量里面,这样你才可以直接调用它来执行一个php脚本 用法:

  • 0.用编辑器修改msmt.php,配置上面几行,连接你的mysql数据库

  • 1.执行php -f msmt.php g name 或者php -f generate name 这将生成一个.sql文件,此sql文件的名称由时间戳和name组成。建议name写的足够好,可以清晰表示你这次sql要做什么,例如:add_index_to_post ,create_account_table。生成的sql文件放置在当前目录下的migrations目录内,请在里面写你的sql,注意需要是正确的mysql语法。

  • 2.执行php -f msmt.php migrate 或者 php -f msmt.php m

    migrations目录下面,所有从未被执行过的sql文件,将在你的mysql数据库上面被依次执行一边,按照生成时间从前到后的顺序。

注意:

  • 你可以生成多个migrations的sql文件,然后一次性执行一下

  • 一个sql文件里面是可以写多条sql的,但是每条sql都应该独占一行.一般情况下,不推荐这样做。虽然开启了transaction,但DDL是不可rollback的。也就是说,如果你某些行的sql有错,执行后,某几行生效了,某几行未生效,而且这次migration未生成log。这个时候其实数据库就处于一个错误的状态了,你就必须小心地,自行手工处理好这个问题。

  • 该脚本在你的数据库里面生成了一个schema__migrations表,它是用来存储执行过的migrations的log的,请勿删除该表

About

a php cli script which copy the ruby on rails database schema migration function

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%