| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package coordinator |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "time" |
| 10 |
| 11 ds "github.com/luci/gae/service/datastore" |
| 12 ) |
| 13 |
| 14 func (s *LogStream) migrateSchema() error { |
| 15 switch s.Schema { |
| 16 case "": |
| 17 // Pre-schema versioned schema. Migrate to "1". |
| 18 // |
| 19 // To do this, we: |
| 20 // - See if the ArchiveWhole property is available. If it is, we
set the |
| 21 // ArchiveLogEntryCount to equal TerminalIndex+1. Otherwise, w
e leave |
| 22 // ArchiveLogEntryCount at zero, as there is no (easy) way to
determine |
| 23 // its value. |
| 24 |
| 25 // Determine archival state. |
| 26 if s.Archived() { |
| 27 switch props := s.extra["ArchiveWhole"]; len(props) { |
| 28 case 0: |
| 29 break |
| 30 |
| 31 case 1: |
| 32 if v, ok := props[0].Value().(bool); ok && v { |
| 33 s.ArchiveLogEntryCount = s.TerminalIndex
+ 1 |
| 34 } |
| 35 |
| 36 default: |
| 37 return fmt.Errorf("pre-schema ArchiveWhole has %
d values", len(props)) |
| 38 } |
| 39 } |
| 40 |
| 41 // Backfill time based on state. |
| 42 switch { |
| 43 case s.Archived(): |
| 44 if dt := dateTimeProp(s.extra["ArchivedTime"]); dt.IsZer
o() { |
| 45 s.ArchivedTime = s.Created |
| 46 } |
| 47 fallthrough |
| 48 |
| 49 case s.Terminated(): |
| 50 if dt := dateTimeProp(s.extra["TerminatedTime"]); dt.IsZ
ero() { |
| 51 s.TerminatedTime = s.Created |
| 52 } |
| 53 fallthrough |
| 54 |
| 55 default: |
| 56 break |
| 57 } |
| 58 |
| 59 // Migration complete! |
| 60 s.Schema = currentLogStreamSchema |
| 61 fallthrough |
| 62 |
| 63 case currentLogStreamSchema: |
| 64 return nil |
| 65 |
| 66 default: |
| 67 return fmt.Errorf("unrecognized log stream schema %q", s.Schema) |
| 68 } |
| 69 } |
| 70 |
| 71 // dateTimeProp returns the time.Time value of a single-property slice, or |
| 72 // a zero time.Time if the slice wasn't single-value or didn't contain a |
| 73 // time.Time. |
| 74 func dateTimeProp(props []ds.Property) time.Time { |
| 75 if len(props) == 1 { |
| 76 if t, ok := props[0].Value().(time.Time); ok { |
| 77 return t |
| 78 } |
| 79 } |
| 80 return time.Time{} |
| 81 } |
| OLD | NEW |