example/ent: add example for adding enums not at the end (#3980)

This commit is contained in:
Ariel Mashraki
2024-03-17 12:30:45 +02:00
committed by GitHub
parent 0a32b6f2bf
commit 09dc9675a6
6 changed files with 9 additions and 5 deletions

View File

@@ -25,7 +25,7 @@ atlas migrate diff <migration_name> \
```bash
atlas migrate lint \
--dev-url="mysql://root:pass@localhost:3306/test" \
--dev-url="docker://mysql/8/dev" \
--dir="file://ent/migrate/migrations" \
--latest=1
```

View File

@@ -0,0 +1,2 @@
-- Modify "payments" table
ALTER TABLE `payments` MODIFY COLUMN `currency` enum('USD','EUR','VND','ILS') NOT NULL;

View File

@@ -1,4 +1,4 @@
h1:vhKIBaVqxvUHieIcRzt0ZNPm4Bw8GLBZVObuChJfSWk=
h1:QMBPKnW6IBUAFXLNorT9V6E3OkehchN72/bgEHavP/0=
20221114082343_create_users.sql h1:EeOPKbOeYUHO830P1MCXKUsxT3wLk/hSgcpT9GzlGZQ=
20221114090322_add_age.sql h1:weRCTqS5cqKyrvIjGXk7Bn24YUXo0nhLNWD0EZBcaoQ=
20221114101516_add_name.sql h1:leQgI2JN3URZyujlNzX3gI53MSiTA02MKI/G9cnMu6I=
@@ -12,3 +12,4 @@ h1:vhKIBaVqxvUHieIcRzt0ZNPm4Bw8GLBZVObuChJfSWk=
20231231120006_add_session.sql h1:n725mYZBuwUU2Nl8XdUFDgn2Dbm2kpVwAASEtWOqQuo=
20231231123114_hash_card.sql h1:ZsZUe2+vJ6echLKnQxBhXyVoBJja2IAY7rX/P5UZ0iE=
20240229200634_add_card_type.sql h1:y6kam4J45wzdgUh7eU9TTgbs8GwKuqNWyfqaFEtNgbY=
20240317101809_add_payment_vnd.sql h1:IuRWMJvwkPoZJ+T9luhjvnaBwGQIvgU5vJbduE4HMok=

View File

@@ -40,7 +40,7 @@ var (
PaymentsColumns = []*schema.Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "amount", Type: field.TypeFloat64},
{Name: "currency", Type: field.TypeEnum, Enums: []string{"USD", "EUR", "ILS"}},
{Name: "currency", Type: field.TypeEnum, Enums: []string{"USD", "EUR", "VND", "ILS"}},
{Name: "time", Type: field.TypeTime},
{Name: "description", Type: field.TypeString},
{Name: "status", Type: field.TypeEnum, Enums: []string{"pending", "completed", "failed"}},

View File

@@ -76,6 +76,7 @@ type Currency string
const (
CurrencyUSD Currency = "USD"
CurrencyEUR Currency = "EUR"
CurrencyVND Currency = "VND"
CurrencyILS Currency = "ILS"
)
@@ -86,7 +87,7 @@ func (c Currency) String() string {
// CurrencyValidator is a validator for the "currency" field enum values. It is called by the builders before save.
func CurrencyValidator(c Currency) error {
switch c {
case CurrencyUSD, CurrencyEUR, CurrencyILS:
case CurrencyUSD, CurrencyEUR, CurrencyVND, CurrencyILS:
return nil
default:
return fmt.Errorf("payment: invalid enum value for currency field: %q", c)

View File

@@ -25,7 +25,7 @@ func (Payment) Fields() []ent.Field {
field.Float("amount").
Positive(),
field.Enum("currency").
Values("USD", "EUR", "ILS"),
Values("USD", "EUR", "VND", "ILS"),
field.Time("time"),
field.String("description"),
field.Enum("status").