Mysql 数据库脚本更改规范

版权声明:本文为博主原创文章,未经博主允许不得转载。转载请在文章结尾显眼处附带博主文章地址。 https://blog.csdn.net/qq_27559331/article/details/81141749

1、SQL文件每个月形成一个文件,以日期开头,便于发版执行(如果文件不存在创建一个,文件名格式为:201805.sql)。

2、SQL文件在做数据结构修改前,必须在开头单独起一行备注此次修改做的业务操作和日期。


例如: #=========2018/05/20  添加产品优惠券日期字段,jira #5247 ============。


3、SQL文件在做数据结构操作前,必须判断是否已经存在相应(字段,表,索引)等是否存在,然后再操作,保证幂等性。

4、数据结构更改需单独提交PR修改SQL文件(此PR只包含SQL文件修改),并注明关联的Jira issue,PR内容需注明SQL更改的内容,由指定人员审核通过后方能合并。

相关示例:

/*========== 创建操作日志表,操作日志表用来处理“添加修改数据”这种操作来保证幂等性使用======================*/
    create table if not exists sqloperate_log
    (
        Id varchar(50) not null
            primary key,
        Descriptions varchar(100) null,
        DateTime timestamp default CURRENT_TIMESTAMP not null,
        constraint uniq_Id
            unique (Id)
    );

/*==============示例,添加表=====================*/
    create table if not exists sqloperate_log
    (
        Id varchar(50) not null
    );

/*==============示例,添加列(在需要新增列的时候,使用过如下方式进行新增)=====================*/
    drop procedure if exists schema_change;

    delimiter ';;'    
    create procedure schema_change() begin

    /*add first column for sqloperate_log table */
    if not exists (select * from information_schema.columns where table_name = 'sqloperate_log' and column_name = 'test') then
        /*this is your sql scripts*/
        alter table sqloperate_log add column `test` varchar(255) NULL;      
    end if;


    if not exists (select * from information_schema.columns where table_name = 'sqloperate_log' and column_name = 'test2') then
        alter table sqloperate_log add column `test2` varchar(255) NULL;      
    end if;

    end;;

    delimiter ';'
    call schema_change();

    drop procedure if exists schema_change;

/*==============示例,添加索引(如果需要对表中的列添加索引,使用下面的示例脚本)=====================*/
    drop procedure if exists schema_change;

    delimiter ';;'    
    create procedure schema_change() begin

    if not exists (select * from information_schema.statistics where table_name = 'sqloperate_log' and index_name = 'descriptions_index') then
        /*this is your sql scripts*/
        create index descriptions_index on sqloperate_log(Descriptions);
    end if;

    end;;

    delimiter ';'
    call schema_change();

    drop procedure if exists schema_change;


/*==============示例,添加修改数据(在需要进行一些脚本更改数据或者初始化数据的时候,使用下面的脚本示例。其中title为当前的操作的描述)=====================*/
    drop procedure if exists sqldata_change;

    delimiter ';;'    
    create procedure sqldata_change() begin

    /*this is your sql scripts descriptions*/
    set @title = '向表appinfo中更新数据-示例';

    if not exists (select * from sqloperate_log where Id = sha1(@title)) then
        /*this is your sql scripts*/
        insert into appinfo(PhoneName,CreateTime) values('test phone name',now()); 

        /*add operate logs, don't delete*/
        insert into sqloperate_log(Id,Descriptions) values(sha1(@title),@title);
    end if;

    end;;

    delimiter ';'
    call sqldata_change();
    drop procedure if exists sqldata_change;


/*============清理示例数据==========================*/
 /*   alter table sqloperate_log drop column `test`;
    alter table sqloperate_log drop column `test2`;
    drop index `descriptions_index` on sqloperate_log;
    truncate table sqloperate_log;
    drop table sqloperate_log;
*/

你有困难我帮忙,我住隔壁我姓王。----------------- 你隔壁的老王宣。

猜你喜欢

转载自blog.csdn.net/qq_27559331/article/details/81141749