Tools for dumping/loading a SQLite database to diffable directory structure
Project description
sqlite-diffable
Tools for dumping/loading a SQLite database to diffable directory structure
Installation
pip install sqlite-diffable
Demo
The repository at simonw/simonwillisonblog-backup contains a backup of the database on my blog, https://simonwillison.net/ - created using this tool.
Dumping a database
Given a SQLite database called fixtures.db containing a table facetable, the following will dump out that table to the dump/ directory:
sqlite-diffable dump fixtures.db dump/ facetable
To dump out every table in that database, use --all:
sqlite-diffable dump fixtures.db dump/ --all
To dump all table except some specific ones, use --exclude one or more times:
sqlite-diffable dump fixtures.db dump/ --all \
--exclude unwanted_first_table \
--exclude unwanted_second_table
Loading a database
To load a previously dumped database, run the following:
sqlite-diffable load restored.db dump/
This will show an error if any of the tables that are being restored already exist in the database file.
You can replace those tables (dropping them before restoring them) using the --replace option:
sqlite-diffable load restored.db dump/ --replace
Converting to JSON objects
Table rows are stored in the .ndjson files as newline-delimited JSON arrays, like this:
["a", "a", "a-a", 63, null, 0.7364712141640124, "$null"]
["a", "b", "a-b", 51, null, 0.6020187290499803, "$null"]
Sometimes it can be more convenient to work with a list of JSON objects.
The sqlite-diffable objects command can read a .ndjson file and its accompanying .metadata.json file and output JSON objects to standard output:
sqlite-diffable objects fixtures.db dump/sortable.ndjson
The output of that command looks something like this:
{"pk1": "a", "pk2": "a", "content": "a-a", "sortable": 63, "sortable_with_nulls": null, "sortable_with_nulls_2": 0.7364712141640124, "text": "$null"}
{"pk1": "a", "pk2": "b", "content": "a-b", "sortable": 51, "sortable_with_nulls": null, "sortable_with_nulls_2": 0.6020187290499803, "text": "$null"}
Add -o to write that output to a file:
sqlite-diffable objects fixtures.db dump/sortable.ndjson -o output.txt
Add --array to output a JSON array of objects, as opposed to a newline-delimited file:
sqlite-diffable objects fixtures.db dump/sortable.ndjson --array
Output:
[
{"pk1": "a", "pk2": "a", "content": "a-a", "sortable": 63, "sortable_with_nulls": null, "sortable_with_nulls_2": 0.7364712141640124, "text": "$null"},
{"pk1": "a", "pk2": "b", "content": "a-b", "sortable": 51, "sortable_with_nulls": null, "sortable_with_nulls_2": 0.6020187290499803, "text": "$null"}
]
Storage format
Each table is represented as two files. The first, table_name.metadata.json, contains metadata describing the structure of the table. For a table called redirects_redirect that file might look like this:
{
"name": "redirects_redirect",
"columns": [
"id",
"domain",
"path",
"target",
"created"
],
"schema": "CREATE TABLE [redirects_redirect] (\n [id] INTEGER PRIMARY KEY,\n [domain] TEXT,\n [path] TEXT,\n [target] TEXT,\n [created] TEXT\n)"
}
It is an object with three keys: name is the name of the table, columns is an array of column strings and schema is the SQL schema text used for tha table.
The second file, table_name.ndjson, contains newline-delimited JSON (aka JSON Lines) for every row in the table. Each row is represented as a JSON array with items corresponding to each of the columns defined in the metadata.
That file for the redirects_redirect.ndjson table might look like this:
[1, "feeds.simonwillison.net", "swn-everything", "https://simonwillison.net/atom/everything/", "2017-10-01T21:11:36.440537+00:00"]
[2, "feeds.simonwillison.net", "swn-entries", "https://simonwillison.net/atom/entries/", "2017-10-01T21:12:32.478849+00:00"]
[3, "feeds.simonwillison.net", "swn-links", "https://simonwillison.net/atom/links/", "2017-10-01T21:12:54.820729+00:00"]
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlite_diffable-0.6.tar.gz.
File metadata
- Download URL: sqlite_diffable-0.6.tar.gz
- Upload date:
- Size: 9.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
deda0e4b2ceb9667479c1fefbdd7fa3acaaa3e8f5afac3ef97cfc06569381128
|
|
| MD5 |
eb77f21c749910c87c94f342ccc4b7d6
|
|
| BLAKE2b-256 |
deee4d81ac96671980d0b5d193f56a515ad22ad9196a3258490636014794dc6f
|
Provenance
The following attestation bundles were made for sqlite_diffable-0.6.tar.gz:
Publisher:
publish.yml on simonw/sqlite-diffable
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlite_diffable-0.6.tar.gz -
Subject digest:
deda0e4b2ceb9667479c1fefbdd7fa3acaaa3e8f5afac3ef97cfc06569381128 - Sigstore transparency entry: 206758463
- Sigstore integration time:
-
Permalink:
simonw/sqlite-diffable@dfe290c982bfc5a94bb1df199badde5a931606a1 -
Branch / Tag:
refs/tags/0.6 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dfe290c982bfc5a94bb1df199badde5a931606a1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file sqlite_diffable-0.6-py3-none-any.whl.
File metadata
- Download URL: sqlite_diffable-0.6-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1eb475778a53a727ac369ef75cc89001f5242e4df913794023791ee613552f24
|
|
| MD5 |
ec67d39b09a65ac1865dd2709b6422bd
|
|
| BLAKE2b-256 |
ea28b057ee5f0804f01e4b6156d631c93b2b2f894483bbc9a8688ad11cf88ad0
|
Provenance
The following attestation bundles were made for sqlite_diffable-0.6-py3-none-any.whl:
Publisher:
publish.yml on simonw/sqlite-diffable
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sqlite_diffable-0.6-py3-none-any.whl -
Subject digest:
1eb475778a53a727ac369ef75cc89001f5242e4df913794023791ee613552f24 - Sigstore transparency entry: 206758464
- Sigstore integration time:
-
Permalink:
simonw/sqlite-diffable@dfe290c982bfc5a94bb1df199badde5a931606a1 -
Branch / Tag:
refs/tags/0.6 - Owner: https://github.com/simonw
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@dfe290c982bfc5a94bb1df199badde5a931606a1 -
Trigger Event:
release
-
Statement type: