-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate table indexes #220
Labels
Comments
Hi @amanbolat, |
Hi @go-jet. Let's say I generated a code for the table type usersTable struct {
postgres.Table
//Columns
ID postgres.ColumnString
Name postgres.ColumnString
AllColumns postgres.ColumnList
MutableColumns postgres.ColumnList
//Indexes
UserNameKeyIndex postgres.Index
} Assume that we have type Index interface {
Name() string
} At last, I have a function that will create a user in DB: func (s *Store) CreateUser(ctx context.Context, user User) error {
// skipped
// Check for duplicate
stmt := table.User.INSERT(table.User.AllColumns).MODEL(user)
res, err := stmt.ExecContext(ctx, s.dbConn)
// pkgpostgres.DuplicateErrorCode = 23505
if IsErrorCode(err, pkgpostgres.DuplicateErrorCode, table.User.UserNameKeyIndex.Name()) {
return ErrDuplicateUserName
} else if err != nil {
return err
}
// skipped
} Code for func IsErrorCode(err error, code string, constraintName *string) bool {
if err == nil {
return false
}
if code == "" {
return false
}
var pgErr *pgconn.PgError
if !errors.As(err, &pgErr) {
return false
}
if pgErr.Code != code {
return false
}
if constraintName != nil && *constraintName != pgErr.ConstraintName {
return false
}
return true
} |
Aha, I see. It makes sense. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is your feature request related to a problem? Please describe.
I have a few tables with unique indexes and I use a function to check the error against its error code and constraint name, which is the index name.
Describe the solution you'd like
Would be nice to have indexes generated for every table.
The text was updated successfully, but these errors were encountered: