Fix impl and test for setting default retention policy

pull/1141/head
Paul Dix 2014-11-18 19:41:50 -05:00
parent 00e87488bd
commit 291360d42b
3 changed files with 32 additions and 19 deletions

View File

@ -466,6 +466,7 @@ func (db *Database) MarshalJSON() ([]byte, error) {
// Copy over properties to intermediate type.
var o databaseJSON
o.Name = db.name
o.DefaultRetentionPolicy = db.defaultRetentionPolicy
o.MaxFieldID = db.maxFieldID
for _, u := range db.users {
o.Users = append(o.Users, u)
@ -492,6 +493,7 @@ func (db *Database) UnmarshalJSON(data []byte) error {
// Copy over properties from intermediate type.
db.name = o.Name
db.defaultRetentionPolicy = o.DefaultRetentionPolicy
db.maxFieldID = o.MaxFieldID
// Copy users.
@ -523,12 +525,13 @@ func (db *Database) UnmarshalJSON(data []byte) error {
// databaseJSON represents the JSON-serialization format for a database.
type databaseJSON struct {
Name string `json:"name,omitempty"`
MaxFieldID uint64 `json:"maxFieldID,omitempty"`
Users []*DBUser `json:"users,omitempty"`
Policies []*RetentionPolicy `json:"policies,omitempty"`
Shards []*Shard `json:"shards,omitempty"`
Series []*Series `json:"series,omitempty"`
Name string `json:"name,omitempty"`
DefaultRetentionPolicy string `json:"defaultRetentionPolicy,omitempty"`
MaxFieldID uint64 `json:"maxFieldID,omitempty"`
Users []*DBUser `json:"users,omitempty"`
Policies []*RetentionPolicy `json:"policies,omitempty"`
Shards []*Shard `json:"shards,omitempty"`
Series []*Series `json:"series,omitempty"`
}
// databases represents a list of databases, sortable by name.

View File

@ -226,13 +226,13 @@ func TestDatabase_CreateRetentionPolicy(t *testing.T) {
}
// Create a retention policy on the database.
ss := &influxdb.RetentionPolicy{
rp := &influxdb.RetentionPolicy{
Name: "bar",
Duration: time.Hour,
ReplicaN: 2,
SplitN: 3,
}
if err := s.Database("foo").CreateRetentionPolicy(ss); err != nil {
if err := s.Database("foo").CreateRetentionPolicy(rp); err != nil {
t.Fatal(err)
}
s.Restart()
@ -240,7 +240,7 @@ func TestDatabase_CreateRetentionPolicy(t *testing.T) {
// Verify that the policy exists.
if o := s.Database("foo").RetentionPolicy("bar"); o == nil {
t.Fatalf("retention policy not found")
} else if !reflect.DeepEqual(ss, o) {
} else if !reflect.DeepEqual(rp, o) {
t.Fatalf("retention policy mismatch: %#v", o)
}
}
@ -352,7 +352,8 @@ func TestDatabase_SetDefaultRetentionPolicy(t *testing.T) {
s.CreateDatabase("foo")
db := s.Database("foo")
if err := db.CreateRetentionPolicy(&influxdb.RetentionPolicy{Name: "bar"}); err != nil {
rp := &influxdb.RetentionPolicy{Name: "bar"}
if err := db.CreateRetentionPolicy(rp); err != nil {
t.Fatal(err)
} else if db.RetentionPolicy("bar") == nil {
t.Fatal("retention policy not created")
@ -363,20 +364,20 @@ func TestDatabase_SetDefaultRetentionPolicy(t *testing.T) {
t.Fatal(err)
}
rp := db.DefaultRetentionPolicy()
if rp == nil {
o := db.DefaultRetentionPolicy()
if o == nil {
t.Fatal("default policy not set")
} else if !reflect.DeepEqual(db.RetentionPolicy("bar"), rp) {
t.Fatalf("retention policy mismatch: %#v", rp)
} else if !reflect.DeepEqual(rp, o) {
t.Fatalf("retention policy mismatch: %#v", o)
}
s.Restart()
rp = db.DefaultRetentionPolicy()
if rp == nil {
o = s.Database("foo").DefaultRetentionPolicy()
if o == nil {
t.Fatal("default policy not kept after restart")
} else if !reflect.DeepEqual(db.RetentionPolicy("bar"), rp) {
t.Fatalf("retention policy mismatch after restart: %#v", rp)
} else if !reflect.DeepEqual(rp, o) {
t.Fatalf("retention policy mismatch after restart: %#v", o)
}
}

View File

@ -619,7 +619,16 @@ func (s *Server) applySetDefaultRetentionPolicy(m *messaging.Message) error {
return ErrDatabaseNotFound
}
return db.applySetDefaultRetentionPolicy(c.Name)
if err := db.applySetDefaultRetentionPolicy(c.Name); err != nil {
return err
}
// Persist to metastore.
s.meta.mustUpdate(func(tx *metatx) error {
return tx.saveDatabase(db)
})
return nil
}
type setDefaultRetentionPolicyCommand struct {