|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | +"flag" |
| 5 | +"fmt" |
| 6 | +"log" |
| 7 | +"os/exec" |
| 8 | +"strings" |
| 9 | + |
| 10 | +"golang.org/x/xerrors" |
| 11 | + |
| 12 | +"golang.org/x/mod/semver" |
| 13 | +) |
| 14 | + |
| 15 | +// main will print out the number of migrations added between each release. |
| 16 | +// All upgrades are categorized as either major, minor, or patch based on semver. |
| 17 | +// |
| 18 | +// This isn't an exact science and is opinionated. Upgrade paths are not |
| 19 | +// always strictly linear from release to release. Users can skip patches for |
| 20 | +// example. |
| 21 | +funcmain() { |
| 22 | +varincludePatchesbool |
| 23 | +varincludeMinorsbool |
| 24 | +varincludeMajorsbool |
| 25 | +varafterV2bool |
| 26 | +varlistMigsbool |
| 27 | +varmigrationDirectorystring |
| 28 | +varversionListstring |
| 29 | + |
| 30 | +// If you only run with --patches, the upgrades that are minors are excluded. |
| 31 | +// Example being 1.0.0 -> 1.1.0 is a minor upgrade, so it's not included. |
| 32 | +flag.BoolVar(&includePatches,"patches",false,"Include patches releases") |
| 33 | +flag.BoolVar(&includeMinors,"minors",false,"Include minor releases") |
| 34 | +flag.BoolVar(&includeMajors,"majors",false,"Include major releases") |
| 35 | +flag.StringVar(&versionList,"versions","","Comma separated list of versions to use. This skips uses git tag to find tags.") |
| 36 | +flag.BoolVar(&afterV2,"after-v2",false,"Only include releases after v2.0.0") |
| 37 | +flag.BoolVar(&listMigs,"list",false,"List migrations") |
| 38 | +flag.StringVar(&migrationDirectory,"dir","coderd/database/migrations","Migration directory") |
| 39 | +flag.Parse() |
| 40 | + |
| 41 | +if!includePatches&&!includeMinors&&!includeMajors&&versionList=="" { |
| 42 | +usage() |
| 43 | +return |
| 44 | +} |
| 45 | + |
| 46 | +varvList []string |
| 47 | +ifversionList!="" { |
| 48 | +// Include all for printing purposes. |
| 49 | +includeMajors=true |
| 50 | +includeMinors=true |
| 51 | +includePatches=true |
| 52 | +vList=strings.Split(versionList,",") |
| 53 | +} |
| 54 | + |
| 55 | +err:=run(Options{ |
| 56 | +VersionList:vList, |
| 57 | +IncludePatches:includePatches, |
| 58 | +IncludeMinors:includeMinors, |
| 59 | +IncludeMajors:includeMajors, |
| 60 | +AfterV2:afterV2, |
| 61 | +ListMigrations:listMigs, |
| 62 | +MigrationDirectory:migrationDirectory, |
| 63 | +}) |
| 64 | +iferr!=nil { |
| 65 | +log.Fatal(err) |
| 66 | +} |
| 67 | +} |
| 68 | + |
| 69 | +funcusage() { |
| 70 | +_,_=fmt.Println("Usage: releasemigrations [--patches] [--minors] [--majors] [--list]") |
| 71 | +_,_=fmt.Println("Choose at lease one of --patches, --minors, or --majors. You can choose all!") |
| 72 | +_,_=fmt.Println("Must be run from the coder repo at the root.") |
| 73 | +} |
| 74 | + |
| 75 | +typeOptionsstruct { |
| 76 | +VersionList []string |
| 77 | +IncludePatchesbool |
| 78 | +IncludeMinorsbool |
| 79 | +IncludeMajorsbool |
| 80 | +AfterV2bool |
| 81 | +ListMigrationsbool |
| 82 | +MigrationDirectorystring |
| 83 | +} |
| 84 | + |
| 85 | +func (oOptions)Filter(tags []string) []string { |
| 86 | +ifo.AfterV2 { |
| 87 | +fori,tag:=rangetags { |
| 88 | +iftag=="v2.0.0" { |
| 89 | +tags=tags[i:] |
| 90 | +break |
| 91 | +} |
| 92 | +} |
| 93 | +} |
| 94 | + |
| 95 | +ifo.IncludeMajors&&o.IncludeMinors&&o.IncludePatches { |
| 96 | +returntags |
| 97 | +} |
| 98 | + |
| 99 | +filtered:=make([]string,0,len(tags)) |
| 100 | +current:=tags[0] |
| 101 | +filtered=append(filtered,current) |
| 102 | +fori:=1;i<len(tags);i++ { |
| 103 | +a:=current |
| 104 | +current=tags[i] |
| 105 | + |
| 106 | +vDiffType:=versionDiff(a,tags[i]) |
| 107 | +if!o.IncludeMajors&&vDiffType=="major" { |
| 108 | +continue |
| 109 | +} |
| 110 | +if!o.IncludeMinors&&vDiffType=="minor" { |
| 111 | +// This isn't perfect, but we need to include |
| 112 | +// the first minor release for the first patch to work. |
| 113 | +// Eg: 1.0.0 -> 1.1.0 -> 1.1.1 |
| 114 | +//If we didn't include 1.1.0, then the 1.1.1 patch would |
| 115 | +// apply to 1.0.0 |
| 116 | +if!o.IncludePatches { |
| 117 | +continue |
| 118 | +} |
| 119 | +} |
| 120 | +if!o.IncludePatches&&vDiffType=="patch" { |
| 121 | +continue |
| 122 | +} |
| 123 | +filtered=append(filtered,tags[i]) |
| 124 | +} |
| 125 | + |
| 126 | +returnfiltered |
| 127 | +} |
| 128 | + |
| 129 | +funcrun(optsOptions)error { |
| 130 | +vartags []string |
| 131 | +iflen(opts.VersionList)>0 { |
| 132 | +tags=opts.VersionList |
| 133 | +}else { |
| 134 | +varerrerror |
| 135 | +tags,err=gitTags() |
| 136 | +iferr!=nil { |
| 137 | +returnxerrors.Errorf("gitTags: %w",err) |
| 138 | +} |
| 139 | +tags=opts.Filter(tags) |
| 140 | +} |
| 141 | + |
| 142 | +patches:=make([]string,0) |
| 143 | +minors:=make([]string,0) |
| 144 | +majors:=make([]string,0) |
| 145 | +patchesHasMig:=0 |
| 146 | +minorsHasMig:=0 |
| 147 | +majorsHasMig:=0 |
| 148 | + |
| 149 | +fori:=0;i<len(tags)-1;i++ { |
| 150 | +a:=tags[i] |
| 151 | +b:=tags[i+1] |
| 152 | + |
| 153 | +migrations,err:=hasMigrationDiff(opts.MigrationDirectory,a,b) |
| 154 | +iferr!=nil { |
| 155 | +returnxerrors.Errorf("hasMigrationDiff %q->%q: %w",a,b,err) |
| 156 | +} |
| 157 | + |
| 158 | +vDiff:=fmt.Sprintf("%s->%s",a,b) |
| 159 | +vDiffType:=versionDiff(a,b) |
| 160 | +skipPrint:=true |
| 161 | +switchvDiffType { |
| 162 | +case"major": |
| 163 | +majors=append(majors,vDiff) |
| 164 | +iflen(migrations)>0 { |
| 165 | +majorsHasMig++ |
| 166 | +} |
| 167 | +skipPrint=!opts.IncludeMajors |
| 168 | +case"minor": |
| 169 | +minors=append(minors,vDiff) |
| 170 | +iflen(migrations)>0 { |
| 171 | +minorsHasMig++ |
| 172 | +} |
| 173 | +skipPrint=!opts.IncludeMinors |
| 174 | +case"patch": |
| 175 | +patches=append(patches,vDiff) |
| 176 | +iflen(migrations)>0 { |
| 177 | +patchesHasMig++ |
| 178 | +} |
| 179 | +skipPrint=!opts.IncludePatches |
| 180 | +} |
| 181 | + |
| 182 | +ifskipPrint { |
| 183 | +continue |
| 184 | +} |
| 185 | + |
| 186 | +ifmigrations!=nil { |
| 187 | +log.Printf("[%s] %d migrations added between %s and %s\n",vDiffType,len(migrations),a,b) |
| 188 | +ifopts.ListMigrations { |
| 189 | +for_,migration:=rangemigrations { |
| 190 | +log.Printf("\t%s",migration) |
| 191 | +} |
| 192 | +} |
| 193 | +}else { |
| 194 | +log.Printf("[%s] No migrations added between %s and %s\n",vDiffType,a,b) |
| 195 | +} |
| 196 | +} |
| 197 | + |
| 198 | +log.Printf("Patches: %d (%d with migrations)\n",len(patches),patchesHasMig) |
| 199 | +log.Printf("Minors: %d (%d with migrations)\n",len(minors),minorsHasMig) |
| 200 | +log.Printf("Majors: %d (%d with migrations)\n",len(majors),majorsHasMig) |
| 201 | + |
| 202 | +returnnil |
| 203 | +} |
| 204 | + |
| 205 | +funcversionDiff(a,bstring)string { |
| 206 | +ac,bc:=semver.Canonical(a),semver.Canonical(b) |
| 207 | +ifsemver.Major(ac)!=semver.Major(bc) { |
| 208 | +return"major" |
| 209 | +} |
| 210 | +ifsemver.MajorMinor(ac)!=semver.MajorMinor(bc) { |
| 211 | +return"minor" |
| 212 | +} |
| 213 | +return"patch" |
| 214 | +} |
| 215 | + |
| 216 | +funchasMigrationDiff(dirstring,a,bstring) ([]string,error) { |
| 217 | +cmd:=exec.Command("git","diff", |
| 218 | +// Only added files |
| 219 | +"--diff-filter=A", |
| 220 | +"--name-only", |
| 221 | +a,b,dir) |
| 222 | +output,err:=cmd.Output() |
| 223 | +iferr!=nil { |
| 224 | +returnnil,xerrors.Errorf("%s\n%s",strings.Join(cmd.Args," "),err) |
| 225 | +} |
| 226 | +iflen(output)==0 { |
| 227 | +returnnil,nil |
| 228 | +} |
| 229 | + |
| 230 | +migrations:=strings.Split(strings.TrimSpace(string(output)),"\n") |
| 231 | +filtered:=make([]string,0,len(migrations)) |
| 232 | +for_,migration:=rangemigrations { |
| 233 | +migration:=migration |
| 234 | +ifstrings.Contains(migration,"fixtures") { |
| 235 | +continue |
| 236 | +} |
| 237 | +// Only show the ups |
| 238 | +ifstrings.HasSuffix(migration,".down.sql") { |
| 239 | +continue |
| 240 | +} |
| 241 | +filtered=append(filtered,migration) |
| 242 | +} |
| 243 | +returnfiltered,nil |
| 244 | +} |
| 245 | + |
| 246 | +funcgitTags() ([]string,error) { |
| 247 | +cmd:=exec.Command("git","tag") |
| 248 | +output,err:=cmd.Output() |
| 249 | +iferr!=nil { |
| 250 | +returnnil,err |
| 251 | +} |
| 252 | + |
| 253 | +tags:=strings.Split(string(output),"\n") |
| 254 | + |
| 255 | +// Sort by semver |
| 256 | +semver.Sort(tags) |
| 257 | + |
| 258 | +filtered:=make([]string,0,len(tags)) |
| 259 | +for_,tag:=rangetags { |
| 260 | +iftag!=""&&semver.IsValid(tag) { |
| 261 | +filtered=append(filtered,tag) |
| 262 | +} |
| 263 | +} |
| 264 | + |
| 265 | +returnfiltered,nil |
| 266 | +} |