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

Paste API #3222

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft

Paste API #3222

wants to merge 5 commits into from

Conversation

dmos62
Copy link
Contributor

@dmos62 dmos62 commented Sep 22, 2023

Fixes #2844

Implements paste API.

Initially we envisioned a generic bulk upsert API that could be used by the frontend paste functionality, hence the title of #2844. I explored that idea extensively, but opted for a non-generic API specifically for pasting.

In my estimate, the bulk upsert as expected by a user using paste in a spreadsheet is wildly different than the bulk upsert expected by database administrator doing INSERT ON CONFLICT DO UPDATE. Trying to accomodate both seemed like a good way to waste a lot of time and end up with bad UX for both.

Instead of summarizing my concerns, I'll summarize the features of the proposed paste API:

updates = [
	dict(
		changes=dict(
			Status='0',
		),
		conditionals=dict(
			id=record_0_id,
		),
	),
	dict(
		changes=dict(
			Status='1',
			Center='2'
		),
		conditionals=dict(
			id=record_1_id,
		),
	),
]
inserts = [
	dict(
		Status='3',
		Center='4',
	),
	dict(
		Status='5',
		Center='6',
	),
]
data = dict(
	updates=updates,
	inserts=inserts,
)
client.post(f'/api/ui/v0/tables/{table.id}/records/paste/', data=data)

Above is the basic outline of the API. The endpoint accepts (via POST) a dict, that has two fields: updates and inserts.

It's assumed that frontend will always know whether a given row being pasted should result in an update or an insert. Making that explicit prevents surprises in case of relevant changes having been made that were not synced with the calling frontend.

Updates are a sequence of dicts: each dict has a changes key and a conditionals key: changes define the field-value pairs to change in the row that is satisfied by the equality comparisons defined by field-value pairs in conditionals. Think how UPDATE works in SQL. An update with changes being {'field1':'val1'} and conditionals being {'id':'val2'} requests that for the single row whose id field equals val2, its field1 field's value is set to val1.

If more than one row would be updated by a given update, that's a fatal exception, because when pasting a row it should never result in more than one updated row. The paste API attempts to minimize surprises to the end user.

Inserts are a sequence of dicts: each dict defines the field-value pairs that should be set on the new resulting row.

An insert will always result in a new row or will panic if that's impossible for whatever reason.

Any panic encountered will rollback all changes made. Paste is meant to be an all-or-nothing operation.

Also, this API panics if any user-defined changes to SERIAL or IDENTITY columns are requested. These column types need special handling when mutating their values, because there are sequences involved, and that seems hard to safely generalize (i.e. prevent surprises).

All updates are performed before any inserts. All updates are performed in order requested; same for inserts.

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the develop branch of the repository
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no
    visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dmos62 dmos62 added this to the Next release milestone Sep 22, 2023
@dmos62 dmos62 added work: backend Related to Python, Django, and simple SQL status: draft labels Sep 22, 2023
@dmos62 dmos62 self-assigned this Sep 22, 2023
@seancolsen seancolsen modified the milestones: Next release, Backlog Oct 2, 2023
@seancolsen
Copy link
Contributor

@dmos62 I assume this PR is to fix #2844, is that right? If so, can you update the PR description?

@seancolsen
Copy link
Contributor

@dmos62 can you give an update one where you're at with this PR? How close to completion is it? What work is remaining?

@dmos62
Copy link
Contributor Author

dmos62 commented Oct 25, 2023

I'll update the top post with what the goal/spec of the PR is.

As to the progress, here's the summary, but keep in mind that I might not recall some details. The PR seems 80% complete. The SQL logic and tests seem to be there. The API tests are there for the update aspect of pasting. The API tests for the insert aspect of pasting are missing. The API tests for the paste operation being all-or-nothing are missing as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
work: backend Related to Python, Django, and simple SQL
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

API to create or update records in bulk
3 participants