Skip to content

Commit

Permalink
fix #1
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Jul 6, 2023
1 parent 5678bfc commit 6f2375b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
5 changes: 5 additions & 0 deletions db_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ func (db *DB) CheckSchema(ctx context.Context) error {
return fmt.Errorf("column %q in table %q not found in schema", col.Name, tableName)
}

if column.Unique { // modify it, so checks are correct.
column.UniqueIndex = fmt.Sprintf("%s_%s_key", tableName, column.Name)
column.Unique = false
}

if expected, got := col.FieldTagString(false), column.FieldTagString(false); expected != got {
return fmt.Errorf("column %q in table %q has wrong field tag: db:\n%s\nvs code:\n%s", col.Name, tableName, expected, got)
}
Expand Down
8 changes: 5 additions & 3 deletions desc/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ func (c *Column) FieldTagString(strict bool) string {

writeTagProp(b, ",unique", c.Unique)
writeTagProp(b, ",conflict=%s", c.Conflict)
writeTagProp(b, ",username", c.Username)
writeTagProp(b, ",password", c.Password)
if strict {
writeTagProp(b, ",username", c.Username)
writeTagProp(b, ",password", c.Password)
}

if tb := c.ReferenceTableName; tb != "" {
// write the ref line.
Expand All @@ -169,8 +171,8 @@ func (c *Column) FieldTagString(strict bool) string {

writeTagProp(b, ",unique_index=%s", c.UniqueIndex)
writeTagProp(b, ",check=%s", c.CheckConstraint)
writeTagProp(b, ",auto", c.AutoGenerated)
if strict {
writeTagProp(b, ",auto", c.AutoGenerated)
writeTagProp(b, ",presenter", c.Presenter)
writeTagProp(b, ",unscannable", c.Unscannable)
}
Expand Down
1 change: 1 addition & 0 deletions desc/struct_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func convertStructFieldToColumnDefinion(tableName string, field reflect.StructFi

if c.PrimaryKey && !c.Nullable && c.Type == UUID && c.Default == "" {
c.Default = genRandomUUIDPGCryptoFunction1
// c.AutoGenerated = true
}

if c.Password && c.Type == InvalidDataType {
Expand Down
22 changes: 20 additions & 2 deletions desc/update_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,20 @@ func BuildUpdateQuery(value any, columnsToUpdate []string, primaryKey *Column) (
return "", nil, err
}

shouldUpdateID := false
for _, col := range columnsToUpdate {
if col == primaryKey.Name {
shouldUpdateID = true
break
}
}

if len(args) == 1 { // the last one is the id.
return "", nil, fmt.Errorf("no arguments found for update, maybe missing struct field tag of \"%s\"", DefaultTag)
}

// build the SQL query using the table definition and its primary key.
query := buildUpdateQuery(primaryKey.Table, args, primaryKey.Name)
query := buildUpdateQuery(primaryKey.Table, args, primaryKey.Name, shouldUpdateID)
return query, args.Values(), nil
}

Expand Down Expand Up @@ -61,7 +73,7 @@ func extractUpdateArguments(value any, columnsToUpdate []string, primaryKey *Col
return args, nil
}

func buildUpdateQuery(td *Table, args Arguments, primaryKeyName string) string {
func buildUpdateQuery(td *Table, args Arguments, primaryKeyName string, shouldUpdateID bool) string {
var b strings.Builder

b.WriteString(`UPDATE "` + td.Name + `" SET `)
Expand All @@ -71,6 +83,12 @@ func buildUpdateQuery(td *Table, args Arguments, primaryKeyName string) string {
for i, a := range args {
c := a.Column

if !shouldUpdateID && c.Name == primaryKeyName {
// Do not update ID if not specifically asked to.
// Fixes #1.
continue
}

if i > 0 {
b.WriteByte(',')
}
Expand Down

0 comments on commit 6f2375b

Please sign in to comment.