How one should handle incremental database schema evolution

99封情书 提交于 2019-12-11 23:48:30

问题


I have a Play framework powered application with database as a persistance layer (and I use Slick for that). I have enabled evolutions, generated 1.sql file and successfully rolled it out to production.

Client requests new features that require database schema modifications - ie. adding new tables, adding new columns and changes to existing columns' nullability.

Once all Slick's Table definitions and related code are updated, I generate schema once again and place it as 2.sql. Evolutions are correctly requested to be run but... generated evolutions does not reflect an incremental update on top of 1.sql state but rather instructions how to create database schema from scratch (ie. CREATE TABLE with all the columns, including new ones rather than ADD COLUMN casuses).

Is it possible to achieve an incremental update so that I can easily just run it in production to get database from revision #1 to revision #2 (SQL "diff" between #1 and #2) or do I have to manually create those evolutions?


回答1:


You do have to manually create those evolutions after v1.

For our products we leave the "generate evolutions" turned on while products are in pre-ship development. Once v1 is ready to be deployed, we turn off the automatic evolution generation and start with manual evolutions.

While this may feel sub-optimal, as a team we feel more comfortable in knowing that humans are coding and reviewing DDL (and sometimes DML) SQL statements for evolutions, rather than an automated script deciding that perhaps a table should be dropped and recreated. In some products, we have several hundred evos spanning years of continuous improvement and adaptation. The system works really well.




回答2:


You need to put only differences into the evolution scripts.

Example

1.sql

CREATE TABLE Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

2.sql

ALTER TABLE Persons ADD Email varchar(255);

Now, your issue:

I generate schema once again and place it as 2.sql

You generated it again so your files look like

1.sql

CREATE TABLE Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);

2.sql

CREATE TABLE Persons (
    PersonID int,
    LastName varchar(255),
    FirstName varchar(255),
    Address varchar(255),
    City varchar(255) 
);
ALTER TABLE Persons ADD Email varchar(255);

You need to put to the 2.sql only incremental changes to the scheme, like in my example.



来源:https://stackoverflow.com/questions/54174366/how-one-should-handle-incremental-database-schema-evolution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!