Validates and fully initializes a set of migrations to reduce the probability of configuration problems as new migrations are introduced. e.g. Checks for missing fields or accidentally duplicated version numbers from copy/pasta problems.
(versions []Migration)
| 866 | // missing fields or accidentally duplicated version numbers from copy/pasta |
| 867 | // problems. |
| 868 | func validateAndInit(versions []Migration) map[int]Migration { |
| 869 | lastVersion := 0 |
| 870 | migrations := make(map[int]Migration, len(versions)) |
| 871 | |
| 872 | for _, versionBundle := range versions { |
| 873 | if versionBundle.SQLDown == "" { |
| 874 | panic(fmt.Sprintf("version bundle should specify Down: %+v", versionBundle)) |
| 875 | } |
| 876 | if versionBundle.SQLUp == "" { |
| 877 | panic(fmt.Sprintf("version bundle should specify Up: %+v", versionBundle)) |
| 878 | } |
| 879 | if versionBundle.Version == 0 { |
| 880 | panic(fmt.Sprintf("version bundle should specify Version: %+v", versionBundle)) |
| 881 | } |
| 882 | |
| 883 | if _, ok := migrations[versionBundle.Version]; ok { |
| 884 | panic(fmt.Sprintf("duplicate version: %03d", versionBundle.Version)) |
| 885 | } |
| 886 | if versionBundle.Version <= lastVersion { |
| 887 | panic(fmt.Sprintf("versions should be ascending; current: %03d, last: %03d", versionBundle.Version, lastVersion)) |
| 888 | } |
| 889 | if versionBundle.Version > lastVersion+1 { |
| 890 | panic(fmt.Sprintf("versions shouldn't skip a sequence number; current: %03d, last: %03d", versionBundle.Version, lastVersion)) |
| 891 | } |
| 892 | |
| 893 | lastVersion = versionBundle.Version |
| 894 | migrations[versionBundle.Version] = versionBundle |
| 895 | } |
| 896 | |
| 897 | return migrations |
| 898 | } |
no outgoing calls
searching dependent graphs…