diff --git a/openmeter/ent/db/migrate/schema.go b/openmeter/ent/db/migrate/schema.go index 2c0f8d559..5edc74e1a 100644 --- a/openmeter/ent/db/migrate/schema.go +++ b/openmeter/ent/db/migrate/schema.go @@ -20,7 +20,7 @@ var ( {Name: "balance", Type: field.TypeFloat64, SchemaType: map[string]string{"postgres": "numeric"}}, {Name: "overage", Type: field.TypeFloat64, SchemaType: map[string]string{"postgres": "numeric"}}, {Name: "at", Type: field.TypeTime}, - {Name: "owner_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "owner_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // BalanceSnapshotsTable holds the schema information for the "balance_snapshots" table. BalanceSnapshotsTable = &schema.Table{ @@ -60,7 +60,7 @@ var ( } // BillingInvoicesColumns holds the columns for the "billing_invoices" table. BillingInvoicesColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, @@ -77,8 +77,8 @@ var ( {Name: "provider_reference", Type: field.TypeString, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "period_start", Type: field.TypeTime}, {Name: "period_end", Type: field.TypeTime}, - {Name: "billing_profile_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, - {Name: "workflow_config_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "billing_profile_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, + {Name: "workflow_config_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // BillingInvoicesTable holds the schema information for the "billing_invoices" table. BillingInvoicesTable = &schema.Table{ @@ -139,7 +139,7 @@ var ( } // BillingInvoiceItemsColumns holds the columns for the "billing_invoice_items" table. BillingInvoiceItemsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, @@ -153,7 +153,7 @@ var ( {Name: "unit_price", Type: field.TypeOther, SchemaType: map[string]string{"postgres": "numeric"}}, {Name: "currency", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(3)"}}, {Name: "tax_code_override", Type: field.TypeJSON, SchemaType: map[string]string{"postgres": "jsonb"}}, - {Name: "invoice_id", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "invoice_id", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // BillingInvoiceItemsTable holds the schema information for the "billing_invoice_items" table. BillingInvoiceItemsTable = &schema.Table{ @@ -198,7 +198,7 @@ var ( } // BillingProfilesColumns holds the columns for the "billing_profiles" table. BillingProfilesColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, @@ -206,7 +206,7 @@ var ( {Name: "key", Type: field.TypeString}, {Name: "provider_config", Type: field.TypeString, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "default", Type: field.TypeBool, Default: false}, - {Name: "workflow_config_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "workflow_config_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // BillingProfilesTable holds the schema information for the "billing_profiles" table. BillingProfilesTable = &schema.Table{ @@ -251,7 +251,7 @@ var ( } // BillingWorkflowConfigsColumns holds the columns for the "billing_workflow_configs" table. BillingWorkflowConfigsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, @@ -290,7 +290,7 @@ var ( } // CustomersColumns holds the columns for the "customers" table. CustomersColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "key", Type: field.TypeString}, {Name: "namespace", Type: field.TypeString}, {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, @@ -346,7 +346,7 @@ var ( {Name: "id", Type: field.TypeInt, Increment: true}, {Name: "subject_key", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, - {Name: "customer_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "customer_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // CustomerSubjectsTable holds the schema information for the "customer_subjects" table. CustomerSubjectsTable = &schema.Table{ @@ -371,7 +371,7 @@ var ( } // EntitlementsColumns holds the columns for the "entitlements" table. EntitlementsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, @@ -390,7 +390,7 @@ var ( {Name: "usage_period_anchor", Type: field.TypeTime, Nullable: true}, {Name: "current_usage_period_start", Type: field.TypeTime, Nullable: true}, {Name: "current_usage_period_end", Type: field.TypeTime, Nullable: true}, - {Name: "feature_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "feature_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // EntitlementsTable holds the schema information for the "entitlements" table. EntitlementsTable = &schema.Table{ @@ -450,7 +450,7 @@ var ( } // FeaturesColumns holds the columns for the "features" table. FeaturesColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, @@ -482,7 +482,7 @@ var ( } // GrantsColumns holds the columns for the "grants" table. GrantsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "metadata", Type: field.TypeJSON, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "created_at", Type: field.TypeTime}, @@ -498,7 +498,7 @@ var ( {Name: "reset_min_rollover", Type: field.TypeFloat64, SchemaType: map[string]string{"postgres": "numeric"}}, {Name: "recurrence_period", Type: field.TypeEnum, Nullable: true, Enums: []string{"DAY", "WEEK", "MONTH", "YEAR"}}, {Name: "recurrence_anchor", Type: field.TypeTime, Nullable: true}, - {Name: "owner_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "owner_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // GrantsTable holds the schema information for the "grants" table. GrantsTable = &schema.Table{ @@ -538,7 +538,7 @@ var ( } // NotificationChannelsColumns holds the columns for the "notification_channels" table. NotificationChannelsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, @@ -578,13 +578,13 @@ var ( } // NotificationEventsColumns holds the columns for the "notification_events" table. NotificationEventsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "type", Type: field.TypeEnum, Enums: []string{"entitlements.balance.threshold"}}, {Name: "payload", Type: field.TypeString, SchemaType: map[string]string{"postgres": "jsonb"}}, {Name: "annotations", Type: field.TypeString, Nullable: true, SchemaType: map[string]string{"postgres": "jsonb"}}, - {Name: "rule_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "rule_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // NotificationEventsTable holds the schema information for the "notification_events" table. NotificationEventsTable = &schema.Table{ @@ -634,7 +634,7 @@ var ( } // NotificationEventDeliveryStatusColumns holds the columns for the "notification_event_delivery_status" table. NotificationEventDeliveryStatusColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, @@ -678,7 +678,7 @@ var ( } // NotificationRulesColumns holds the columns for the "notification_rules" table. NotificationRulesColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, @@ -718,13 +718,13 @@ var ( } // UsageResetsColumns holds the columns for the "usage_resets" table. UsageResetsColumns = []*schema.Column{ - {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "id", Type: field.TypeString, Unique: true, SchemaType: map[string]string{"postgres": "varchar(34)"}}, {Name: "namespace", Type: field.TypeString}, {Name: "created_at", Type: field.TypeTime}, {Name: "updated_at", Type: field.TypeTime}, {Name: "deleted_at", Type: field.TypeTime, Nullable: true}, {Name: "reset_time", Type: field.TypeTime}, - {Name: "entitlement_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "entitlement_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // UsageResetsTable holds the schema information for the "usage_resets" table. UsageResetsTable = &schema.Table{ @@ -764,8 +764,8 @@ var ( } // NotificationChannelRulesColumns holds the columns for the "notification_channel_rules" table. NotificationChannelRulesColumns = []*schema.Column{ - {Name: "notification_channel_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, - {Name: "notification_rule_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "notification_channel_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, + {Name: "notification_rule_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // NotificationChannelRulesTable holds the schema information for the "notification_channel_rules" table. NotificationChannelRulesTable = &schema.Table{ @@ -789,8 +789,8 @@ var ( } // NotificationEventDeliveryStatusEventsColumns holds the columns for the "notification_event_delivery_status_events" table. NotificationEventDeliveryStatusEventsColumns = []*schema.Column{ - {Name: "notification_event_delivery_status_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, - {Name: "notification_event_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "char(26)"}}, + {Name: "notification_event_delivery_status_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, + {Name: "notification_event_id", Type: field.TypeString, SchemaType: map[string]string{"postgres": "varchar(34)"}}, } // NotificationEventDeliveryStatusEventsTable holds the schema information for the "notification_event_delivery_status_events" table. NotificationEventDeliveryStatusEventsTable = &schema.Table{ diff --git a/openmeter/ent/schema/billing.go b/openmeter/ent/schema/billing.go index e9ec0c0d0..55aecf33a 100644 --- a/openmeter/ent/schema/billing.go +++ b/openmeter/ent/schema/billing.go @@ -25,7 +25,7 @@ type BillingProfile struct { func (BillingProfile) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixBillingProfile)), entutils.NamespaceMixin{}, entutils.TimeMixin{}, } @@ -138,7 +138,7 @@ type BillingWorkflowConfig struct { func (BillingWorkflowConfig) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixWorkflowConfig)), entutils.NamespaceMixin{}, entutils.TimeMixin{}, } @@ -190,7 +190,7 @@ type BillingInvoiceItem struct { func (BillingInvoiceItem) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixBillingInvoiceItem)), entutils.NamespaceMixin{}, entutils.TimeMixin{}, entutils.MetadataAnnotationsMixin{}, @@ -263,7 +263,7 @@ type BillingInvoice struct { func (BillingInvoice) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixBillingInvoice)), entutils.NamespaceMixin{}, entutils.TimeMixin{}, entutils.MetadataAnnotationsMixin{}, diff --git a/openmeter/ent/schema/customer.go b/openmeter/ent/schema/customer.go index 2b6ac9456..809ec63bb 100644 --- a/openmeter/ent/schema/customer.go +++ b/openmeter/ent/schema/customer.go @@ -20,7 +20,7 @@ type Customer struct { func (Customer) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.ResourceMixin{}, + entutils.Must(entutils.ResourceMixin(IDPrefixCustomer)), entutils.CustomerAddressMixin{ FieldPrefix: "billing", }, diff --git a/openmeter/ent/schema/entitlement.go b/openmeter/ent/schema/entitlement.go index 87b7d9dbe..c5de7b06d 100644 --- a/openmeter/ent/schema/entitlement.go +++ b/openmeter/ent/schema/entitlement.go @@ -18,7 +18,7 @@ type Entitlement struct { func (Entitlement) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixEntitlement)), entutils.NamespaceMixin{}, entutils.MetadataAnnotationsMixin{}, entutils.TimeMixin{}, diff --git a/openmeter/ent/schema/feature.go b/openmeter/ent/schema/feature.go index aa1af92a3..592ef6242 100644 --- a/openmeter/ent/schema/feature.go +++ b/openmeter/ent/schema/feature.go @@ -15,7 +15,7 @@ type Feature struct { func (Feature) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixFeature)), entutils.TimeMixin{}, entutils.MetadataAnnotationsMixin{}, } diff --git a/openmeter/ent/schema/grant.go b/openmeter/ent/schema/grant.go index a89fb323b..2ac1bb2a2 100644 --- a/openmeter/ent/schema/grant.go +++ b/openmeter/ent/schema/grant.go @@ -18,7 +18,7 @@ type Grant struct { func (Grant) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixGrant)), entutils.NamespaceMixin{}, entutils.MetadataAnnotationsMixin{}, entutils.TimeMixin{}, diff --git a/openmeter/ent/schema/idprefix.go b/openmeter/ent/schema/idprefix.go new file mode 100644 index 000000000..a75bffb2d --- /dev/null +++ b/openmeter/ent/schema/idprefix.go @@ -0,0 +1,34 @@ +package schema + +// Table of ID prefixes for each entity type. +// Please make sure you are picking a unique prefix for each entity type. +// Recommendation: +// - at least 3 characters of the entity type name. +// - max length is 8 characters (field size limit) + +const ( + // Billing + IDPrefixBillingProfile = "bi_p_" + IDPrefixBillingInvoice = "bi_i_" + IDPrefixBillingInvoiceItem = "bi_ii_" + IDPrefixWorkflowConfig = "bi_wc_" + + // Customer + IDPrefixCustomer = "cus_" + + // Entitlements + IDPrefixEntitlement = "en_" + IDPrefixEntitlementUsageReset = "en_ur_" + + // Credits + IDPrefixGrant = "cr_g_" + + // Notifications + IDPrefixNotificationChannel = "no_c_" + IDPrefixNotificationRule = "no_r_" + IDPrefixNotificationEvent = "no_e_" + IDPrefixNotificationEventDeliveryStatus = "no_ed_" + + // Product catalog + IDPrefixFeature = "pr_f_" +) diff --git a/openmeter/ent/schema/notification.go b/openmeter/ent/schema/notification.go index 0dba5a4d3..c80378a06 100644 --- a/openmeter/ent/schema/notification.go +++ b/openmeter/ent/schema/notification.go @@ -24,7 +24,7 @@ type NotificationChannel struct { func (NotificationChannel) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixNotificationChannel)), entutils.NamespaceMixin{}, entutils.TimeMixin{}, } @@ -68,7 +68,7 @@ type NotificationRule struct { func (NotificationRule) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixNotificationRule)), entutils.NamespaceMixin{}, entutils.TimeMixin{}, } @@ -117,7 +117,7 @@ type NotificationEvent struct { func (NotificationEvent) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixNotificationEvent)), entutils.NamespaceMixin{}, } } @@ -182,7 +182,7 @@ type NotificationEventDeliveryStatus struct { func (NotificationEventDeliveryStatus) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixNotificationEventDeliveryStatus)), entutils.NamespaceMixin{}, } } diff --git a/openmeter/ent/schema/usage_reset.go b/openmeter/ent/schema/usage_reset.go index 6d0f2ccd6..93f26f92d 100644 --- a/openmeter/ent/schema/usage_reset.go +++ b/openmeter/ent/schema/usage_reset.go @@ -16,7 +16,7 @@ type UsageReset struct { func (UsageReset) Mixin() []ent.Mixin { return []ent.Mixin{ - entutils.IDMixin{}, + entutils.Must(entutils.IDMixin(IDPrefixEntitlementUsageReset)), entutils.NamespaceMixin{}, entutils.TimeMixin{}, } diff --git a/pkg/framework/entutils/mixins.go b/pkg/framework/entutils/mixins.go index e685f9d28..c4579de4b 100644 --- a/pkg/framework/entutils/mixins.go +++ b/pkg/framework/entutils/mixins.go @@ -14,14 +14,27 @@ import ( "github.com/openmeterio/openmeter/pkg/models" ) +func ResourceMixin(IDPrefix string) (ent.Mixin, error) { + idMixin, err := IDMixin(IDPrefix) + if err != nil { + return nil, err + } + + return resourceMixin{ + IDMixin: idMixin, + }, nil +} + // ResourceMixin adds common fields -type ResourceMixin struct { +type resourceMixin struct { mixin.Schema + + IDMixin ent.Mixin } -func (ResourceMixin) Fields() []ent.Field { +func (r resourceMixin) Fields() []ent.Field { var fields []ent.Field - fields = append(fields, IDMixin{}.Fields()...) + fields = append(fields, r.IDMixin.Fields()...) fields = append(fields, KeyMixin{}.Fields()...) fields = append(fields, NamespaceMixin{}.Fields()...) fields = append(fields, MetadataAnnotationsMixin{}.Fields()...) @@ -30,9 +43,9 @@ func (ResourceMixin) Fields() []ent.Field { return fields } -func (ResourceMixin) Indexes() []ent.Index { +func (r resourceMixin) Indexes() []ent.Index { var indexes []ent.Index - indexes = append(indexes, IDMixin{}.Indexes()...) + indexes = append(indexes, r.IDMixin.Indexes()...) indexes = append(indexes, NamespaceMixin{}.Indexes()...) indexes = append(indexes, MetadataAnnotationsMixin{}.Indexes()...) indexes = append(indexes, TimeMixin{}.Indexes()...) @@ -56,26 +69,40 @@ func (ResourceMixin) Indexes() []ent.Index { } // IDMixin adds the ID field to the schema -type IDMixin struct { +func IDMixin(IDPrefix string) (ent.Mixin, error) { + if IDPrefix == "" { + return nil, fmt.Errorf("IDPrefix is required") + } + + return idMixin{IDPrefix: IDPrefix}, nil +} + +type idMixin struct { mixin.Schema + + IDPrefix string +} + +func (i idMixin) ULIDWithPrefix() string { + return fmt.Sprintf("%s_%s", i.IDPrefix, ulid.Make().String()) } // Fields of the IDMixin. -func (IDMixin) Fields() []ent.Field { +func (i idMixin) Fields() []ent.Field { return []ent.Field{ field.String("id"). DefaultFunc(func() string { - return ulid.Make().String() + return i.ULIDWithPrefix() }). Unique(). Immutable(). SchemaType(map[string]string{ - dialect.Postgres: "char(26)", + dialect.Postgres: "varchar(34)", }), } } -func (IDMixin) Indexes() []ent.Index { +func (idMixin) Indexes() []ent.Index { return []ent.Index{ index.Fields("id").Unique(), } @@ -169,3 +196,11 @@ func (c CustomerAddressMixin) Fields() []ent.Field { field.String(fmt.Sprintf("%s_address_phone_number", c.FieldPrefix)).Optional().Nillable(), } } + +func Must(m ent.Mixin, err error) ent.Mixin { + if err != nil { + panic(err) + } + + return m +} diff --git a/tools/migrate/migrations/20240920084000_ulid-prefixes.down.sql b/tools/migrate/migrations/20240920084000_ulid-prefixes.down.sql new file mode 100644 index 000000000..9daab8c7e --- /dev/null +++ b/tools/migrate/migrations/20240920084000_ulid-prefixes.down.sql @@ -0,0 +1,34 @@ +-- reverse: modify "usage_resets" table +ALTER TABLE "usage_resets" ALTER COLUMN "entitlement_id" TYPE character(26), ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "notification_rules" table +ALTER TABLE "notification_rules" ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "notification_events" table +ALTER TABLE "notification_events" ALTER COLUMN "rule_id" TYPE character(26), ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "notification_event_delivery_status_events" table +ALTER TABLE "notification_event_delivery_status_events" ALTER COLUMN "notification_event_id" TYPE character(26), ALTER COLUMN "notification_event_delivery_status_id" TYPE character(26); +-- reverse: modify "notification_event_delivery_status" table +ALTER TABLE "notification_event_delivery_status" ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "notification_channels" table +ALTER TABLE "notification_channels" ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "notification_channel_rules" table +ALTER TABLE "notification_channel_rules" ALTER COLUMN "notification_rule_id" TYPE character(26), ALTER COLUMN "notification_channel_id" TYPE character(26); +-- reverse: modify "grants" table +ALTER TABLE "grants" ALTER COLUMN "owner_id" TYPE character(26), ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "features" table +ALTER TABLE "features" ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "entitlements" table +ALTER TABLE "entitlements" ALTER COLUMN "feature_id" TYPE character(26), ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "customers" table +ALTER TABLE "customers" ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "customer_subjects" table +ALTER TABLE "customer_subjects" ALTER COLUMN "customer_id" TYPE character(26); +-- reverse: modify "billing_workflow_configs" table +ALTER TABLE "billing_workflow_configs" ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "billing_profiles" table +ALTER TABLE "billing_profiles" ALTER COLUMN "workflow_config_id" TYPE character(26), ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "billing_invoices" table +ALTER TABLE "billing_invoices" ALTER COLUMN "workflow_config_id" TYPE character(26), ALTER COLUMN "billing_profile_id" TYPE character(26), ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "billing_invoice_items" table +ALTER TABLE "billing_invoice_items" ALTER COLUMN "invoice_id" TYPE character(26), ALTER COLUMN "id" TYPE character(26); +-- reverse: modify "balance_snapshots" table +ALTER TABLE "balance_snapshots" ALTER COLUMN "owner_id" TYPE character(26); diff --git a/tools/migrate/migrations/20240920084000_ulid-prefixes.up.sql b/tools/migrate/migrations/20240920084000_ulid-prefixes.up.sql new file mode 100644 index 000000000..b261e5ecc --- /dev/null +++ b/tools/migrate/migrations/20240920084000_ulid-prefixes.up.sql @@ -0,0 +1,34 @@ +-- modify "balance_snapshots" table +ALTER TABLE "balance_snapshots" ALTER COLUMN "owner_id" TYPE character varying(34); +-- modify "billing_invoice_items" table +ALTER TABLE "billing_invoice_items" ALTER COLUMN "id" TYPE character varying(34), ALTER COLUMN "invoice_id" TYPE character varying(34); +-- modify "billing_invoices" table +ALTER TABLE "billing_invoices" ALTER COLUMN "id" TYPE character varying(34), ALTER COLUMN "billing_profile_id" TYPE character varying(34), ALTER COLUMN "workflow_config_id" TYPE character varying(34); +-- modify "billing_profiles" table +ALTER TABLE "billing_profiles" ALTER COLUMN "id" TYPE character varying(34), ALTER COLUMN "workflow_config_id" TYPE character varying(34); +-- modify "billing_workflow_configs" table +ALTER TABLE "billing_workflow_configs" ALTER COLUMN "id" TYPE character varying(34); +-- modify "customer_subjects" table +ALTER TABLE "customer_subjects" ALTER COLUMN "customer_id" TYPE character varying(34); +-- modify "customers" table +ALTER TABLE "customers" ALTER COLUMN "id" TYPE character varying(34); +-- modify "entitlements" table +ALTER TABLE "entitlements" ALTER COLUMN "id" TYPE character varying(34), ALTER COLUMN "feature_id" TYPE character varying(34); +-- modify "features" table +ALTER TABLE "features" ALTER COLUMN "id" TYPE character varying(34); +-- modify "grants" table +ALTER TABLE "grants" ALTER COLUMN "id" TYPE character varying(34), ALTER COLUMN "owner_id" TYPE character varying(34); +-- modify "notification_channel_rules" table +ALTER TABLE "notification_channel_rules" ALTER COLUMN "notification_channel_id" TYPE character varying(34), ALTER COLUMN "notification_rule_id" TYPE character varying(34); +-- modify "notification_channels" table +ALTER TABLE "notification_channels" ALTER COLUMN "id" TYPE character varying(34); +-- modify "notification_event_delivery_status" table +ALTER TABLE "notification_event_delivery_status" ALTER COLUMN "id" TYPE character varying(34); +-- modify "notification_event_delivery_status_events" table +ALTER TABLE "notification_event_delivery_status_events" ALTER COLUMN "notification_event_delivery_status_id" TYPE character varying(34), ALTER COLUMN "notification_event_id" TYPE character varying(34); +-- modify "notification_events" table +ALTER TABLE "notification_events" ALTER COLUMN "id" TYPE character varying(34), ALTER COLUMN "rule_id" TYPE character varying(34); +-- modify "notification_rules" table +ALTER TABLE "notification_rules" ALTER COLUMN "id" TYPE character varying(34); +-- modify "usage_resets" table +ALTER TABLE "usage_resets" ALTER COLUMN "id" TYPE character varying(34), ALTER COLUMN "entitlement_id" TYPE character varying(34); diff --git a/tools/migrate/migrations/atlas.sum b/tools/migrate/migrations/atlas.sum index 4d41e7b7d..d874bc6cf 100644 --- a/tools/migrate/migrations/atlas.sum +++ b/tools/migrate/migrations/atlas.sum @@ -1,4 +1,4 @@ -h1:xsZpOJs6jnPPXtbUTxid8GPAZlJGCfc2ub2ZxVr457M= +h1:Ujytk4nrhHygRydYCS8v+2QUpuUoDQqf40LreTCD0fc= 20240826120919_init.down.sql h1:AIbgwwngjkJEYa3yRZsIXQyBa2+qoZttwMXHxXEbHLI= 20240826120919_init.up.sql h1:/hYHWF3Z3dab8SMKnw99ixVktCuJe2bAw5wstCZIEN8= 20240903155435_entitlement-expired-index.down.sql h1:np2xgYs3KQ2z7qPBcobtGNhqWQ3V8NwEP9E5U3TmpSA= @@ -11,3 +11,5 @@ h1:xsZpOJs6jnPPXtbUTxid8GPAZlJGCfc2ub2ZxVr457M= 20240919144910_customer-timezone.up.sql h1:l/L/85zsacSORnsgUdBSOjzQvf0L91byK2wh4Dz6eCI= 20240920070940_indexing-fixes.down.sql h1:IhKm+adPcfd7XOWYmFurnMpWCSSkWTosCs/aYstpvgc= 20240920070940_indexing-fixes.up.sql h1:ne1XfTKACTk02BAJhxLWb/xFLLI/UWYWEyn17nDHaKQ= +20240920084000_ulid-prefixes.down.sql h1:cf6ncEx+coqrHkAXMUVCY7oK7fBsoRo2mq5IzSIMP/M= +20240920084000_ulid-prefixes.up.sql h1:okYpy/WqDghnrrSpWtpcRHHhSqRw5MzqlbevnI60tGM=