Repository navigation
Add Cloudflare D1 as a database source - #227
Merged
Merged
Conversation
Backs up and restores the D1 databases of a Cloudflare account through the REST API of Cloudflare, with an API token from a Token credential profile. The export is polled and its SQL dump stored unchanged. A restore into an existing database puts the drops ahead of the dump in one import, so a failed import leaves the database as it was, and logs the Time Travel bookmark from before. A new name creates the database in the jurisdiction of the source or where the connection says. A check before the export warns about integers beyond 2^53, which the export of Cloudflare rounds.
With cloudflareD1 in the lab config the seed fills the database dbackup-lab-shop of the test account with the SQLite dataset and adds the source Lab Cloudflare D1 and a job to Local, which lab:check tests and runs like the other lab connections.
The family cloud backs up a database of a Cloudflare test account, restores it under a new name and over a changed copy, and compares both through the API of Cloudflare. It reads the account from E2E_D1_ACCOUNT_ID and E2E_D1_TOKEN or the lab config and skips without one, and only its CI job gets the token.
A restore streams the drops ahead of the dump into the upload instead of writing a second file, so no decrypted copy lands in the temp folder. A stored token no longer follows a change of the account ID. The Database Explorer reads at most 16 MB per page and shows BLOBs cut to 1 KB, polls are tried again with a pause after a failure on the way, and a database a restore created stays when Cloudflare may still finish the import. URLs in the messages of Cloudflare are left out of the run log.
Compose up without a service starts every service of the project, so the family cloud started the whole lab on the runner beside the build of the image.
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
The export of Cloudflare writes a NUL character of a text raw into the dump, and the import stops reading at it and still reports success, so a restore writes such a character as char(0). Every import now ends with a check table, and a restore that did not run to the end fails instead of reporting success. The check before a backup also names values too long for the import of D1, the code 7500 no longer reads as a missing permission, and the guide gives the measured lock times.
Tests for an API that times out, breaks off or answers too much, the bound of the database list, ping and the job form, the value check, failed exports, downloads and uploads, where a restore puts a new database, the order of the drops and the restore of earlier versions. The unused batch and getDatabase helpers are gone.
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
DBackup could not back up Cloudflare D1. D1 runs only at Cloudflare, so there is no server to reach with a dump tool, and the only backup Cloudflare offers lives inside the same account.
Solution
A new database source, Cloudflare D1, that works through the REST API of Cloudflare alone, with an API token from a credential profile of the type Token. The connection is a Cloudflare account, and a job picks one, several or all of its D1 databases by name. Each database is exported by Cloudflare as a SQL dump, which DBackup stores unchanged as one entry of the archive. A restore replaces an existing database in place, so its ID and the Worker bindings stay, or creates a new one under a new name.
Details:
cloudflareD1in the lab config the seed fillsdbackup-lab-shopin the test account and adds the source and a job to Local.cloudagainst a test account at Cloudflare. Only its CI job gets the token (E2E_D1_TOKEN,E2E_D1_ACCOUNT_ID), and without one the test skips.Testing
pnpm validateandpnpm run buildpass, with unit tests for the API client, backup, restore, the Explorer and the address of stored secrets against a stand-in for Cloudflare.pnpm lab:seed,pnpm lab:checkand the lab job through the real runner.E2E_FAMILIES=cloud pnpm test:e2epassed on the lab before and after the changes from the security review.