-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from cashapp/jayj/old-table-timestamped
Jayj/old table timestamped
- Loading branch information
Showing
7 changed files
with
171 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package check | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/siddontang/loggers" | ||
) | ||
|
||
const ( | ||
// Max table name length in MySQL | ||
maxTableNameLength = 64 | ||
|
||
// Formats for table names | ||
NameFormatSentinel = "_%s_sentinel" | ||
NameFormatCheckpoint = "_%s_chkpnt" | ||
NameFormatNew = "_%s_new" | ||
NameFormatOld = "_%s_old" | ||
NameFormatOldTimeStamp = "_%s_old_%s" | ||
NameFormatTimestamp = "20060102_150405" | ||
) | ||
|
||
var ( | ||
// The number of extra characters needed for table names with all possible | ||
// formats. These vars are calculated in the `init` function below. | ||
NameFormatNormalExtraChars = 0 | ||
NameFormatTimestampExtraChars = 0 | ||
) | ||
|
||
func init() { | ||
registerCheck("tablename", tableNameCheck, ScopePreflight) | ||
|
||
// Calculate the number of extra characters needed table names with all possible formats | ||
for _, format := range []string{NameFormatSentinel, NameFormatCheckpoint, NameFormatNew, NameFormatOld} { | ||
extraChars := len(strings.Replace(format, "%s", "", -1)) | ||
if extraChars > NameFormatNormalExtraChars { | ||
NameFormatNormalExtraChars = extraChars | ||
} | ||
} | ||
|
||
// Calculate the number of extra characters needed for table names with the old timestamp format | ||
NameFormatTimestampExtraChars = len(strings.Replace(NameFormatOldTimeStamp, "%s", "", -1)) + len(NameFormatTimestamp) | ||
} | ||
|
||
func tableNameCheck(ctx context.Context, r Resources, logger loggers.Advanced) error { | ||
tableName := r.Table.TableName | ||
if len(tableName) < 1 { | ||
return errors.New("table name must be at least 1 character") | ||
} | ||
|
||
timestampTableNameLength := maxTableNameLength - NameFormatTimestampExtraChars | ||
if r.SkipDropAfterCutover && len(tableName) > timestampTableNameLength { | ||
return fmt.Errorf("table name must be less than %d characters when --skip-drop-after-cutover is set", timestampTableNameLength) | ||
} | ||
|
||
normalTableNameLength := maxTableNameLength - NameFormatNormalExtraChars | ||
if len(tableName) > normalTableNameLength { | ||
return fmt.Errorf("table name must be less than %d characters", normalTableNameLength) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package check | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/cashapp/spirit/pkg/table" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCheckTableNameConstants(t *testing.T) { | ||
// Calculated extra chars should always be greater than 0 | ||
assert.Positive(t, NameFormatNormalExtraChars) | ||
assert.Positive(t, NameFormatTimestampExtraChars) | ||
|
||
// Calculated extra chars should be less than the max table name length | ||
assert.Less(t, NameFormatNormalExtraChars, maxTableNameLength) | ||
assert.Less(t, NameFormatTimestampExtraChars, maxTableNameLength) | ||
} | ||
|
||
func TestCheckTableName(t *testing.T) { | ||
testTableName := func(name string, skipDropAfterCutover bool) error { | ||
r := Resources{ | ||
Table: &table.TableInfo{ | ||
TableName: name, | ||
}, | ||
SkipDropAfterCutover: skipDropAfterCutover, | ||
} | ||
return tableNameCheck(context.Background(), r, logrus.New()) | ||
} | ||
|
||
assert.NoError(t, testTableName("a", false)) | ||
assert.NoError(t, testTableName("a", true)) | ||
|
||
assert.ErrorContains(t, testTableName("", false), "table name must be at least 1 character") | ||
assert.ErrorContains(t, testTableName("", true), "table name must be at least 1 character") | ||
|
||
longName := "thisisareallylongtablenamethisisareallylongtablenamethisisareallylongtablename" | ||
assert.ErrorContains(t, testTableName(longName, false), "table name must be less than") | ||
assert.ErrorContains(t, testTableName(longName, true), "table name must be less than") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.