Migration of DB Schemas

Prince Pereira
3 min readSep 30, 2021

Need of DB Schema Migration

  • When a new field is introduced in newer versions of software.
  • When an existing field is removed in newer versions of software.
  • When an existing field or datatype is changed.

How to achieve DB Schema Migration

  • Maintain versions for Schema structures.
  • Maintain version directory for each version.
  • Formation of higher-level version struct should be made by extending lower-level version.
  • Development code should not directly use the versioned struct. Versioned struct should be aliased and aliased DB schema should be used all over the source code. If the versioned code is used in source code, then any change in the version will make the developer to refactor it in every used place.
    To avoid version dependency, the aliased struct should be kept outside version directories/packages.
  • DB Paths also should have versioning mentioned.
  • Constants to mention current DB version and Previous DB version should be mentioned. This will help to choose which version to be used in DB path and which struct version to be used while doing migration.
  • When application (Pod/Container) comes up, check if migration is required. If migration required do the migration to new version and delete older version information.

--

--

Prince Pereira

Senior Software Engineer - Microsoft | SDN | Java | Golang | DS & Algo | Microservices | Kubernetes | Docker | gRPC & Protocol Buffer