From 76c5e48ef817c16c8e1e1b2c844881658c243b7f Mon Sep 17 00:00:00 2001 From: Ariel Mashraki <7413593+a8m@users.noreply.github.com> Date: Wed, 13 Jan 2021 12:00:30 +0200 Subject: [PATCH] entc/gen: move migrate annotation creation to init (#1165) * fix issue with pointer values in Annotation * entc/gen: move migrate annotation creation to init Co-authored-by: Ruben de Vries --- entc/gen/func.go | 7 +++++- entc/gen/internal/bindata.go | 4 +-- entc/gen/template/migrate/schema.tmpl | 25 ++++++++++++++++--- entc/gen/type_test.go | 17 +++++++++++++ entc/integration/config/ent/migrate/schema.go | 7 +++++- entc/integration/config/ent/schema/user.go | 4 ++- 6 files changed, 55 insertions(+), 9 deletions(-) diff --git a/entc/gen/func.go b/entc/gen/func.go index dc0126b7e..de27f1ac4 100644 --- a/entc/gen/func.go +++ b/entc/gen/func.go @@ -418,7 +418,12 @@ func dumpFields(v interface{}) string { } fv := rv.Field(i) if !fv.IsZero() { - fields = append(fields, fmt.Sprintf("%s: %#v", f.Name, fv.Interface())) + if fv.Kind() == reflect.Ptr { + fv = reflect.Indirect(fv) + fields = append(fields, fmt.Sprintf("%s: &[]%s{%#v}[0]", f.Name, fv.Type(), fv.Interface())) + } else { + fields = append(fields, fmt.Sprintf("%s: %#v", f.Name, fv.Interface())) + } } } return strings.Join(fields, ", ") diff --git a/entc/gen/internal/bindata.go b/entc/gen/internal/bindata.go index 191db144b..9706aa314 100644 --- a/entc/gen/internal/bindata.go +++ b/entc/gen/internal/bindata.go @@ -1070,7 +1070,7 @@ func templateMigrateMigrateTmpl() (*asset, error) { return a, nil } -var _templateMigrateSchemaTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x57\x5b\x6f\xdb\x36\x14\x7e\x96\x7e\xc5\x81\xe0\x15\x6d\x60\x4b\x6d\xde\x26\xc0\x0f\x41\xda\x02\x41\x87\xac\x58\xda\xa7\x20\x18\x18\xea\xc8\x26\x2c\x91\x0a\x45\x65\xf1\x34\xfd\xf7\x81\x17\x49\x94\x6f\x71\xb7\xfa\xc5\xbc\x9c\xeb\x77\x6e\x54\xdb\x26\x17\xe1\xb5\xa8\xb6\x92\xad\xd6\x0a\x2e\xdf\x7f\xf8\x75\x51\x49\xac\x91\x2b\xf8\x4c\x28\x3e\x0a\xb1\x81\x1b\x4e\x63\xb8\x2a\x0a\x30\x44\x35\xe8\x7b\xf9\x8c\x59\x1c\x7e\x5b\xb3\x1a\x6a\xd1\x48\x8a\x40\x45\x86\xc0\x6a\x28\x18\x45\x5e\x63\x06\x0d\xcf\x50\x82\x5a\x23\x5c\x55\x84\xae\x11\x2e\xe3\xf7\xfd\x2d\xe4\xa2\xe1\x59\xc8\xb8\xb9\xff\xed\xe6\xfa\xd3\xed\xdd\x27\xc8\x59\x81\xe0\xce\xa4\x10\x0a\x32\x26\x91\x2a\x21\xb7\x20\x72\x50\x9e\x32\x25\x11\xe3\xf0\x22\xe9\xba\x30\x6c\x5b\xc8\x30\x67\x1c\x21\xaa\xe9\x1a\x4b\x12\x81\x3d\x5e\xc0\x5f\x4c\xad\x01\x5f\x14\xf2\x0c\x66\x10\x7d\x25\x74\x43\x56\x18\x41\x54\xb2\x95\x24\x0a\x23\x58\x74\x5d\x18\xb4\x2d\x28\x2c\xab\x82\x28\x84\x68\x8d\x24\x43\x19\x41\xac\xa5\xb4\x2d\x68\x5e\x2d\x8f\x95\x95\x90\x0a\xde\x1a\x72\x49\xf8\x0a\x61\xf6\xe7\x1c\x66\x1c\xd2\x25\xcc\xe2\x5b\x91\x61\xad\x09\x83\x20\x6a\x5b\x98\xc5\xd7\x82\xe7\x6c\x15\x3b\x9d\xd0\x75\x89\x3e\xe6\xde\x41\xa4\x45\x2d\x06\x05\x41\xb4\x62\x6a\xdd\x3c\xc6\x54\x94\x49\xee\xc0\x4f\x90\xab\x24\x63\xa4\x40\xaa\xf4\xba\x7e\x2a\xa2\x33\x28\xeb\xa7\x22\x71\x60\x9c\xa0\xb6\x14\x49\xce\xb0\xc8\xa2\xf0\x5d\x18\x3e\x13\x69\x1d\x5c\xf8\x1e\x2a\xeb\xe1\x37\xf2\x58\xf4\x2e\x6a\x8a\xe4\x02\x72\xc6\x33\x50\xdb\x0a\x81\x9b\xe8\xdb\xd0\xad\x24\xa9\xd6\x43\xc4\x94\x66\x9b\x03\xcb\x01\x5f\x58\xad\x6a\x30\x51\xb3\x22\x66\x86\x2d\x5d\x02\xe3\x19\xbe\x0c\x28\xbe\x1f\x95\x1c\x07\xba\x6d\x8d\xcc\x27\x98\xa9\xf8\x96\x94\xa8\xb1\x35\x26\xda\x3b\x2b\x7a\xa9\xd9\xcc\xde\xa2\x3c\xc6\xd3\x19\x40\x45\xd1\x94\xbc\xd6\xa2\x2b\x52\x53\x52\x0c\xe2\xfe\x81\x4a\x32\xae\x72\x88\x7e\xa9\xaf\x2d\x55\x64\x19\x93\x04\xb4\x82\x9e\xb5\xeb\x60\x2d\x8a\xac\x36\xbe\xf7\x87\xb9\xb0\xa9\x6f\x72\xc1\x49\xec\xba\xc8\xa2\x11\x1b\xed\x13\x09\x4b\xb8\x7f\xb8\xb0\xf1\x88\xad\xb6\x36\x0c\x26\x10\x50\xe3\xbe\x72\xb7\x2e\x0e\x41\xd0\x82\x96\x9d\x5a\x45\x74\x50\x34\x87\x6f\xdb\x0a\x53\x30\xb1\x8d\xed\x9d\x3e\xd1\x69\x59\x2b\x47\x35\xb7\x12\xda\x85\x46\x72\x46\xe3\xef\x9c\x3d\x35\xfa\x02\xec\x2a\x05\x25\x1b\x9c\xfb\xa0\xf9\xe4\x37\x9c\x4a\x2c\x75\xab\xe8\x3a\x18\x36\xaf\x30\xdd\x36\x45\xe1\xa2\x04\xfd\x3a\x05\x67\xfc\x78\x77\x80\xdf\x14\xf3\x8c\xc6\x77\xec\x6f\xc3\xad\xff\x0d\x67\x7c\x9a\xfe\x4a\x29\xa9\xe9\xf5\xbf\xc5\x29\x36\x08\x1d\xe7\xf8\xc4\x9b\xd2\x44\xc5\x2c\x52\xb8\x7f\xa8\x95\x64\x7c\xd5\xc2\x58\xfa\x26\x6d\x8d\x20\x6d\x3b\x4e\x25\xc2\x29\x7b\x3e\x62\x4e\x9a\xc2\x80\xe6\x96\xe7\x78\x71\x67\x72\x43\x87\xd0\xf8\x3e\xec\x52\x28\x49\x75\x6f\xed\x3b\x60\xe6\x66\x0e\xb3\xe7\x89\xa9\x1b\xbd\x70\xf9\xf2\x3c\x35\x7b\x2c\x0f\x9b\x1a\x5e\x77\x0a\x82\xa1\x64\x4c\x0a\xbf\x52\x30\xa6\x10\xa7\xe5\xa2\xfa\xa8\x8f\xc5\x62\xf3\x1d\x18\xcf\x85\x2c\x89\x62\x82\x9f\x57\x37\x83\xa8\x25\xbc\x71\x35\x63\x14\x9a\x92\xf1\xca\x61\xe4\x37\xee\xb8\xca\x49\x77\xaa\xd7\xdc\x7d\x95\xac\x24\x72\xfb\x05\xb7\xe9\xe1\x4a\xdc\xed\x46\xd5\xc6\xd5\xe3\xc8\xd9\x87\xcd\x27\x65\xf3\xa3\x95\x3b\x54\x85\xee\x61\xd5\xc6\x35\xb1\xa1\x84\xa7\x46\xde\xeb\x2d\x83\xae\x7b\xd8\xc9\x91\x69\x90\x76\xb7\xd6\xb9\xcf\x42\x22\x5b\xf1\x2f\xb8\xad\x7d\xef\xc6\xe3\x83\x1e\xe6\xbd\x87\x1e\xfb\xa8\xd5\xb9\x70\xb7\x2d\x1f\x45\xe1\xf0\xce\x37\xb1\xdd\x0f\x90\xfb\xa8\x1f\x86\x35\x00\xd8\xd3\x4c\x3f\x18\xcd\xf9\x66\x1f\xb2\x7d\x70\x2f\x8f\xa1\x3b\x05\x98\x7e\xe8\x01\xbe\xfc\x51\x84\xf7\x41\x3e\x74\xd2\xcd\x87\xa8\x26\x17\x50\x89\x5a\x55\x82\x23\x48\xcc\x25\x72\xca\xf8\x0a\x94\x00\xf2\x2c\x98\x9d\x98\x74\x8d\x74\xa3\x4f\x0b\x21\xaa\x61\x28\xea\xdf\x1f\x98\xff\x2f\xcc\x46\xfe\xd7\x61\xb3\xe4\xa6\x78\xfe\x1b\x80\x7d\x0f\xf0\x05\x9d\x1a\x9f\x3f\x11\xe5\xbe\x37\xe6\x9b\xf8\x77\xfe\xbd\xca\x88\x9a\x4e\xb7\x5e\x46\x7f\x99\xba\x7e\x13\xf7\xcd\x36\x3c\xa2\x63\x47\xf4\x47\x2c\xf0\xa8\x68\x7b\x79\xae\x68\x6f\xe2\xee\xd6\x68\x3f\x21\x55\x7c\xa3\xdf\x42\x38\xc4\xc1\x6d\xfd\x5c\x30\x47\xed\x5e\xaf\xd1\x69\xc0\xb2\x17\x57\x0f\x3b\x62\xc6\x92\xf5\x3b\x24\xcb\x5e\xa6\x3d\x52\xff\xfa\xe1\xdf\x13\x0c\xcf\x82\x81\xe2\xb5\xfc\xdc\xb7\xcb\xa5\xa7\x16\x77\x2c\xcf\xce\x2d\xea\x9f\x57\xd5\x07\x12\xee\xc0\xd1\xe0\x76\xbf\xd8\x21\x39\x30\x2b\xbd\x14\x22\x5c\x39\x5f\xae\x38\x17\xca\x4e\x39\xc7\x39\x9e\xa4\xf0\xc6\x3e\xee\x3d\x2a\x33\xc6\xb3\xa6\xac\x3e\xeb\x37\x5c\x6d\x45\x99\xb7\xc5\xa1\xd9\xec\xef\x93\x04\xdc\x63\xdd\xce\x5a\x52\x14\x66\xa8\x2a\x7b\xe8\x9e\xe9\x2e\x70\x61\xe0\x68\xfd\x27\xe8\x30\x4e\x5f\xff\x14\x08\xbc\x2e\x70\xea\x25\x30\x0f\xa7\x46\x77\xfa\x83\x23\x6f\x38\x05\xc6\x99\x7a\xfb\x0e\xda\x73\x3f\x3c\x7e\xf8\x05\xb2\x93\x5d\x27\x06\x9b\xff\xba\xf0\xaf\xc7\x34\x1a\xda\x1c\x2c\xe1\xdc\xfe\xb7\x6b\x4b\x0f\x81\xb7\xb6\xdf\xb1\x6e\xf3\x6f\x00\x00\x00\xff\xff\xb0\x6f\xd6\xdb\x96\x0f\x00\x00") +var _templateMigrateSchemaTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xac\x58\x5b\x6f\xdb\x36\x14\x7e\xb6\x7e\xc5\x81\xe0\x15\x6d\xe0\x48\x6d\xde\x66\xc0\x0f\x41\xda\x02\x41\x87\xb4\x58\xd2\xa7\xa0\x18\x18\xea\xc8\x26\x4c\x91\x0a\x45\xa7\xf1\x34\xfd\xf7\x81\x17\x49\x94\x2d\xc7\xce\xd6\xbc\x44\x24\xcf\xf5\x3b\x37\xd2\x75\x9d\x9e\x45\x57\xb2\xdc\x2a\xb6\x5c\x69\xb8\x78\xff\xe1\xf7\xf3\x52\x61\x85\x42\xc3\x67\x42\xf1\x41\xca\x35\x5c\x0b\x9a\xc0\x25\xe7\x60\x89\x2a\x30\xe7\xea\x09\xb3\x24\xba\x5b\xb1\x0a\x2a\xb9\x51\x14\x81\xca\x0c\x81\x55\xc0\x19\x45\x51\x61\x06\x1b\x91\xa1\x02\xbd\x42\xb8\x2c\x09\x5d\x21\x5c\x24\xef\xdb\x53\xc8\xe5\x46\x64\x11\x13\xf6\xfc\x8f\xeb\xab\x4f\x37\xb7\x9f\x20\x67\x1c\xc1\xef\x29\x29\x35\x64\x4c\x21\xd5\x52\x6d\x41\xe6\xa0\x03\x65\x5a\x21\x26\xd1\x59\xda\x34\x51\x54\xd7\x90\x61\xce\x04\x42\x5c\xd1\x15\x16\x24\x06\xb7\x7d\x0e\x3f\x99\x5e\x01\x3e\x6b\x14\x19\x4c\x21\xfe\x46\xe8\x9a\x2c\x31\x86\xb8\x60\x4b\x45\x34\xc6\x70\xde\x34\xd1\xa4\xae\x41\x63\x51\x72\xa2\x11\xe2\x15\x92\x0c\x55\x0c\x89\x91\x52\xd7\x60\x78\x8d\x3c\x56\x94\x52\x69\x78\x6b\xc9\x15\x11\x4b\x84\xe9\x5f\x33\x98\x0a\x98\x2f\x60\x9a\xdc\xc8\x0c\x2b\x43\x38\x99\xc4\x75\x0d\xd3\xe4\x4a\x8a\x9c\x2d\x13\xaf\x13\x9a\x26\x35\xdb\x22\xd8\x88\x8d\xa8\xf3\x4e\xc1\x24\x5e\x32\xbd\xda\x3c\x24\x54\x16\x69\xee\xc1\x4f\x51\xe8\x34\x63\x84\x23\xd5\xe6\xbb\x7a\xe4\xf1\x09\x94\xd5\x23\x4f\x3d\x18\x2f\x50\x3b\x8a\x34\x67\xc8\xb3\x38\x7a\x17\x45\x4f\x44\x39\x07\xcf\x43\x0f\xb5\xf3\xf0\x8e\x3c\xf0\xd6\x45\x43\x91\x9e\x41\xce\x44\x06\x7a\x5b\x22\x08\x1b\x7d\x17\xba\xa5\x22\xe5\xaa\x8b\x98\x36\x6c\x33\x60\x39\xe0\x33\xab\x74\x05\x36\x6a\x4e\xc4\xd4\xb2\xcd\x17\xc0\x44\x86\xcf\x1d\x8a\xef\x7b\x25\x87\x81\xae\x6b\x2b\xf3\x11\xa6\x3a\xb9\x21\x05\x1a\x6c\xad\x89\xee\xcc\x89\x5e\x18\x36\xbb\x76\x28\xf7\xf1\xf4\x06\x50\xc9\x37\x85\xa8\x8c\xe8\x92\x54\x94\xf0\x4e\xdc\x3f\x50\x2a\x26\x74\x0e\xf1\x6f\xd5\x95\xa3\x8a\x1d\x63\x9a\x82\x51\xd0\xb2\x36\x0d\xac\x24\xcf\x2a\xeb\x7b\xbb\x99\x4b\x97\xfa\x36\x17\xbc\xc4\xa6\x89\x1d\x1a\x89\xd5\x3e\x90\xb0\x80\xfb\x1f\x67\x2e\x1e\x89\xd3\x56\x47\x93\x01\x04\xd4\xba\xaf\xfd\xa9\x8f\xc3\x64\x52\x83\x91\x3d\x77\x8a\x68\xa7\x68\x06\x77\xdb\x12\xe7\x60\x63\x9b\xb8\x33\xb3\x63\xd2\xb2\xd2\x9e\x6a\xe6\x24\xd4\xe7\x06\xc9\x29\x4d\xbe\x0b\xf6\xb8\x31\x07\xe0\xbe\xe6\xa0\xd5\x06\x67\x21\x68\x21\xf9\xb5\xa0\x0a\x0b\xd3\x2a\x9a\x06\xba\xc5\x11\xa6\x9b\x0d\xe7\x3e\x4a\xd0\x7e\xcf\xc1\x1b\xdf\x9f\x8d\xf0\xdb\x62\x9e\xd2\xe4\x96\xfd\x6d\xb9\xcd\x7f\xcb\x99\xbc\x4c\x7f\xa9\xb5\x32\xf4\xe6\xbf\xc3\x29\xb1\x08\x1d\xe6\xf8\x24\x36\x85\x8d\x8a\xfd\x98\xc3\xfd\x8f\x4a\x2b\x26\x96\x35\xf4\xa5\x6f\xd3\xd6\x0a\x32\xb6\xe3\x50\x22\xbc\x64\xcf\x47\xcc\xc9\x86\x5b\xd0\xfc\xe7\x29\x5e\xdc\xda\xdc\x30\x21\xb4\xbe\x77\xab\x39\x14\xa4\xbc\x77\xf6\x8d\x98\xb9\x9e\xc1\xf4\x69\x60\xea\xda\x7c\xf8\x7c\x79\x1a\x9a\xdd\x97\x87\x4b\x8d\xa0\x3b\x4d\x26\x5d\xc9\xd8\x14\x3e\x52\x30\xb6\x10\x87\xe5\xa2\xdb\xa8\xf7\xc5\xe2\xf2\x1d\x98\xc8\xa5\x2a\x88\x66\x52\x9c\x56\x37\x9d\xa8\x05\xbc\xf1\x35\x63\x15\xda\x92\x09\xca\xa1\xe7\xb7\xee\xf8\xca\x99\xef\x54\xaf\x3d\xfb\xa6\x58\x41\xd4\xf6\x0b\x6e\xe7\xe3\x95\xb8\xdb\x8d\xca\xb5\xaf\xc7\x9e\xb3\x0d\x5b\x48\xca\x66\x07\x2b\xb7\xab\x0a\xd3\xc3\xca\xb5\x6f\x62\x5d\x09\x0f\x8d\xbc\x37\x4b\x06\x4d\xf3\x63\x27\x47\x86\x41\xda\x5d\x3a\xe7\x3e\x4b\x85\x6c\x29\xbe\xe0\xb6\x0a\xbd\xeb\xb7\x47\x3d\xcc\x5b\x0f\x03\xf6\x5e\xab\x77\xe1\x76\x5b\x3c\x48\xee\xf1\xce\xd7\x89\x5b\x77\x90\x87\xa8\x8f\xc3\x3a\x01\xd8\xd3\x4c\x3f\x58\xcd\xf9\x7a\x1f\xb2\x7d\x70\x2f\x0e\xa1\x3b\x04\x98\x7e\x68\x01\xbe\x78\x2d\xc2\xfb\x20\x8f\xed\x34\xb3\x2e\xaa\xe9\x19\x94\xb2\xd2\xa5\x14\x08\x0a\x73\x85\x82\x32\xb1\x04\x2d\x81\x3c\x49\xe6\x26\x26\x5d\x21\x5d\x9b\x5d\x2e\x65\xd9\x0d\x45\xf3\xf7\x27\xe6\xff\x0b\xb3\x9e\xff\x38\x6c\x8e\xdc\x16\xcf\x7f\x03\xb0\xed\x01\xa1\xa0\x97\xc6\xe7\x2f\x44\xb9\xed\x8d\xf9\x3a\xf9\x2a\xbe\x97\x19\xd1\xc3\xe9\xd6\xca\x68\x0f\xe7\xbe\xdf\x24\x6d\xb3\x8d\x0e\xe8\xd8\x11\xfd\x11\x39\x1e\x14\xed\x0e\x4f\x15\x1d\x4c\xdc\xdd\x1a\x6d\x27\xa4\x4e\xae\xcd\x5d\x08\xbb\x38\xf8\x65\x98\x0b\x76\xab\xde\xeb\x35\x26\x0d\x58\xf6\xec\xeb\x61\x47\x4c\x5f\xb2\x61\x87\x64\xd9\xf3\xb0\x47\x9a\xbf\x76\xf8\xb7\x04\xdd\xb5\xa0\xa3\x38\x96\x9f\xfb\x76\xf9\xf4\x34\xe2\x0e\xe5\xd9\xa9\x45\xfd\xeb\xaa\x7a\x24\xe1\x46\xb6\x3a\xb7\xdb\x8f\x1d\x92\xf1\x59\x19\xae\xd3\x14\xfc\xe5\xd9\xcd\x3e\xc2\xb9\x1d\x72\xda\x6d\xfa\x6b\xb3\x07\x32\x9a\x78\xda\xf0\x4a\xd8\x8d\xb7\xe3\x57\xf3\x49\x50\x95\x2f\x4d\xe6\x59\x34\x34\xba\x31\x0f\x80\x7c\x23\x28\x30\xc1\xf4\xdb\x77\x50\x0f\x1e\x02\x07\x1e\x01\xaf\xbe\x0d\xec\x44\xfa\x85\x21\x13\x4e\xfa\xf0\xb8\x0f\x69\xd7\x72\x60\x01\xa7\xf6\xa2\x5d\x5b\x06\xcf\x01\x57\xf5\x44\x68\x6f\xd4\xa5\x10\x52\xbb\x8b\xc9\x88\x4d\xc1\xe9\x02\xde\xb8\xc7\x59\xb0\x59\xef\xdc\xe1\x88\xd0\xdd\x03\xc5\x25\xd2\x9d\xbb\xfc\xf6\x57\xd2\x91\xee\xb0\x23\xe0\x6a\x45\x54\x85\xba\x13\xe1\xd7\xaf\x14\x22\x39\x0f\xbd\xb2\x05\xed\x76\x5e\x27\xe8\x6b\x69\x78\xfa\xf2\xf4\xeb\x23\x42\x9a\x68\x4f\x50\xf7\x7a\x20\xbc\xd7\x34\x0a\xf5\x80\x74\x01\x02\x7f\xbe\x7d\x90\x92\xbf\xb3\x3c\x67\xa7\x31\xd5\xb5\x53\xcd\x84\xfb\x81\xc1\x9a\x6a\x5e\x2d\xa6\x45\xf0\xca\x30\xe7\x84\x57\x38\xe8\x18\x7b\xc9\xd2\x2e\x82\x6f\xf7\x6b\x84\x5f\xfc\x1b\x00\x00\xff\xff\x34\xc4\x14\x6d\x5c\x11\x00\x00") func templateMigrateSchemaTmplBytes() ([]byte, error) { return bindataRead( @@ -1085,7 +1085,7 @@ func templateMigrateSchemaTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/migrate/schema.tmpl", size: 3990, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "template/migrate/schema.tmpl", size: 4444, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/gen/template/migrate/schema.tmpl b/entc/gen/template/migrate/schema.tmpl index 211bc1b4f..e91ea07ec 100644 --- a/entc/gen/template/migrate/schema.tmpl +++ b/entc/gen/template/migrate/schema.tmpl @@ -97,9 +97,6 @@ var ( {{- end }} }, {{- end }} - {{- with $ant := $t.Annotation }} - Annotation: &entsql.Annotation{ {{ dumpFields $ant }} }, - {{- end }} } {{- end }} // Tables holds all the tables in the schema. @@ -111,11 +108,31 @@ var ( ) func init() { - {{- range $_, $t := $.Tables }} + {{- range $t := $.Tables }} {{- $table := pascal $t.Name | printf "%sTable" }} {{- range $i, $fk := $t.ForeignKeys }} {{ $table }}.ForeignKeys[{{ $i }}].RefTable = {{ pascal $fk.RefTable.Name | printf "%sTable" }} {{- end }} + {{- with $ant := $t.Annotation }} + {{ $table }}.Annotation = &entsql.Annotation{ + {{- with $ant.Table }} + Table: "{{ . }}", + {{- end }} + {{- with $ant.Charset }} + Charset: "{{ . }}", + {{- end }} + {{- with $ant.Collation }} + Collation: "{{ . }}", + {{- end }} + {{- with $ant.Options }} + Options: "{{ . }}", + {{- end }} + } + {{- with $ant.Incremental }} + {{ $table }}.Annotation.Incremental = new(bool) + *{{ $table }}.Annotation.Incremental = {{ with indirect . }}true{{ else }}false{{ end }} + {{- end }} + {{- end }} {{- end }} } diff --git a/entc/gen/type_test.go b/entc/gen/type_test.go index 6b159ba00..f9dcef056 100644 --- a/entc/gen/type_test.go +++ b/entc/gen/type_test.go @@ -273,6 +273,23 @@ func TestField_DefaultName(t *testing.T) { } } +func TestField_incremental(t *testing.T) { + tests := []struct { + annotations map[string]interface{} + def bool + expected bool + }{ + {dict("EntSQL", nil), false, false}, + {dict("EntSQL", nil), true, true}, + {dict("EntSQL", dict("incremental", true)), false, true}, + {dict("EntSQL", dict("incremental", false)), true, false}, + } + for _, tt := range tests { + typ := &Field{Annotations: tt.annotations} + require.Equal(t, tt.expected, typ.incremental(tt.def)) + } +} + func TestBuilderField(t *testing.T) { tests := []struct { name string diff --git a/entc/integration/config/ent/migrate/schema.go b/entc/integration/config/ent/migrate/schema.go index fb7f95416..8df71a102 100644 --- a/entc/integration/config/ent/migrate/schema.go +++ b/entc/integration/config/ent/migrate/schema.go @@ -24,7 +24,6 @@ var ( Columns: UsersColumns, PrimaryKey: []*schema.Column{UsersColumns[0]}, ForeignKeys: []*schema.ForeignKey{}, - Annotation: &entsql.Annotation{Table: "Users", Charset: "utf8mb4"}, } // Tables holds all the tables in the schema. Tables = []*schema.Table{ @@ -33,4 +32,10 @@ var ( ) func init() { + UsersTable.Annotation = &entsql.Annotation{ + Table: "Users", + Charset: "utf8mb4", + } + UsersTable.Annotation.Incremental = new(bool) + *UsersTable.Annotation.Incremental = false } diff --git a/entc/integration/config/ent/schema/user.go b/entc/integration/config/ent/schema/user.go index 40d76a0cb..3e4c8a13f 100644 --- a/entc/integration/config/ent/schema/user.go +++ b/entc/integration/config/ent/schema/user.go @@ -57,9 +57,11 @@ func (User) Mixin() []ent.Mixin { // Annotations of the User schema. func (User) Annotations() []schema.Annotation { + incremental := false return []schema.Annotation{ entsql.Annotation{ - Table: "Users", + Table: "Users", + Incremental: &incremental, }, } }