Skip to content
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

Implement renameCollection command #2343

Merged
merged 159 commits into from May 8, 2023
Merged

Implement renameCollection command #2343

merged 159 commits into from May 8, 2023

Conversation

b1ron
Copy link
Contributor

@b1ron b1ron commented Apr 2, 2023

Description

Closes #1517.

See also FerretDB/dance#422 for the additional name-related diff tests.

Readiness checklist

  • I added/updated unit tests.
  • I added/updated integration/compatibility tests.
  • I added/updated comments and checked rendering.
  • I made spot refactorings.
  • I updated user documentation.
  • I ran task all, and it passed.
  • I ensured that PR title is good enough for the changelog.
  • (for maintainers only) I set Reviewers (@FerretDB/core), Assignee, Labels, Project and project's Sprint fields.
  • I marked all done items in this checklist.

@b1ron b1ron added the code/feature Some user-visible feature is not implemented yet label Apr 2, 2023
@b1ron b1ron self-assigned this Apr 2, 2023
@chilagrow chilagrow mentioned this pull request Apr 3, 2023
9 tasks
@codecov
Copy link

codecov bot commented Apr 3, 2023

Codecov Report

Merging #2343 (5e78acd) into main (050702e) will increase coverage by 0.10%.
The diff coverage is 76.53%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2343      +/-   ##
==========================================
+ Coverage   65.58%   65.69%   +0.10%     
==========================================
  Files         411      415       +4     
  Lines       20148    20343     +195     
==========================================
+ Hits        13215    13364     +149     
- Misses       5990     6027      +37     
- Partials      943      952       +9     
Impacted Files Coverage Δ
internal/handlers/common/msg_listcommands.go 100.00% <ø> (ø)
internal/handlers/commonerrors/error.go 70.00% <ø> (ø)
internal/handlers/commonerrors/errorcode_string.go 80.00% <ø> (ø)
internal/handlers/dummy/msg_renamecollection.go 0.00% <0.00%> (ø)
internal/handlers/hana/msg_renamecollection.go 0.00% <0.00%> (ø)
internal/handlers/tigris/msg_renamecollection.go 0.00% <0.00%> (ø)
internal/handlers/pg/pgdb/database_metadata.go 80.17% <69.49%> (-2.33%) ⬇️
internal/handlers/pg/msg_renamecollection.go 82.11% <82.11%> (ø)
internal/handlers/pg/pgdb/collections.go 70.00% <100.00%> (+2.14%) ⬆️

... and 1 file with indirect coverage changes

Flag Coverage Δ
integration 58.80% <76.53%> (+0.17%) ⬆️
mongodb 5.51% <0.00%> (+<0.01%) ⬆️
pg 58.70% <76.53%> (+0.17%) ⬆️
unit 25.59% <21.93%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

w84thesun
w84thesun previously approved these changes May 5, 2023
Copy link
Contributor

@w84thesun w84thesun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a lot of changes! Overall looks good! I added a minor misspelling fix.

internal/handlers/pg/pgdb/database_metadata.go Outdated Show resolved Hide resolved
Co-authored-by: Dmitry <dmitry.eremenko@ferretdb.io>
Copy link
Member

@noisersup noisersup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything seems to be covered and tests are awesome! Also nice idea with using indexes for table names in metadata. Let's add a comment about this index though to make it easy in the future to understand this code :)

break
}

tableName = fmt.Sprintf("%s_%d", defaultTableName, i)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we know is it possible to exceed the PostgreSQL table name limit by renaming and creating collection with a long name and renaming + recreating it multiple times? It doesn't sound like a serious issue although it's nice if we have it in mind :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test> db.runCommand({renameCollection:"test.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", to:"test.b" })
{ ok: 1 }
test> db.runCommand({renameCollection:"test.b", to:"test.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" })
MongoServerError: error with target namespace: Invalid collection name: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that we have different validation on rename than on create, exceeding the number is even not possible for a <235 x 'b' which wasn't inserted to the collection previously.

test> db.runCommand({renameCollection:"test.values2", to:"test.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" })
MongoServerError: error with target namespace: Invalid collection name: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

As this is very specific corner case we could fix that in followup either :)

Copy link
Member

@noisersup noisersup May 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test> db.runCommand({renameCollection:"test.values2", to:"test.bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" })
{ ok: 1 }

I was able to rename it to 63 character long collection name (max of pg table name), so we would probably need to reuse the same functions that are already used for collection creation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's possible. Ideally, in this case, we should remove some symbols from the table name... I think I'll create a separate PR about it, as otherwise, this one will be even bigger (especially, with tests).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do that in this issue (if not in this PR). That seems to be a serious problem

Copy link
Member

@noisersup noisersup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test> db.values.renameCollection("values2")
MongoServerError: renameCollection: support for field "dropTarget" with value false is not implemented yet
test> db.values.renameCollection("values2",true)
MongoServerError: renameCollection: support for field "dropTarget" with value true is not implemented yet
test> db.values.renameCollection("values2",false)
MongoServerError: renameCollection: support for field "dropTarget" with value false is not implemented yet
test>

It also seems that we are very strict in terms of dropTarget field. I understand it's not in the scope, but returning the error even if it's false make it impossible to use renameCollection by mongosh commands and one is forced to use runCommand.

As dropTarget is false by default and it's behavior when false is exactly the same as our current behavior (we don't drop target collection if it already exists), can we return error only if dropTarget is set to true?

If we want to prioritize merging of this PR we could even do that in followup PR

@rumyantseva
Copy link
Member

@noisersup nice catch!

internal/handlers/pg/pgdb/databases.go Outdated Show resolved Hide resolved
internal/handlers/commonerrors/error.go Outdated Show resolved Hide resolved
Copy link
Contributor

@chilagrow chilagrow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great 🤗 There are some linter warnings and unhappy tests. Let's tackle those. Aside from comments addressed by @noisersup and @AlekSi, looks good to me!

compatNSTo: targetDB.Name() + "." + strings.Repeat("aB", 150),
resultType: emptyResult,
altMessage: "error with target namespace: Invalid collection name: " + strings.Repeat("aB", 150),
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great coverage of tests 🤗 At the moment, some of them are unhappy, let's fix them.

@AlekSi AlekSi added the no ci label May 8, 2023
@AlekSi AlekSi removed the no ci label May 8, 2023
@AlekSi AlekSi disabled auto-merge May 8, 2023 19:26
@AlekSi AlekSi merged commit 0ffed38 into FerretDB:main May 8, 2023
25 checks passed
@AlekSi AlekSi added this to the v1.1.0 milestone May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code/feature Some user-visible feature is not implemented yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Implement renameCollection command
6 participants