Skip to content

Commit

Permalink
generate RENAME statement for new name format
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach committed Nov 27, 2023
1 parent 1a06719 commit b55ca8c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
46 changes: 45 additions & 1 deletion go/vt/schema/tablegc.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (

const (
GCTableNameExpression string = `^_vt_(HOLD|PURGE|EVAC|DROP)_([0-f]{32})_([0-9]{14})$`
NewGCTableNameExpression string = `^_vt_(hld|prg|evc|drp)_([0-f]{32})_([0-9]{14})_$` // =========== use this in conjunction with GCTableNameExpression in vreplicator. Add testing!
NewGCTableNameExpression string = `^_vt_(hld|prg|evc|drp)_([0-f]{32})_([0-9]{14})_$`
)

var (
Expand Down Expand Up @@ -87,11 +87,41 @@ func generateGCTableName(state TableGCState, uuid string, t time.Time) (tableNam
return fmt.Sprintf("_vt_%s_%s_%s", state, uuid, timestamp), nil
}

// generateGCTableNameNewFormat creates a GC table name, based on desired state and time, and with optional preset UUID.
// If uuid is given, then it must be in GC-UUID format. If empty, the function auto-generates a UUID.
func generateGCTableNameNewFormat(state TableGCState, uuid string, t time.Time) (tableName string, err error) {
if uuid == "" {
uuid, err = CreateUUIDWithDelimiter("")
}
if err != nil {
return "", err
}
if !IsGCUUID(uuid) {
return "", fmt.Errorf("Not a valid GC UUID format: %s", uuid)
}
timestamp := ToReadableTimestamp(t)
var hint string
for k, v := range gcStates {
if v != state {
continue
}
if len(k) == 3 && k != string(state) { // the "new" format
hint = k
}
}
return fmt.Sprintf("_vt_%s_%s_%s_", hint, uuid, timestamp), nil
}

// GenerateGCTableName creates a GC table name, based on desired state and time, and with random UUID
func GenerateGCTableName(state TableGCState, t time.Time) (tableName string, err error) {
return generateGCTableName(state, "", t)
}

// GenerateGCTableNameNewFormat creates a GC table name, based on desired state and time, and with random UUID
func GenerateGCTableNameNewFormat(state TableGCState, t time.Time) (tableName string, err error) {
return generateGCTableNameNewFormat(state, "", t)
}

// AnalyzeGCTableName analyzes a given table name to see if it's a GC table, and if so, parse out
// its state, uuid, and timestamp
func AnalyzeGCTableName(tableName string) (isGCTable bool, state TableGCState, uuid string, t time.Time, err error) {
Expand Down Expand Up @@ -133,11 +163,25 @@ func GenerateRenameStatementWithUUID(fromTableName string, state TableGCState, u
return fmt.Sprintf("RENAME TABLE `%s` TO %s", fromTableName, toTableName), toTableName, nil
}

// GenerateRenameStatementWithUUIDNewFormat generates a "RENAME TABLE" statement, where a table is renamed to a GC table, with preset UUID
func GenerateRenameStatementWithUUIDNewFormat(fromTableName string, state TableGCState, uuid string, t time.Time) (statement string, toTableName string, err error) {
toTableName, err = generateGCTableNameNewFormat(state, uuid, t)
if err != nil {
return "", "", err
}
return fmt.Sprintf("RENAME TABLE `%s` TO %s", fromTableName, toTableName), toTableName, nil
}

// GenerateRenameStatement generates a "RENAME TABLE" statement, where a table is renamed to a GC table.
func GenerateRenameStatement(fromTableName string, state TableGCState, t time.Time) (statement string, toTableName string, err error) {
return GenerateRenameStatementWithUUID(fromTableName, state, "", t)
}

// GenerateRenameStatement generates a "RENAME TABLE" statement, where a table is renamed to a GC table.
func GenerateRenameStatementNewFormat(fromTableName string, state TableGCState, t time.Time) (statement string, toTableName string, err error) {
return GenerateRenameStatementWithUUIDNewFormat(fromTableName, state, "", t)
}

// ParseGCLifecycle parses a comma separated list of gc states and returns a map of indicated states
func ParseGCLifecycle(gcLifecycle string) (states map[TableGCState]bool, err error) {
states = make(map[TableGCState]bool)
Expand Down
8 changes: 8 additions & 0 deletions go/vt/schema/tablegc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func TestIsGCTableName(t *testing.T) {
tableName, err := generateGCTableName(state, "", tm)
assert.NoError(t, err)
assert.True(t, IsGCTableName(tableName))

tableName, err = generateGCTableNameNewFormat(state, "6ace8bcef73211ea87e9f875a4d24e90", tm)
assert.NoError(t, err)
assert.Truef(t, IsGCTableName(tableName), "table name: %s", tableName)

tableName, err = GenerateGCTableNameNewFormat(state, tm)
assert.NoError(t, err)
assert.Truef(t, IsGCTableName(tableName), "table name: %s", tableName)
}
}
t.Run("accept", func(t *testing.T) {
Expand Down

0 comments on commit b55ca8c

Please sign in to comment.