-
-
Notifications
You must be signed in to change notification settings - Fork 719
feat(import): validate primary keys early #9349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
macneale4
merged 3 commits into
dolthub:main
from
codeaucafe:codeaucafe/feat/1083-validate-import-args-before-reading-import-rows
Jun 24, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,191 @@ | ||
| // Copyright 2025 Dolthub, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package tblcmds | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/dolthub/dolt/go/libraries/doltcore/rowconv" | ||
| "github.com/dolthub/dolt/go/libraries/doltcore/schema" | ||
| ) | ||
|
|
||
| func TestValidatePrimaryKeysAgainstSchema(t *testing.T) { | ||
| // Create a test schema with columns: id, name, email, age | ||
| testCols := []schema.Column{ | ||
| {Name: "id", Tag: 0, IsPartOfPK: true}, | ||
| {Name: "name", Tag: 1, IsPartOfPK: false}, | ||
| {Name: "email", Tag: 2, IsPartOfPK: false}, | ||
| {Name: "age", Tag: 3, IsPartOfPK: false}, | ||
| } | ||
| testSchema, err := schema.NewSchema( | ||
| schema.NewColCollection(testCols...), | ||
| nil, // pkOrdinals - nil means use default ordinals based on IsPartOfPK | ||
| schema.Collation_Default, | ||
| nil, // indexes | ||
| nil, // checks | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| primaryKeys []string | ||
| schema schema.Schema | ||
| expectError bool | ||
| errorContains string | ||
| }{ | ||
| { | ||
| name: "valid single primary key", | ||
| primaryKeys: []string{"id"}, | ||
| schema: testSchema, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "valid multiple primary keys", | ||
| primaryKeys: []string{"id", "name"}, | ||
| schema: testSchema, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "empty primary keys", | ||
| primaryKeys: []string{}, | ||
| schema: testSchema, | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "invalid single primary key", | ||
| primaryKeys: []string{"invalid_col"}, | ||
| schema: testSchema, | ||
| expectError: true, | ||
| errorContains: "primary key 'invalid_col' not found in import file. Available columns: id, name, email, age", | ||
| }, | ||
| { | ||
| name: "mix of valid and invalid primary keys", | ||
| primaryKeys: []string{"id", "invalid_col1", "name", "invalid_col2"}, | ||
| schema: testSchema, | ||
| expectError: true, | ||
| errorContains: "primary keys [invalid_col1 invalid_col2] not found in import file. Available columns: id, name, email, age", | ||
| }, | ||
| { | ||
| name: "all invalid primary keys", | ||
| primaryKeys: []string{"col1", "col2", "col3"}, | ||
| schema: testSchema, | ||
| expectError: true, | ||
| errorContains: "primary keys [col1 col2 col3] not found in import file. Available columns: id, name, email, age", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := validatePrimaryKeysAgainstSchema(tt.primaryKeys, tt.schema, rowconv.NameMapper{}) | ||
|
|
||
| if tt.expectError { | ||
| assert.Error(t, err) | ||
| if tt.errorContains != "" { | ||
| assert.Contains(t, err.Error(), tt.errorContains) | ||
| } | ||
| } else { | ||
| assert.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestValidatePrimaryKeysAgainstSchemaColumnOrder(t *testing.T) { | ||
| // Test that available columns are listed in a consistent order | ||
| testCols := []schema.Column{ | ||
| {Name: "zebra", Tag: 3, IsPartOfPK: false}, | ||
| {Name: "alpha", Tag: 0, IsPartOfPK: true}, | ||
| {Name: "beta", Tag: 1, IsPartOfPK: false}, | ||
| {Name: "gamma", Tag: 2, IsPartOfPK: false}, | ||
| } | ||
| testSchema, err := schema.NewSchema( | ||
| schema.NewColCollection(testCols...), | ||
| nil, // pkOrdinals - nil means use default ordinals based on IsPartOfPK | ||
| schema.Collation_Default, | ||
| nil, // indexes | ||
| nil, // checks | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| err = validatePrimaryKeysAgainstSchema([]string{"invalid"}, testSchema, rowconv.NameMapper{}) | ||
| assert.Error(t, err) | ||
| // The implementation returns a detailed error with available columns | ||
| assert.Contains(t, err.Error(), "primary key 'invalid' not found in import file. Available columns: zebra, alpha, beta, gamma") | ||
| } | ||
|
|
||
| func TestValidatePrimaryKeysWithNameMapping(t *testing.T) { | ||
| // Create a test schema with columns: user_id, user_name, user_email | ||
| testCols := []schema.Column{ | ||
| {Name: "user_id", Tag: 0, IsPartOfPK: true}, | ||
| {Name: "user_name", Tag: 1, IsPartOfPK: false}, | ||
| {Name: "user_email", Tag: 2, IsPartOfPK: false}, | ||
| } | ||
| testSchema, err := schema.NewSchema( | ||
| schema.NewColCollection(testCols...), | ||
| nil, | ||
| schema.Collation_Default, | ||
| nil, | ||
| nil, | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| // Create a name mapper that maps user_id -> id, user_name -> name, user_email -> email | ||
| nameMapper := rowconv.NameMapper{ | ||
| "user_id": "id", | ||
| "user_name": "name", | ||
| "user_email": "email", | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| primaryKeys []string | ||
| expectError bool | ||
| }{ | ||
| { | ||
| name: "primary key using mapped name", | ||
| primaryKeys: []string{"id"}, // mapped from user_id | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "primary key using original name", | ||
| primaryKeys: []string{"user_id"}, // original column name | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "multiple primary keys with mixed names", | ||
| primaryKeys: []string{"id", "name"}, // mapped names | ||
| expectError: false, | ||
| }, | ||
| { | ||
| name: "invalid primary key not in mapping", | ||
| primaryKeys: []string{"invalid_col"}, | ||
| expectError: true, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| err := validatePrimaryKeysAgainstSchema(tt.primaryKeys, testSchema, nameMapper) | ||
| if tt.expectError { | ||
| assert.Error(t, err) | ||
| } else { | ||
| assert.NoError(t, err) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.