Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(schema/mysql): stop duplicating DDL for partitioned tables (BYT-10057) (#21212)
Danny Xu8a0aeced70bb
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.goindex d000bfe1ad..f26b73ca76 100644--- a/backend/plugin/schema/differ.go+++ b/backend/plugin/schema/differ.go@@ -374,7 +374,7 @@ func addNewSchemaObjects(diff *MetadataDiff, schemaName string, schema *model.Sc// Add all tablesfor _, tableName := range schema.ListTableNames() {- table := schema.GetTable(tableName)+ table := schema.GetRootTable(tableName)if table != nil && !table.GetProto().GetSkipDump() {diff.TableChanges = append(diff.TableChanges, &TableDiff{Action: MetadataDiffActionCreate,@@ -493,8 +493,8 @@ func compareSchemaObjects(engine storepb.Engine, diff *MetadataDiff, schemaName// Check for dropped tablesfor _, tableName := range oldSchema.ListTableNames() {- if newSchema.GetTable(tableName) == nil {- oldTable := oldSchema.GetTable(tableName)+ if newSchema.GetRootTable(tableName) == nil {+ oldTable := oldSchema.GetRootTable(tableName)if oldTable != nil && !oldTable.GetProto().GetSkipDump() {diff.TableChanges = append(diff.TableChanges, &TableDiff{Action: MetadataDiffActionDrop,@@ -508,12 +508,12 @@ func compareSchemaObjects(engine storepb.Engine, diff *MetadataDiff, schemaName// Check for new and modified tablesfor _, tableName := range newSchema.ListTableNames() {- newTable := newSchema.GetTable(tableName)+ newTable := newSchema.GetRootTable(tableName)if newTable == nil || newTable.GetProto().GetSkipDump() {continue}- if oldSchema.GetTable(tableName) == nil {+ if oldSchema.GetRootTable(tableName) == nil {diff.TableChanges = append(diff.TableChanges, &TableDiff{Action: MetadataDiffActionCreate,SchemaName: schemaName,@@ -522,7 +522,7 @@ func compareSchemaObjects(engine storepb.Engine, diff *MetadataDiff, schemaName})} else {// Compare table details- oldTable := oldSchema.GetTable(tableName)+ oldTable := oldSchema.GetRootTable(tableName)if oldTable != nil && !oldTable.GetProto().GetSkipDump() {tableDiff := compareTableDetails(engine, schemaName, tableName, oldTable, newTable)if tableDiff != nil {diff --git a/backend/plugin/schema/mysql/generate_migration.go b/backend/plugin/schema/mysql/generate_migration.goindex 2531e410b8..5ad601d750 100644--- a/backend/plugin/schema/mysql/generate_migration.go+++ b/backend/plugin/schema/mysql/generate_migration.go@@ -601,6 +601,15 @@ func writeCreateTableWithoutForeignKeys(buf *strings.Builder, tableName string,_, _ = buf.WriteString("'")}+ // Partitioning is part of the table definition, so it has to be emitted here: MySQL+ // cannot partition a table after the fact without rewriting it. printPartitionClause+ // writes a self-contained /*!50100 ... */ block, matching the schema dump.+ if len(table.Partitions) > 0 {+ if err := printPartitionClause(buf, table.Partitions, table.Engine); err != nil {+ return err+ }+ }+_, _ = buf.WriteString(";\n")// Create non-unique indexes separatelydiff --git a/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go b/backend/plugin/schema/mysql/generate_migration_testcontainer_test.goindex 88be2e76b7..6d92876995 100644--- a/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go+++ b/backend/plugin/schema/mysql/generate_migration_testcontainer_test.go@@ -632,6 +632,65 @@ ALTER TABLE sales_data ADD CONSTRAINT chk_amount_positive CHECK (amount > 0);`,description: "Operations on partitioned tables",},+ {+ // The "partitioned_tables" case above only exercises the drop direction, which+ // generateMigration deduplicates on its own (see tableDrop). This case makes the+ // rollback re-add the constraints, so it covers the create/alter direction where+ // each partition used to contribute another copy of the table to the diff+ // (BYT-10057): the repeated ADD PRIMARY KEY failed with "Multiple primary key+ // defined" and the repeated CREATE INDEX with "Duplicate key name".+ name: "reverse_partitioned_table_constraints",+ initialSchema: `+CREATE TABLE ads_member_asset_by_account_channel_d (+ dt DATE NOT NULL,+ member_id BIGINT NOT NULL,+ account_type VARCHAR(50) NOT NULL,+ net_asset_amount DECIMAL(38,10) DEFAULT NULL,+ PRIMARY KEY (dt, member_id, account_type),+ KEY sindex (dt, member_id, account_type)+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci+PARTITION BY RANGE (TO_DAYS(dt)) (+ PARTITION p0 VALUES LESS THAN (739000),+ PARTITION p1 VALUES LESS THAN (739001),+ PARTITION p2 VALUES LESS THAN (739002),+ PARTITION p3 VALUES LESS THAN (739003),+ PARTITION p_future VALUES LESS THAN MAXVALUE+);+`,+ migrationDDL: `+DROP INDEX sindex ON ads_member_asset_by_account_channel_d;+ALTER TABLE ads_member_asset_by_account_channel_d DROP PRIMARY KEY;+`,+ description: "Re-adding a primary key and an overlapping secondary index on a partitioned table",+ },+ {+ // Recreating the table exercises the partition clause itself: partitioning is+ // part of the table definition and cannot be added afterwards, so a generated+ // CREATE TABLE that omits it silently hands the target an unpartitioned copy.+ // The schema comparison at the end of this test is what catches that, since+ // partitions are not normalized away.+ name: "reverse_create_partitioned_table",+ initialSchema: `+CREATE TABLE ads_member_asset_by_account_channel_d (+ dt DATE NOT NULL,+ member_id BIGINT NOT NULL,+ account_type VARCHAR(50) NOT NULL,+ net_asset_amount DECIMAL(38,10) DEFAULT NULL,+ PRIMARY KEY (dt, member_id, account_type),+ KEY sindex (dt, member_id, account_type)+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci+PARTITION BY RANGE (TO_DAYS(dt)) (+ PARTITION p0 VALUES LESS THAN (739000),+ PARTITION p1 VALUES LESS THAN (739001),+ PARTITION p2 VALUES LESS THAN (739002),+ PARTITION p_future VALUES LESS THAN MAXVALUE+);+`,+ migrationDDL: `+DROP TABLE ads_member_asset_by_account_channel_d;+`,+ description: "Recreating a partitioned table from metadata",+ },{name: "generated_columns",initialSchema: `diff --git a/backend/plugin/schema/mysql/get_database_definition.go b/backend/plugin/schema/mysql/get_database_definition.goindex bbf9b9ac02..88651c8ff2 100644--- a/backend/plugin/schema/mysql/get_database_definition.go+++ b/backend/plugin/schema/mysql/get_database_definition.go@@ -609,7 +609,7 @@ func writeTable(out *strings.Builder, table *storepb.TableMetadata) error {}if len(table.Partitions) > 0 {- if err := printPartitionClause(out, table.Partitions); err != nil {+ if err := printPartitionClause(out, table.Partitions, table.Engine); err != nil {return err}}@@ -619,7 +619,7 @@ func writeTable(out *strings.Builder, table *storepb.TableMetadata) error {}// Copy the logic from backend/plugin/schema/mysql/state.go.-func printPartitionClause(buf *strings.Builder, partitions []*storepb.TablePartitionMetadata) error {+func printPartitionClause(buf *strings.Builder, partitions []*storepb.TablePartitionMetadata, engine string) error {if len(partitions) == 0 {return nil}@@ -734,7 +734,7 @@ func printPartitionClause(buf *strings.Builder, partitions []*storepb.TablePartireturn err}}- if _, err := fmt.Fprintf(buf, "PARTITION %s", partition.Name); err != nil {+ if _, err := fmt.Fprintf(buf, "PARTITION %s", mysqlQuoteIdentifier(partition.Name)); err != nil {return err}if preposition != "" {@@ -754,10 +754,10 @@ func printPartitionClause(buf *strings.Builder, partitions []*storepb.TablePartireturn err}for j, subPartition := range partition.Subpartitions {- if _, err := fmt.Fprintf(buf, "SUBPARTITION %s", subPartition.Name); err != nil {+ if _, err := fmt.Fprintf(buf, "SUBPARTITION %s", mysqlQuoteIdentifier(subPartition.Name)); err != nil {return err}- if err := writePartitionOptions(buf); err != nil {+ if err := writePartitionOptions(buf, engine); err != nil {return err}if j == len(partition.Subpartitions)-1 {@@ -771,7 +771,7 @@ func printPartitionClause(buf *strings.Builder, partitions []*storepb.TableParti}}} else {- if err := writePartitionOptions(buf); err != nil {+ if err := writePartitionOptions(buf, engine); err != nil {return err}}@@ -808,7 +808,7 @@ func getPrepositionByType(tp storepb.TablePartitionMetadata_Type) (string, error}}-func writePartitionOptions(buf io.StringWriter) error {+func writePartitionOptions(buf io.StringWriter, engine string) error {/*int err = 0;err += add_space(fptr);@@ -833,8 +833,15 @@ func writePartitionOptions(buf io.StringWriter) error {err += add_keyword_string(fptr, "COMMENT", true, p_elem->part_comment);return err + add_engine(fptr, p_elem->engine_type);*/- // TODO(zp): Get all the partition options from the metadata is too complex, just write ENGINE=InnoDB for now.- if _, err := buf.WriteString(" ENGINE=InnoDB"); err != nil {+ // TODO(zp): Get all the partition options from the metadata is too complex, just write+ // the engine for now. It must be the table's own engine: MySQL rejects a partitioned+ // table whose partitions name a different one with "ERROR 1497: The mix of handlers in+ // the partitions is not allowed in this version of MySQL", and partitioning a+ // non-InnoDB table is still legal on 5.7.+ if engine == "" {+ engine = "InnoDB"+ }+ if _, err := buf.WriteString(" ENGINE=" + engine); err != nil {return err}@@ -1781,7 +1788,7 @@ func writeTableSDL(buf *strings.Builder, table *storepb.TableMetadata) error {// differ canonicalizes the expression and per-partition ENGINE, so reusing the// mysqldump partition writer round-trips as a no-op without an SDL-specific form.if len(table.Partitions) > 0 {- if err := printPartitionClause(buf, table.Partitions); err != nil {+ if err := printPartitionClause(buf, table.Partitions, table.Engine); err != nil {return err}}diff --git a/backend/plugin/schema/mysql/partition_diff_test.go b/backend/plugin/schema/mysql/partition_diff_test.gonew file mode 100644index 0000000000..81de3ee104--- /dev/null+++ b/backend/plugin/schema/mysql/partition_diff_test.go@@ -0,0 +1,179 @@+package mysql++import (+ "fmt"+ "strings"+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+ "github.com/bytebase/bytebase/backend/plugin/schema"+ "github.com/bytebase/bytebase/backend/store/model"+)++// partitionedTable is the shape reported in BYT-10057: a daily BI table with a composite+// primary key, a secondary index over the same ordered columns, and one range partition+// per day. Each partition used to add another copy of the table to the diff, so the+// generated script repeated the whole DDL block once per partition and died on the second+// copy with "Duplicate key name" (1061) or "Multiple primary key defined" (1068).+func partitionedTable(partitionCount int) *storepb.TableMetadata {+ table := &storepb.TableMetadata{+ Name: "ads_member_asset_by_account_channel_d",+ Engine: "InnoDB",+ Charset: "utf8mb4",+ Collation: "utf8mb4_general_ci",+ Columns: []*storepb.ColumnMetadata{+ {Name: "dt", Type: "date", Nullable: false},+ {Name: "member_id", Type: "bigint", Nullable: false},+ {Name: "account_type", Type: "varchar(50)", Nullable: false},+ },+ Indexes: []*storepb.IndexMetadata{+ {Name: "PRIMARY", Expressions: []string{"dt", "member_id", "account_type"}, Primary: true, Unique: true, Visible: true, Type: "BTREE"},+ {Name: "sindex", Expressions: []string{"dt", "member_id", "account_type"}, Visible: true, Type: "BTREE"},+ },+ }+ for i := range partitionCount {+ table.Partitions = append(table.Partitions, &storepb.TablePartitionMetadata{+ Name: fmt.Sprintf("p%d", i),+ Type: storepb.TablePartitionMetadata_RANGE,+ Expression: "to_days(`dt`)",+ Value: fmt.Sprintf("%d", 739000+i),+ })+ }+ return table+}++func databaseOf(tables ...*storepb.TableMetadata) *model.DatabaseMetadata {+ return model.NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{+ Name: "db",+ Schemas: []*storepb.SchemaMetadata{{Name: "", Tables: tables}},+ }, nil, &storepb.DatabaseConfig{}, storepb.Engine_MYSQL, true)+}++func TestDiffMigrationPartitionedTableEmittedOnce(t *testing.T) {+ for _, partitionCount := range []int{0, 1, 3, 180} {+ t.Run(fmt.Sprintf("partitions=%d", partitionCount), func(t *testing.T) {+ a := require.New(t)+ source := partitionedTable(partitionCount)++ // Target does not have the table yet: the 1061 shape.+ migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))+ a.NoError(err)+ a.Equal(1, strings.Count(migration, "CREATE TABLE"), "CREATE TABLE must be emitted once:\n%s", migration)+ a.Equal(1, strings.Count(migration, "CREATE INDEX `sindex`"), "sindex must be emitted once:\n%s", migration)++ // Target has the table but neither the primary key nor the index: the 1068 shape.+ bare := &storepb.TableMetadata{+ Name: source.Name,+ Engine: source.Engine,+ Charset: source.Charset,+ Collation: source.Collation,+ Columns: source.Columns,+ }+ migration, err = schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(bare), databaseOf(source))+ a.NoError(err)+ a.Equal(1, strings.Count(migration, "ADD PRIMARY KEY"), "primary key must be added once:\n%s", migration)+ a.Equal(1, strings.Count(migration, "CREATE INDEX `sindex`"), "sindex must be emitted once:\n%s", migration)++ // Diffing the table against itself stays convergent.+ migration, err = schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(source), databaseOf(partitionedTable(partitionCount)))+ a.NoError(err)+ a.Empty(migration)+ })+ }+}++func TestDiffMigrationPreservesPartitioning(t *testing.T) {+ a := require.New(t)+ source := partitionedTable(3)++ migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))+ a.NoError(err)+ a.Contains(migration, "PARTITION BY RANGE (to_days(`dt`))", "created table must keep its partitioning:\n%s", migration)+ for _, partition := range source.Partitions {+ a.Contains(migration, "PARTITION `"+partition.Name+"`", "partition %s must be created", partition.Name)+ }+}++// MySQL rejects a partitioned table whose partitions declare a different storage engine+// than the table itself: "ERROR 1497 (HY000): The mix of handlers in the partitions is+// not allowed in this version of MySQL". Partitioning a non-InnoDB table is still legal+// on MySQL 5.7, so the partition clause has to carry the table's own engine.+func TestDiffMigrationPartitionEngineMatchesTable(t *testing.T) {+ for _, engine := range []string{"InnoDB", "MyISAM", "ARCHIVE"} {+ t.Run(engine, func(t *testing.T) {+ a := require.New(t)+ source := partitionedTable(2)+ source.Engine = engine++ migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))+ a.NoError(err)+ a.Equal(len(source.Partitions), strings.Count(migration, "PARTITION `p"),+ "every partition must be emitted:\n%s", migration)+ a.Equal(len(source.Partitions)+1, strings.Count(migration, "ENGINE="+engine),+ "the table and every partition must declare the same engine:\n%s", migration)+ })+ }+}++// Partition names are identifiers, so a reserved word is legal when quoted at creation and+// comes back from information_schema unquoted. MySQL's own SHOW CREATE TABLE quotes them,+// and the regenerated DDL has to as well or it is a syntax error.+func TestDiffMigrationQuotesPartitionNames(t *testing.T) {+ a := require.New(t)+ source := partitionedTable(0)+ source.Partitions = []*storepb.TablePartitionMetadata{+ {Name: "select", Type: storepb.TablePartitionMetadata_RANGE, Expression: "to_days(`dt`)", Value: "739000"},+ {Name: "order", Type: storepb.TablePartitionMetadata_RANGE, Expression: "to_days(`dt`)", Value: "MAXVALUE"},+ }++ migration, err := schema.DiffMigration(storepb.Engine_MYSQL, databaseOf(), databaseOf(source))+ a.NoError(err)+ a.Contains(migration, "PARTITION `select`", "reserved-word partition name must be quoted:\n%s", migration)+ a.Contains(migration, "PARTITION `order`", "reserved-word partition name must be quoted:\n%s", migration)+}++// tableAction returns the action the diff recorded for one table, or "" if absent.+func tableAction(diff *schema.MetadataDiff, name string) schema.MetadataDiffAction {+ for _, tableDiff := range diff.TableChanges {+ if tableDiff.TableName == name {+ return tableDiff.Action+ }+ }+ return ""+}++// The differ decides whether a table exists in the other snapshot by name. A partition of+// some other table may carry that name, and resolving it as if it were the table makes the+// differ miss a drop and mistake a create for a modification of the partition's owner.+func TestDiffMigrationPartitionAliasDoesNotMaskTableExistence(t *testing.T) {+ archive := &storepb.TableMetadata{+ Name: "archive", Engine: "InnoDB",+ Columns: []*storepb.ColumnMetadata{{Name: "archive_col", Type: "int"}},+ }+ events := func(partitions ...*storepb.TablePartitionMetadata) *storepb.TableMetadata {+ return &storepb.TableMetadata{+ Name: "events", Engine: "InnoDB",+ Columns: []*storepb.ColumnMetadata{{Name: "events_col", Type: "int"}},+ Partitions: partitions,+ }+ }+ alias := &storepb.TablePartitionMetadata{Name: "archive", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"}++ t.Run("drop", func(t *testing.T) {+ a := require.New(t)+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_MYSQL,+ databaseOf(archive, events()), databaseOf(events(alias)))+ a.NoError(err)+ a.Equal(schema.MetadataDiffActionDrop, tableAction(diff, "archive"))+ })++ t.Run("create", func(t *testing.T) {+ a := require.New(t)+ diff, err := schema.GetDatabaseSchemaDiff(storepb.Engine_MYSQL,+ databaseOf(events(alias)), databaseOf(archive, events()))+ a.NoError(err)+ a.Equal(schema.MetadataDiffActionCreate, tableAction(diff, "archive"))+ })+}diff --git a/backend/store/model/database.go b/backend/store/model/database.goindex 45f91d152a..d09bc7081d 100644--- a/backend/store/model/database.go+++ b/backend/store/model/database.go@@ -29,9 +29,14 @@ type DatabaseMetadata struct {// SchemaMetadata is the unified metadata for a schema, combining proto metadata and catalog config.type SchemaMetadata struct {- isObjectCaseSensitive bool- isDetailCaseSensitive bool- internalTables map[string]*TableMetadata+ isObjectCaseSensitive bool+ isDetailCaseSensitive bool+ internalTables map[string]*TableMetadata+ // internalPartitionTables maps a partition name to its owning table, so a partition can+ // be resolved by name. It is kept apart from internalTables because partition names are+ // scoped per table and may collide with a real table's name; merging the two let a+ // partition shadow a root table and hand callers the wrong table's proto.+ internalPartitionTables map[string]*TableMetadatainternalExternalTable map[string]*ExternalTableMetadatainternalViews map[string]*storepb.ViewMetadatainternalMaterializedView map[string]*storepb.MaterializedViewMetadata@@ -127,6 +132,7 @@ func NewDatabaseMetadata(isObjectCaseSensitive: isObjectCaseSensitive,isDetailCaseSensitive: isDetailCaseSensitive,internalTables: make(map[string]*TableMetadata),+ internalPartitionTables: make(map[string]*TableMetadata),internalExternalTable: make(map[string]*ExternalTableMetadata),internalViews: make(map[string]*storepb.ViewMetadata),internalMaterializedView: make(map[string]*storepb.MaterializedViewMetadata),@@ -141,6 +147,10 @@ func NewDatabaseMetadata(tables, names := buildTablesMetadata(table, tableCatalog, isDetailCaseSensitive)for i, table := range tables {tableID := normalizeNameByCaseSensitivity(names[i], isObjectCaseSensitive)+ if table.partitionOf != nil {+ schemaMetadata.internalPartitionTables[tableID] = table+ continue+ }schemaMetadata.internalTables[tableID] = table}}@@ -285,6 +295,7 @@ func (d *DatabaseMetadata) CreateSchema(schemaName string) *SchemaMetadata {isObjectCaseSensitive: d.isObjectCaseSensitive,isDetailCaseSensitive: d.isDetailCaseSensitive,internalTables: make(map[string]*TableMetadata),+ internalPartitionTables: make(map[string]*TableMetadata),internalExternalTable: make(map[string]*ExternalTableMetadata),internalViews: make(map[string]*storepb.ViewMetadata),internalMaterializedView: make(map[string]*storepb.MaterializedViewMetadata),@@ -335,7 +346,23 @@ func (s *SchemaMetadata) GetTable(name string) *TableMetadata {return nil}nameID := normalizeNameByCaseSensitivity(name, s.isObjectCaseSensitive)- return s.internalTables[nameID]+ if table, ok := s.internalTables[nameID]; ok {+ return table+ }+ return s.internalPartitionTables[nameID]+}++// GetRootTable looks up a real table, ignoring partition aliases. Callers asking "does a+// table by this name exist" want this rather than GetTable: a partition may carry another+// table's name, and answering with its owner makes the schema differ miss a drop and+// mistake a create for a modification. GetTable keeps resolving partition names because+// engines such as PostgreSQL expose partitions as queryable relations, so query analysis+// has to find them.+func (s *SchemaMetadata) GetRootTable(name string) *TableMetadata {+ if s == nil {+ return nil+ }+ return s.internalTables[normalizeNameByCaseSensitivity(name, s.isObjectCaseSensitive)]}// GetIndex gets the index by name.@@ -446,7 +473,10 @@ func (s *SchemaMetadata) GetCatalog() *storepb.SchemaCatalog {// ListTableNames lists the table names.func (s *SchemaMetadata) ListTableNames() []string {- var result []string+ // internalTables holds only root tables; partitions live in internalPartitionTables.+ // Listing and GetTable therefore read the same map, so every name returned here+ // resolves back to the table it names.+ result := make([]string, 0, len(s.internalTables))for _, table := range s.internalTables {result = append(result, table.GetProto().GetName())}@@ -514,7 +544,7 @@ func (s *SchemaMetadata) ListSequenceNames() []string {// Returns the created TableMetadata or an error if the table already exists.func (s *SchemaMetadata) CreateTable(tableName string) (*TableMetadata, error) {// Check if table already exists- if s.GetTable(tableName) != nil {+ if s.GetRootTable(tableName) != nil {return nil, errors.Errorf("table %q already exists in schema %q", tableName, s.proto.Name)}@@ -547,7 +577,7 @@ func (s *SchemaMetadata) CreateTable(tableName string) (*TableMetadata, error) {// Returns an error if the table does not exist.func (s *SchemaMetadata) DropTable(tableName string) error {// Check if table exists- if s.GetTable(tableName) == nil {+ if s.GetRootTable(tableName) == nil {return errors.Errorf("table %q does not exist in schema %q", tableName, s.proto.Name)}@@ -581,13 +611,13 @@ func (s *SchemaMetadata) RenameTable(oldName string, newName string) error {}// Check if old table exists- oldTable := s.GetTable(oldName)+ oldTable := s.GetRootTable(oldName)if oldTable == nil {return errors.Errorf("table %q does not exist in schema %q", oldName, s.proto.Name)}// Check if new table already exists- if s.GetTable(newName) != nil {+ if s.GetRootTable(newName) != nil {return errors.Errorf("table %q already exists in schema %q", newName, s.proto.Name)}diff --git a/backend/store/model/database_test.go b/backend/store/model/database_test.goindex 355a4fd997..56d3054c53 100644--- a/backend/store/model/database_test.go+++ b/backend/store/model/database_test.go@@ -357,3 +357,135 @@ func TestTableMetadata_DropColumn_NotExists(t *testing.T) {require.NotNil(t, err)require.Contains(t, err.Error(), "does not exist")}++func TestListTableNames_ExcludesPartitions(t *testing.T) {+ a := require.New(t)++ // A range-partitioned table whose last partition is itself subpartitioned. Every+ // partition and subpartition is registered in internalTables under its own name but+ // carries the parent's proto, so listing them once yielded "sales" six times.+ sales := &storepb.TableMetadata{+ Name: "sales",+ Columns: []*storepb.ColumnMetadata{{Name: "dt"}},+ Partitions: []*storepb.TablePartitionMetadata{+ {Name: "p0", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"},+ {Name: "p1", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"},+ {+ Name: "p2", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt",+ Subpartitions: []*storepb.TablePartitionMetadata{+ {Name: "p2s0", Type: storepb.TablePartitionMetadata_HASH, Expression: "dt"},+ {Name: "p2s1", Type: storepb.TablePartitionMetadata_HASH, Expression: "dt"},+ },+ },+ },+ }+ customers := &storepb.TableMetadata{+ Name: "customers",+ Columns: []*storepb.ColumnMetadata{{Name: "id"}},+ }++ dbMetadata := NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{+ Name: "db",+ Schemas: []*storepb.SchemaMetadata{{Name: "", Tables: []*storepb.TableMetadata{sales, customers}}},+ }, nil, &storepb.DatabaseConfig{}, storepb.Engine_MYSQL, true)+ schemaMetadata := dbMetadata.GetSchemaMetadata("")++ a.Equal([]string{"customers", "sales"}, schemaMetadata.ListTableNames())++ // Partitions stay resolvable by name; only the listing changed.+ for _, partitionName := range []string{"p0", "p1", "p2", "p2s0", "p2s1"} {+ a.NotNil(schemaMetadata.GetTable(partitionName), "partition %s should still resolve", partitionName)+ }+ a.NotNil(schemaMetadata.GetTable("sales"))+}++// Partition names live in a namespace separate from table names, so a partition may carry+// its own table's name. Both land in internalTables under the same key and the partition+// wrapper wins, so the root table has to be listed from the schema's own table list rather+// than inferred from that map.+func TestListTableNames_PartitionSharingTableName(t *testing.T) {+ a := require.New(t)++ dbMetadata := NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{+ Name: "db",+ Schemas: []*storepb.SchemaMetadata{{Name: "", Tables: []*storepb.TableMetadata{+ {+ Name: "sales",+ Columns: []*storepb.ColumnMetadata{{Name: "dt"}},+ Partitions: []*storepb.TablePartitionMetadata{+ {Name: "sales", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"},+ {Name: "p1", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"},+ },+ },+ }}},+ }, nil, &storepb.DatabaseConfig{}, storepb.Engine_MYSQL, true)++ a.Equal([]string{"sales"}, dbMetadata.GetSchemaMetadata("").ListTableNames())+}++// A partition of one table may share the name of a different root table. The root listing+// and the root lookup must agree: resolving that name has to return the root table's own+// proto, not the partition owner's, or the differ generates one table using another's+// columns and indexes.+func TestGetTable_PartitionAliasDoesNotShadowRootTable(t *testing.T) {+ a := require.New(t)++ dbMetadata := NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{+ Name: "db",+ Schemas: []*storepb.SchemaMetadata{{Name: "", Tables: []*storepb.TableMetadata{+ {+ Name: "archive",+ Columns: []*storepb.ColumnMetadata{{Name: "archive_col"}},+ },+ {+ Name: "events",+ Columns: []*storepb.ColumnMetadata{{Name: "events_col"}},+ Partitions: []*storepb.TablePartitionMetadata{+ {Name: "archive", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"},+ {Name: "recent", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"},+ },+ },+ }}},+ }, nil, &storepb.DatabaseConfig{}, storepb.Engine_MYSQL, true)+ schemaMetadata := dbMetadata.GetSchemaMetadata("")++ a.Equal([]string{"archive", "events"}, schemaMetadata.ListTableNames())++ // Every listed name must resolve to that same table.+ for _, name := range schemaMetadata.ListTableNames() {+ a.Equal(name, schemaMetadata.GetTable(name).GetProto().GetName(),+ "GetTable(%q) resolved to the wrong table", name)+ }+ a.NotNil(schemaMetadata.GetTable("archive").GetColumn("archive_col"))++ // A partition name that is not also a root table still resolves to its owner.+ a.Equal("events", schemaMetadata.GetTable("recent").GetProto().GetName())+}++// A partition name does not reserve a table name, and dropping a partition by name is not+// dropping a table. Table creation and drops therefore have to consult real tables only.+func TestTableMutationsIgnorePartitionAliases(t *testing.T) {+ a := require.New(t)++ dbMetadata := NewDatabaseMetadata(&storepb.DatabaseSchemaMetadata{+ Name: "db",+ Schemas: []*storepb.SchemaMetadata{{Name: "", Tables: []*storepb.TableMetadata{+ {+ Name: "events",+ Columns: []*storepb.ColumnMetadata{{Name: "events_col"}},+ Partitions: []*storepb.TablePartitionMetadata{+ {Name: "archive", Type: storepb.TablePartitionMetadata_RANGE, Expression: "dt"},+ },+ },+ }}},+ }, nil, &storepb.DatabaseConfig{}, storepb.Engine_MYSQL, true)+ schemaMetadata := dbMetadata.GetSchemaMetadata("")++ a.ErrorContains(schemaMetadata.DropTable("archive"), "does not exist")++ created, err := schemaMetadata.CreateTable("archive")+ a.NoError(err)+ a.Equal("archive", created.GetProto().GetName())+ a.Equal([]string{"archive", "events"}, schemaMetadata.ListTableNames())+ a.Equal("archive", schemaMetadata.GetTable("archive").GetProto().GetName())+}