Skip to content

Commit 5ce7f0a

Browse files
authored
Merge pull request #1080 from gotify/dont-recreate-deleted-entities
fix: don't recreate deleted entities
2 parents dd06dc7 + b1512f5 commit 5ce7f0a

9 files changed

Lines changed: 66 additions & 5 deletions

File tree

‎database/application.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func (d *GormDatabase) GetApplicationsByUser(userID uint) ([]*model.Application,
7272

7373
// UpdateApplication updates an application.
7474
func (d *GormDatabase) UpdateApplication(app *model.Application) error {
75-
return d.DB.Save(app).Error
75+
return d.updateExisting(app)
7676
}
7777

7878
// UpdateApplicationTokenLastUsed updates the last used time of the application token.

‎database/application_test.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/gotify/server/v3/model"
77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
func (s *DatabaseSuite) TestApplication() {
@@ -99,3 +100,16 @@ func (s *DatabaseSuite) TestDeleteAppDeletesMessages() {
99100
assert.NotEmpty(s.T(), msgs)
100101
}
101102
}
103+
104+
func (s *DatabaseSuite) TestUpdateDeletedApplicationDoesNotRecreateIt() {
105+
app := &model.Application{UserID: 1, Token: "ASTALE", Name: "backup"}
106+
require.NoError(s.T(), s.db.CreateApplication(app))
107+
require.NoError(s.T(), s.db.DeleteApplicationByID(app.ID))
108+
109+
app.Name = "renamed"
110+
assert.NoError(s.T(), s.db.UpdateApplication(app))
111+
112+
if a, err := s.db.GetApplicationByID(app.ID); assert.NoError(s.T(), err) {
113+
assert.Nil(s.T(), a)
114+
}
115+
}

‎database/client.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func (d *GormDatabase) DeleteClientByID(id uint) error {
6060
// UpdateClient updates a client.
6161
func (d *GormDatabase) UpdateClient(client *model.Client) error {
6262
client.PopulateExpiresAt()
63-
return d.DB.Save(client).Error
63+
return d.updateExisting(client)
6464
}
6565

6666
// UpdateClientTokensLastUsed updates the last used timestamp of clients and
@@ -74,7 +74,7 @@ func (d *GormDatabase) UpdateClientTokensLastUsedAndExpiresAt(tokens []string, t
7474
for _, c := range clients {
7575
c.LastUsed = t
7676
c.PopulateExpiresAt()
77-
if err := tx.Save(c).Error; err != nil {
77+
if err := tx.Model(c).Select("last_used", "expires_at").Updates(c).Error; err != nil {
7878
return err
7979
}
8080
}

‎database/client_test.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/gotify/server/v3/model"
77
"github.com/stretchr/testify/assert"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
func (s *DatabaseSuite) TestClient() {
@@ -194,3 +195,19 @@ func (s *DatabaseSuite) TestClientSelectHidesExpired() {
194195
assert.Equal(s.T(), valid.Token, cs[0].Token)
195196
}
196197
}
198+
199+
func (s *DatabaseSuite) TestUpdateDeletedClientDoesNotRecreateIt() {
200+
user := &model.User{Name: "stale", Pass: []byte{1}}
201+
require.NoError(s.T(), s.db.CreateUser(user))
202+
203+
client := &model.Client{UserID: user.ID, Token: "CSTALE", Name: "android"}
204+
require.NoError(s.T(), s.db.CreateClient(client))
205+
require.NoError(s.T(), s.db.DeleteClientByID(client.ID))
206+
207+
client.Name = "renamed"
208+
assert.NoError(s.T(), s.db.UpdateClient(client))
209+
210+
if c, err := s.db.GetClientByID(client.ID); assert.NoError(s.T(), err) {
211+
assert.Nil(s.T(), c)
212+
}
213+
}

‎database/database.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ func New(dialect, connection, defaultUser, defaultPass string, strength int, cre
112112
return &GormDatabase{DB: db}, nil
113113
}
114114

115+
func (d *GormDatabase) updateExisting(value any) error {
116+
return d.DB.Select("*").Updates(value).Error
117+
}
118+
115119
func fillMissingCreatedAt(db *gorm.DB, now time.Time) error {
116120
models := []any{
117121
new(model.User),

‎database/plugin.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (d *GormDatabase) GetPluginConfByID(id uint) (*model.PluginConf, error) {
7474

7575
// UpdatePluginConf updates plugin configuration.
7676
func (d *GormDatabase) UpdatePluginConf(p *model.PluginConf) error {
77-
return d.DB.Save(p).Error
77+
return d.updateExisting(p)
7878
}
7979

8080
// DeletePluginConfByID deletes a plugin configuration by its id.

‎database/plugin_test.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,16 @@ func (s *DatabaseSuite) TestPluginConf() {
6868
assert.Equal(s.T(), false, pluginConf.Enabled)
6969
assert.Equal(s.T(), testConf, string(pluginConf.Config))
7070
}
71+
72+
func (s *DatabaseSuite) TestUpdateDeletedPluginConfDoesNotRecreateIt() {
73+
conf := &model.PluginConf{UserID: 1, ModulePath: "github.com/gotify/stale", Token: "PSTALE"}
74+
require.NoError(s.T(), s.db.CreatePluginConf(conf))
75+
require.NoError(s.T(), s.db.DeletePluginConfByID(conf.ID))
76+
77+
conf.Enabled = true
78+
assert.NoError(s.T(), s.db.UpdatePluginConf(conf))
79+
80+
if c, err := s.db.GetPluginConfByToken(conf.Token); assert.NoError(s.T(), err) {
81+
assert.Nil(s.T(), c)
82+
}
83+
}

‎database/user.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (d *GormDatabase) DeleteUserByID(id uint) error {
8383

8484
// UpdateUser updates a user.
8585
func (d *GormDatabase) UpdateUser(user *model.User) error {
86-
return d.DB.Save(user).Error
86+
return d.updateExisting(user)
8787
}
8888

8989
// CreateUser creates a user.

‎database/user_test.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,16 @@ func (s *DatabaseSuite) TestDeleteUserDeletesApplicationsAndClientsAndPluginConf
180180
require.NoError(s.T(), err)
181181
assert.NotNil(s.T(), msg)
182182
}
183+
184+
func (s *DatabaseSuite) TestUpdateDeletedUserDoesNotRecreateIt() {
185+
user := &model.User{Name: "stale", Pass: []byte{1}}
186+
require.NoError(s.T(), s.db.CreateUser(user))
187+
require.NoError(s.T(), s.db.DeleteUserByID(user.ID))
188+
189+
user.Admin = true
190+
assert.NoError(s.T(), s.db.UpdateUser(user))
191+
192+
if u, err := s.db.GetUserByID(user.ID); assert.NoError(s.T(), err) {
193+
assert.Nil(s.T(), u)
194+
}
195+
}

0 commit comments

Comments
 (0)