Study Initial schema generation and how to make changes in model after migration to single database
Time to time we are doing some small changes in our model. Sometimes we just adding new fields
But sometimes https://github.com/eclipse/che/issues/1820 we change completely structure of some object.
We need to figure out how :
- Change structure of database from previus version or much earlier version
- What to do with existing data during migration.
After a discussion and some study and investigation process it was decided to define an explicit database schema based on a current version generated by EclipseLink. This approach allows us to create native migration scripts, avoid useless mappings for those entries which need foreign keys, use all the power of abilities of SQL standard. Consider the following scripts hierarchy:
resources/
/sql
/5.0.0
1.init.sql
/5.0.0-M1
1.rename_fields.sql
2.add_workspace_constraint.sql
/postgresql
2.add_workspace_constraint.sql
/5.0.1
1.stacks_migration.sql
From the example
- There is a directory called
sqlwhich contains initialization/migration scripts - There are version directories under the
sqllike5.0.0or5.0.0-M1these directories contain scripts for specific versions - There are sql scripts under project version duiretories line
1.init.sqlor1.rename_fields.sqlthe naming of scripts is pretty simple: the first is a script version in a project versions it's1in1.init.sqlthen description of changes(if necessary)initin1.init.sqland then the file extension.sqlin1.init.sql - There is a directory under
5.0.0-M1calledpostresqlif current database provider is posgresql then the script from5.0.0-M1/posgresql/2.add_workspace_constraint.sqlwill be used instead of5.0.0-M1/2.add_workspace_constraint.sql, so basically if the same script name is provided in provider-specific directory then this script will be used instead
in this way we can easily create initialization scripts, extend an existing schema and manage overrides for different database. It also allows us to develop one version migrations in separate task simultaneously by versioning scripts under current project version.