vitess.io/vitess@v0.16.2/go/vt/schema/name.go (about)

     1  /*
     2  Copyright 2020 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package schema
    18  
    19  import (
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/google/uuid"
    24  )
    25  
    26  const (
    27  	readableTimeFormat = "20060102150405"
    28  )
    29  
    30  // CreateUUIDWithDelimiter creates a globally unique ID, with a given delimiter
    31  // example results:
    32  // - 1876a01a-354d-11eb-9a79-f8e4e33000bb (delimiter = "-")
    33  // - 7cee19dd_354b_11eb_82cd_f875a4d24e90 (delimiter = "_")
    34  // - 55d00cdce6ab11eabfe60242ac1c000d (delimiter = "")
    35  func CreateUUIDWithDelimiter(delimiter string) (string, error) {
    36  	u, err := uuid.NewUUID()
    37  	if err != nil {
    38  		return "", err
    39  	}
    40  	result := u.String()
    41  	result = strings.Replace(result, "-", delimiter, -1)
    42  	return result, nil
    43  }
    44  
    45  // CreateUUID creates a globally unique ID
    46  // example result "1876a01a-354d-11eb-9a79-f8e4e33000bb"
    47  func CreateUUID() (string, error) {
    48  	return CreateUUIDWithDelimiter("-")
    49  }
    50  
    51  // ToReadableTimestamp returns a timestamp, in seconds resolution, that is human readable
    52  // (as opposed to unix timestamp which is just a number)
    53  // Example: for Aug 25 2020, 16:04:25 we return "20200825160425"
    54  func ToReadableTimestamp(t time.Time) string {
    55  	return t.Format(readableTimeFormat)
    56  }
    57  
    58  // IsInternalOperationTableName answers 'true' when the given table name stands for an internal Vitess
    59  // table used for operations such as:
    60  // - Online DDL (gh-ost, pt-online-schema-change)
    61  // - Table GC (renamed before drop)
    62  // Apps such as VStreamer may choose to ignore such tables.
    63  func IsInternalOperationTableName(tableName string) bool {
    64  	if IsGCTableName(tableName) {
    65  		return true
    66  	}
    67  	if IsOnlineDDLTableName(tableName) {
    68  		return true
    69  	}
    70  	return false
    71  }