Skip to content
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

WIP: don't set created_at to now() by default for updates #125

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 15 additions & 7 deletions adapter/specs/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package specs

import (
"testing"
"time"

"github.com/go-rel/rel"
"github.com/go-rel/rel/where"
Expand Down Expand Up @@ -144,17 +145,19 @@ func Inserts(t *testing.T, repo rel.Repository) {

repo.MustInsert(ctx, &user)

now := time.Now()

tests := []interface{}{
&User{},
&User{Name: "insert", Age: 100},
&User{Name: "insert", Age: 100, Note: &note},
&User{Note: &note},
&User{ID: 123, Name: "insert", Age: 100, Note: &note},
&User{ID: 123, Name: "insert", Age: 100, Note: &note, CreatedAt: now},
&Address{},
&Address{Name: "work"},
&Address{UserID: &user.ID},
&Address{Name: "work", UserID: &user.ID},
&Address{ID: 123, Name: "work", UserID: &user.ID},
&Address{ID: 123, Name: "work", UserID: &user.ID, CreatedAt: now},
&Composite{Primary1: 1, Primary2: 2, Data: "data-1-2"},
}

Expand Down Expand Up @@ -192,19 +195,21 @@ func InsertAll(t *testing.T, repo rel.Repository) {

repo.MustInsert(ctx, &user)

now := time.Now()

tests := []interface{}{
&[]User{{}},
&[]User{{Name: "insert", Age: 100}},
&[]User{{Name: "insert", Age: 100, Note: &note}},
&[]User{{Note: &note}},
&[]User{{Name: "insert", Age: 100}, {Name: "insert too"}},
&[]User{{ID: 224, Name: "insert", Age: 100}, {ID: 234, Name: "insert too"}},
&[]User{{ID: 224, Name: "insert", Age: 100, CreatedAt: now}, {ID: 234, Name: "insert too", CreatedAt: now}},
&[]Address{{}},
&[]Address{{Name: "work"}},
&[]Address{{UserID: &user.ID}},
&[]Address{{Name: "work", UserID: &user.ID}},
&[]Address{{Name: "work"}, {Name: "home"}},
&[]Address{{ID: 233, Name: "work"}, {ID: 235, Name: "home"}},
&[]Address{{ID: 233, Name: "work", CreatedAt: now}, {ID: 235, Name: "home", CreatedAt: now}},
}

for _, record := range tests {
Expand All @@ -217,10 +222,13 @@ func InsertAll(t *testing.T, repo rel.Repository) {

// InsertAllPartialCustomPrimary tests insert multiple specifications.
func InsertAllPartialCustomPrimary(t *testing.T, repo rel.Repository) {

now := time.Now()

tests := []interface{}{
&[]User{{ID: 300, Name: "insert 300", Age: 100}, {Name: "insert 300+?"}},
&[]User{{Name: "insert 305-?", Age: 100}, {ID: 305, Name: "insert 305+?"}},
&[]User{{Name: "insert 310-?"}, {ID: 310, Name: "insert 310", Age: 100}, {Name: "insert 300+?"}},
&[]User{{ID: 300, Name: "insert 300", Age: 100, CreatedAt: now}, {Name: "insert 300+?"}},
&[]User{{Name: "insert 305-?", Age: 100}, {ID: 305, Name: "insert 305+?", CreatedAt: now}},
&[]User{{Name: "insert 310-?"}, {ID: 310, Name: "insert 310", Age: 100, CreatedAt: now}, {Name: "insert 300+?"}},
}

for _, record := range tests {
Expand Down
6 changes: 0 additions & 6 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,6 @@ func TestRepository_Update_saveBelongsTo(t *testing.T) {
ID: 1,
Name: "name",
UpdatedAt: now(),
CreatedAt: now(),
},
}, profile)

Expand Down Expand Up @@ -1345,7 +1344,6 @@ func TestRepository_Update_saveHasOne(t *testing.T) {
assert.Equal(t, User{
ID: userID,
UpdatedAt: now(),
CreatedAt: now(),
Address: Address{
ID: 1,
Street: "street",
Expand Down Expand Up @@ -1377,7 +1375,6 @@ func TestRepository_Update_saveHasOneCascadeDisabled(t *testing.T) {
assert.Equal(t, User{
ID: userID,
UpdatedAt: now(),
CreatedAt: now(),
Address: Address{
ID: 1,
Street: "street",
Expand Down Expand Up @@ -1434,7 +1431,6 @@ func TestRepository_Update_saveHasMany(t *testing.T) {
assert.Nil(t, repo.Update(context.TODO(), &user))
assert.Equal(t, User{
ID: 10,
CreatedAt: now(),
UpdatedAt: now(),
UserRoles: []UserRole{
{UserID: 10, RoleID: 2},
Expand All @@ -1461,7 +1457,6 @@ func TestRepository_Update_saveHasManyCascadeDisabled(t *testing.T) {
assert.Nil(t, repo.Update(context.TODO(), &user, Cascade(false)))
assert.Equal(t, User{
ID: 10,
CreatedAt: now(),
UpdatedAt: now(),
UserRoles: []UserRole{
{RoleID: 2},
Expand Down Expand Up @@ -2241,7 +2236,6 @@ func TestRepository_saveHasMany_replace(t *testing.T) {
assert.Nil(t, repo.(*repository).saveHasMany(cw, doc, &mutation, false))
assert.Equal(t, User{
ID: 1,
CreatedAt: now(),
UpdatedAt: now(),
Emails: []Email{
{ID: 3, UserID: 1, Email: "email3@gmail.com"},
Expand Down
2 changes: 1 addition & 1 deletion structset.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (s Structset) Apply(doc *Document, mut *Mutation) {
switch field {
case "created_at", "inserted_at":
if doc.Flag(HasCreatedAt) {
if value, ok := doc.Value(field); ok && value.(time.Time).IsZero() {
if value, ok := doc.Value(field); ok && value.(time.Time).IsZero() && isZero(doc.PrimaryValue()) {
s.set(doc, mut, field, t, true)
continue
}
Expand Down
3 changes: 1 addition & 2 deletions structset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestStructset(t *testing.T) {
"id": Set("id", 1),
"name": Set("name", "Luffy"),
"age": Set("age", 0),
"created_at": Set("created_at", now()),
"created_at": Set("created_at", time.Time{}),
"updated_at": Set("updated_at", now()),
},
}
Expand Down Expand Up @@ -85,7 +85,6 @@ func TestStructset_skipZero(t *testing.T) {
Mutates: map[string]Mutate{
"id": Set("id", 1),
"name": Set("name", "Luffy"),
"created_at": Set("created_at", now()),
"updated_at": Set("updated_at", now()),
},
}
Expand Down