dsqlite 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dsqlite-0.1.0/LICENSE +21 -0
- dsqlite-0.1.0/PKG-INFO +205 -0
- dsqlite-0.1.0/README.md +181 -0
- dsqlite-0.1.0/pyproject.toml +86 -0
- dsqlite-0.1.0/pyproject.toml.orig +73 -0
- dsqlite-0.1.0/src/dsqlite/__init__.py +24 -0
- dsqlite-0.1.0/src/dsqlite/_bucket.py +227 -0
- dsqlite-0.1.0/src/dsqlite/_local.py +255 -0
- dsqlite-0.1.0/src/dsqlite/_sql.py +389 -0
- dsqlite-0.1.0/src/dsqlite/_sync.py +384 -0
- dsqlite-0.1.0/src/dsqlite/_types.py +151 -0
- dsqlite-0.1.0/src/dsqlite/_uuid.py +30 -0
- dsqlite-0.1.0/src/dsqlite/py.typed +0 -0
dsqlite-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 James Abel
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
dsqlite-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: dsqlite
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Row-level last-write-wins sync of a local SQLite database through an S3 bucket
|
|
5
|
+
Keywords: sqlite,sync,s3,replication,last-write-wins,local-first
|
|
6
|
+
Author: James Abel
|
|
7
|
+
Author-email: James Abel <j@abel.co>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
Classifier: Operating System :: OS Independent
|
|
18
|
+
Requires-Dist: msqlite>=1.3.0
|
|
19
|
+
Requires-Dist: awsimple>=9.5.0
|
|
20
|
+
Requires-Python: >=3.12
|
|
21
|
+
Project-URL: Source, https://github.com/jamesabel/dsqlite
|
|
22
|
+
Project-URL: Changelog, https://github.com/jamesabel/dsqlite/blob/main/CHANGELOG.md
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+

|
|
26
|
+

|
|
27
|
+

|
|
28
|
+

|
|
29
|
+

|
|
30
|
+

|
|
31
|
+
|
|
32
|
+
# dsqlite
|
|
33
|
+
|
|
34
|
+
Row-level last-write-wins sync of a local SQLite database through an S3 bucket, built on
|
|
35
|
+
[msqlite](https://github.com/jamesabel/msqlite) and [awsimple](https://github.com/jamesabel/awsimple).
|
|
36
|
+
No service runs anywhere: the bucket is the only thing the replicas share.
|
|
37
|
+
|
|
38
|
+
- **Local first.** Your reads and writes touch only the local file. They never wait for the network and work offline.
|
|
39
|
+
- **Converges.** Every replica ends up with the same rows.
|
|
40
|
+
- **Newest write wins**, per row. Writes are ordered by a hybrid logical clock, with ties broken by replica id.
|
|
41
|
+
- **Atomic.** A local transaction is pushed whole, and a peer applies each pushed object in one transaction.
|
|
42
|
+
- **Crash safe.** A crash loses no committed local change, and bucket objects are written once and never changed.
|
|
43
|
+
- **History.** Every write of every row is kept, so an earlier state of a row can be restored.
|
|
44
|
+
|
|
45
|
+
## Quick start
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
from msqlite import Database
|
|
49
|
+
from dsqlite import Sync
|
|
50
|
+
|
|
51
|
+
db = Database("app.sqlite")
|
|
52
|
+
with db.write() as tx:
|
|
53
|
+
tx.execute("CREATE TABLE IF NOT EXISTS files (path TEXT PRIMARY KEY, size INTEGER, digest BLOB)")
|
|
54
|
+
|
|
55
|
+
sync = Sync(db, bucket="my-bucket", name="app", tables=["files"]) # no network request yet
|
|
56
|
+
|
|
57
|
+
with db.write() as tx: # use the database exactly as before
|
|
58
|
+
tx.execute("INSERT INTO files VALUES ('a.txt', 12, NULL)")
|
|
59
|
+
|
|
60
|
+
result = sync.sync() # push local changes, pull and apply the other replicas'
|
|
61
|
+
print(result.pushed, result.applied)
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
To keep syncing in the background:
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
with sync: # or sync.start() ... sync.stop()
|
|
68
|
+
... # local writes are pushed about a second later; peers' pushes arrive by notification
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Run the same code against the same bucket and `name` on each machine. The bucket must already exist.
|
|
72
|
+
|
|
73
|
+
A complete application, a todo list in the system tray shared between computers, is in [example/](example/README.md).
|
|
74
|
+
|
|
75
|
+
## How it works
|
|
76
|
+
|
|
77
|
+
`Sync` installs triggers on each synced table. A trigger stamps each row write and records it, with the full row,
|
|
78
|
+
in dsqlite's own tables inside your database (all named `dsqlite_...`). The triggers are plain SQL, so writes made
|
|
79
|
+
through any connection are captured, including ones that do not go through msqlite.
|
|
80
|
+
|
|
81
|
+
A push uploads the recorded local changes as one compressed JSON object. A pull lists each peer's objects after the
|
|
82
|
+
last one applied and applies them: a change is written to its table only if its stamp is greater than the row's
|
|
83
|
+
current stamp, and is added to the history either way.
|
|
84
|
+
|
|
85
|
+
```
|
|
86
|
+
<name>/replicas/<replica id>.json.gz one per replica, so peers can find each other
|
|
87
|
+
<name>/changes/<replica id>/<first seq>.json.gz one per push
|
|
88
|
+
<name>/snapshots/<time>-<replica id>.json.gz full state, for starting a new replica
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Change objects are never deleted, so the bucket holds the complete history. A snapshot is uploaded every
|
|
92
|
+
`snapshot_every` change objects (default 1000, counted across runs of the application) or by `sync.snapshot()`; a new replica starts from the newest snapshot
|
|
93
|
+
and reads only the change objects after it.
|
|
94
|
+
|
|
95
|
+
## History
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
for change in sync.history("files", "a.txt"): # oldest first; a tuple for a composite primary key
|
|
99
|
+
print(change.stamp, change.op, change.row)
|
|
100
|
+
|
|
101
|
+
sync.restore("files", "a.txt", change.stamp) # writes that state back as a new local write, which then syncs
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
A replica that started from a snapshot holds history from that point on. `sync.history(..., full=True)` also reads
|
|
105
|
+
every change object in the bucket.
|
|
106
|
+
|
|
107
|
+
## Notifications and AWS permissions
|
|
108
|
+
|
|
109
|
+
While the background thread runs, each push is announced through awsimple `Pub`/`Sub`, so peers pull within
|
|
110
|
+
seconds instead of waiting for the next poll (`poll_s`, default 60). This uses SNS, SQS and a DynamoDB table in
|
|
111
|
+
addition to S3, and the credentials need access to them. With `Sync(..., notifications=False)` only S3 is used
|
|
112
|
+
(`s3:GetObject`, `s3:PutObject`, `s3:ListBucket`, `s3:GetBucketLocation`) and peers are found by polling.
|
|
113
|
+
|
|
114
|
+
Credentials come from the usual AWS configuration, or pass `profile_name`, `aws_access_key_id`,
|
|
115
|
+
`aws_secret_access_key` and `region_name`.
|
|
116
|
+
|
|
117
|
+
`sync.wake()` asks the background thread to sync now instead of at its next poll, for a "sync now" button. It
|
|
118
|
+
returns at once.
|
|
119
|
+
|
|
120
|
+
`sync.status` says how the background syncing is doing, for a status line: whether it is running and syncing right
|
|
121
|
+
now, when it last synced and what made it sync then (its start, the poll interval, a notification, a local change
|
|
122
|
+
or `wake()`), whether local changes are waiting to be pushed, and what the problem is if there is one (offline or
|
|
123
|
+
refused for now, which is retried, or the fault that stopped it).
|
|
124
|
+
|
|
125
|
+
## Usage and cost
|
|
126
|
+
|
|
127
|
+
`sync.usage` is what the `Sync` has used of S3 that AWS charges for, since it was created, on every thread:
|
|
128
|
+
|
|
129
|
+
```python
|
|
130
|
+
usage = sync.usage
|
|
131
|
+
usage.uploads, usage.uploaded_bytes # objects written, and their (compressed) size
|
|
132
|
+
usage.downloads, usage.downloaded_bytes # objects read, and their size: what data transfer out is charged for
|
|
133
|
+
usage.tier1_requests # requests priced as PUT/LIST: the uploads and the bucket listings
|
|
134
|
+
usage.tier2_requests # requests priced as GET: the downloads and the checks of what was written
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
It has no prices, which differ by region and change, and it does not count the notifications (SNS, SQS and
|
|
138
|
+
DynamoDB). Two `Usage` values add up with `+`.
|
|
139
|
+
|
|
140
|
+
`sync.stored()` asks S3 what the database holds in the bucket, from every replica, which is what AWS charges storage
|
|
141
|
+
for:
|
|
142
|
+
|
|
143
|
+
```python
|
|
144
|
+
stored = sync.stored()
|
|
145
|
+
stored.objects, stored.size # the number of objects, and their (compressed) size in bytes
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
The [todo example](example/README.md) turns both into estimated costs.
|
|
149
|
+
|
|
150
|
+
## Errors
|
|
151
|
+
|
|
152
|
+
- Offline: `sync()` raises the botocore error; the background thread logs a warning and tries again next cycle.
|
|
153
|
+
It does the same when AWS refuses a request for a reason that passes: throttling, a server error, or credentials
|
|
154
|
+
that have expired (renew them and syncing carries on). Any other refusal, such as access denied, ends the thread.
|
|
155
|
+
- `Sync.raise_if_failed()` and `Sync.stop()` raise whatever ended the background thread; `Sync.failure` returns it.
|
|
156
|
+
- `ReplicaIdConflictError`: two databases share a replica id, because a database file was copied. Call
|
|
157
|
+
`sync.new_replica_id()` once on the copy.
|
|
158
|
+
- A remote change that violates a local constraint raises `sqlite3.IntegrityError` and nothing from that object is
|
|
159
|
+
applied.
|
|
160
|
+
|
|
161
|
+
## Limits
|
|
162
|
+
|
|
163
|
+
- Each synced table needs a declared primary key, and REAL values cannot be primary key values.
|
|
164
|
+
- Choose primary keys that two machines cannot produce independently for different things. dsqlite treats rows with
|
|
165
|
+
the same primary key as the same row, on every replica. With an auto-increment integer key, two machines that
|
|
166
|
+
each insert a row while out of sync both get, say, id 7 for unrelated rows; when they sync, the newer row
|
|
167
|
+
replaces the older one and one of the two inserts is lost. Use a key that is unique wherever it is created:
|
|
168
|
+
a version 7 UUID, from `dsqlite.uuid7()` (for example `str(uuid7())` in a TEXT column), which also starts with
|
|
169
|
+
the time, so new rows sit together in the index. A UUID key also keeps a row's history in one piece: history
|
|
170
|
+
follows the primary key, so if a value that can change (a file's path, say) is the key, a rename becomes a delete
|
|
171
|
+
of one row and the creation of another, and the history is split between them. Keep such values in an ordinary
|
|
172
|
+
indexed column instead.
|
|
173
|
+
- Do not make that index `UNIQUE`. Two machines can each create a row with the same value (the same path) while
|
|
174
|
+
out of sync; with a unique index the second one fails to apply on arrival. The application decides what two such
|
|
175
|
+
rows mean, for example by keeping the newer one.
|
|
176
|
+
- A natural key is right only where the same key always means the same row and never changes, such as the name
|
|
177
|
+
of a setting.
|
|
178
|
+
- The whole row is replaced: two machines editing different columns of one row do not merge.
|
|
179
|
+
- The schema of a synced table must be the same on every replica. Changes from a replica with a different schema
|
|
180
|
+
are held (reported in `SyncResult.held` and as a warning) until the local schema matches; create a new `Sync`
|
|
181
|
+
after changing the schema.
|
|
182
|
+
- Constraints other than the primary key are local. There are no transactions or constraints across machines.
|
|
183
|
+
- A row that `INSERT OR REPLACE`, `UPDATE OR REPLACE` or `REPLACE` deletes because the new row has its unique values
|
|
184
|
+
is recorded as deleted. The exception is a unique index that is partial or on an expression: a row deleted through
|
|
185
|
+
one of those is not seen as a delete unless `PRAGMA recursive_triggers` is on.
|
|
186
|
+
- A table that is left out of `tables` is no longer recorded from then on. Its earlier history stays in the database.
|
|
187
|
+
- A table that already has rows when `Sync` is first attached has them recorded as written at that moment.
|
|
188
|
+
- Meant for 2 to around 20 replicas, databases up to a few GB and bursty writes, not sustained high write rates.
|
|
189
|
+
- Requires Python 3.12 or later, SQLite 3.33 or later, and a UTF-8 database.
|
|
190
|
+
|
|
191
|
+
## Development
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
uv sync
|
|
195
|
+
uv run pytest
|
|
196
|
+
uv run pytest test_dsqlite/test_sync.py::test_a_write_reaches_the_other_replica
|
|
197
|
+
uv run ruff check src test_dsqlite example
|
|
198
|
+
uv run ruff format src test_dsqlite example
|
|
199
|
+
uv run mypy
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
Tests run against [moto](https://github.com/getmoto/moto), so they need no AWS account. Set
|
|
203
|
+
`AWSIMPLE_USE_MOTO_MOCK=0` to run them against real AWS.
|
|
204
|
+
|
|
205
|
+
MIT License.
|
dsqlite-0.1.0/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+

|
|
2
|
+

|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
# dsqlite
|
|
9
|
+
|
|
10
|
+
Row-level last-write-wins sync of a local SQLite database through an S3 bucket, built on
|
|
11
|
+
[msqlite](https://github.com/jamesabel/msqlite) and [awsimple](https://github.com/jamesabel/awsimple).
|
|
12
|
+
No service runs anywhere: the bucket is the only thing the replicas share.
|
|
13
|
+
|
|
14
|
+
- **Local first.** Your reads and writes touch only the local file. They never wait for the network and work offline.
|
|
15
|
+
- **Converges.** Every replica ends up with the same rows.
|
|
16
|
+
- **Newest write wins**, per row. Writes are ordered by a hybrid logical clock, with ties broken by replica id.
|
|
17
|
+
- **Atomic.** A local transaction is pushed whole, and a peer applies each pushed object in one transaction.
|
|
18
|
+
- **Crash safe.** A crash loses no committed local change, and bucket objects are written once and never changed.
|
|
19
|
+
- **History.** Every write of every row is kept, so an earlier state of a row can be restored.
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from msqlite import Database
|
|
25
|
+
from dsqlite import Sync
|
|
26
|
+
|
|
27
|
+
db = Database("app.sqlite")
|
|
28
|
+
with db.write() as tx:
|
|
29
|
+
tx.execute("CREATE TABLE IF NOT EXISTS files (path TEXT PRIMARY KEY, size INTEGER, digest BLOB)")
|
|
30
|
+
|
|
31
|
+
sync = Sync(db, bucket="my-bucket", name="app", tables=["files"]) # no network request yet
|
|
32
|
+
|
|
33
|
+
with db.write() as tx: # use the database exactly as before
|
|
34
|
+
tx.execute("INSERT INTO files VALUES ('a.txt', 12, NULL)")
|
|
35
|
+
|
|
36
|
+
result = sync.sync() # push local changes, pull and apply the other replicas'
|
|
37
|
+
print(result.pushed, result.applied)
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
To keep syncing in the background:
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
with sync: # or sync.start() ... sync.stop()
|
|
44
|
+
... # local writes are pushed about a second later; peers' pushes arrive by notification
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Run the same code against the same bucket and `name` on each machine. The bucket must already exist.
|
|
48
|
+
|
|
49
|
+
A complete application, a todo list in the system tray shared between computers, is in [example/](example/README.md).
|
|
50
|
+
|
|
51
|
+
## How it works
|
|
52
|
+
|
|
53
|
+
`Sync` installs triggers on each synced table. A trigger stamps each row write and records it, with the full row,
|
|
54
|
+
in dsqlite's own tables inside your database (all named `dsqlite_...`). The triggers are plain SQL, so writes made
|
|
55
|
+
through any connection are captured, including ones that do not go through msqlite.
|
|
56
|
+
|
|
57
|
+
A push uploads the recorded local changes as one compressed JSON object. A pull lists each peer's objects after the
|
|
58
|
+
last one applied and applies them: a change is written to its table only if its stamp is greater than the row's
|
|
59
|
+
current stamp, and is added to the history either way.
|
|
60
|
+
|
|
61
|
+
```
|
|
62
|
+
<name>/replicas/<replica id>.json.gz one per replica, so peers can find each other
|
|
63
|
+
<name>/changes/<replica id>/<first seq>.json.gz one per push
|
|
64
|
+
<name>/snapshots/<time>-<replica id>.json.gz full state, for starting a new replica
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Change objects are never deleted, so the bucket holds the complete history. A snapshot is uploaded every
|
|
68
|
+
`snapshot_every` change objects (default 1000, counted across runs of the application) or by `sync.snapshot()`; a new replica starts from the newest snapshot
|
|
69
|
+
and reads only the change objects after it.
|
|
70
|
+
|
|
71
|
+
## History
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
for change in sync.history("files", "a.txt"): # oldest first; a tuple for a composite primary key
|
|
75
|
+
print(change.stamp, change.op, change.row)
|
|
76
|
+
|
|
77
|
+
sync.restore("files", "a.txt", change.stamp) # writes that state back as a new local write, which then syncs
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
A replica that started from a snapshot holds history from that point on. `sync.history(..., full=True)` also reads
|
|
81
|
+
every change object in the bucket.
|
|
82
|
+
|
|
83
|
+
## Notifications and AWS permissions
|
|
84
|
+
|
|
85
|
+
While the background thread runs, each push is announced through awsimple `Pub`/`Sub`, so peers pull within
|
|
86
|
+
seconds instead of waiting for the next poll (`poll_s`, default 60). This uses SNS, SQS and a DynamoDB table in
|
|
87
|
+
addition to S3, and the credentials need access to them. With `Sync(..., notifications=False)` only S3 is used
|
|
88
|
+
(`s3:GetObject`, `s3:PutObject`, `s3:ListBucket`, `s3:GetBucketLocation`) and peers are found by polling.
|
|
89
|
+
|
|
90
|
+
Credentials come from the usual AWS configuration, or pass `profile_name`, `aws_access_key_id`,
|
|
91
|
+
`aws_secret_access_key` and `region_name`.
|
|
92
|
+
|
|
93
|
+
`sync.wake()` asks the background thread to sync now instead of at its next poll, for a "sync now" button. It
|
|
94
|
+
returns at once.
|
|
95
|
+
|
|
96
|
+
`sync.status` says how the background syncing is doing, for a status line: whether it is running and syncing right
|
|
97
|
+
now, when it last synced and what made it sync then (its start, the poll interval, a notification, a local change
|
|
98
|
+
or `wake()`), whether local changes are waiting to be pushed, and what the problem is if there is one (offline or
|
|
99
|
+
refused for now, which is retried, or the fault that stopped it).
|
|
100
|
+
|
|
101
|
+
## Usage and cost
|
|
102
|
+
|
|
103
|
+
`sync.usage` is what the `Sync` has used of S3 that AWS charges for, since it was created, on every thread:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
usage = sync.usage
|
|
107
|
+
usage.uploads, usage.uploaded_bytes # objects written, and their (compressed) size
|
|
108
|
+
usage.downloads, usage.downloaded_bytes # objects read, and their size: what data transfer out is charged for
|
|
109
|
+
usage.tier1_requests # requests priced as PUT/LIST: the uploads and the bucket listings
|
|
110
|
+
usage.tier2_requests # requests priced as GET: the downloads and the checks of what was written
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
It has no prices, which differ by region and change, and it does not count the notifications (SNS, SQS and
|
|
114
|
+
DynamoDB). Two `Usage` values add up with `+`.
|
|
115
|
+
|
|
116
|
+
`sync.stored()` asks S3 what the database holds in the bucket, from every replica, which is what AWS charges storage
|
|
117
|
+
for:
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
stored = sync.stored()
|
|
121
|
+
stored.objects, stored.size # the number of objects, and their (compressed) size in bytes
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
The [todo example](example/README.md) turns both into estimated costs.
|
|
125
|
+
|
|
126
|
+
## Errors
|
|
127
|
+
|
|
128
|
+
- Offline: `sync()` raises the botocore error; the background thread logs a warning and tries again next cycle.
|
|
129
|
+
It does the same when AWS refuses a request for a reason that passes: throttling, a server error, or credentials
|
|
130
|
+
that have expired (renew them and syncing carries on). Any other refusal, such as access denied, ends the thread.
|
|
131
|
+
- `Sync.raise_if_failed()` and `Sync.stop()` raise whatever ended the background thread; `Sync.failure` returns it.
|
|
132
|
+
- `ReplicaIdConflictError`: two databases share a replica id, because a database file was copied. Call
|
|
133
|
+
`sync.new_replica_id()` once on the copy.
|
|
134
|
+
- A remote change that violates a local constraint raises `sqlite3.IntegrityError` and nothing from that object is
|
|
135
|
+
applied.
|
|
136
|
+
|
|
137
|
+
## Limits
|
|
138
|
+
|
|
139
|
+
- Each synced table needs a declared primary key, and REAL values cannot be primary key values.
|
|
140
|
+
- Choose primary keys that two machines cannot produce independently for different things. dsqlite treats rows with
|
|
141
|
+
the same primary key as the same row, on every replica. With an auto-increment integer key, two machines that
|
|
142
|
+
each insert a row while out of sync both get, say, id 7 for unrelated rows; when they sync, the newer row
|
|
143
|
+
replaces the older one and one of the two inserts is lost. Use a key that is unique wherever it is created:
|
|
144
|
+
a version 7 UUID, from `dsqlite.uuid7()` (for example `str(uuid7())` in a TEXT column), which also starts with
|
|
145
|
+
the time, so new rows sit together in the index. A UUID key also keeps a row's history in one piece: history
|
|
146
|
+
follows the primary key, so if a value that can change (a file's path, say) is the key, a rename becomes a delete
|
|
147
|
+
of one row and the creation of another, and the history is split between them. Keep such values in an ordinary
|
|
148
|
+
indexed column instead.
|
|
149
|
+
- Do not make that index `UNIQUE`. Two machines can each create a row with the same value (the same path) while
|
|
150
|
+
out of sync; with a unique index the second one fails to apply on arrival. The application decides what two such
|
|
151
|
+
rows mean, for example by keeping the newer one.
|
|
152
|
+
- A natural key is right only where the same key always means the same row and never changes, such as the name
|
|
153
|
+
of a setting.
|
|
154
|
+
- The whole row is replaced: two machines editing different columns of one row do not merge.
|
|
155
|
+
- The schema of a synced table must be the same on every replica. Changes from a replica with a different schema
|
|
156
|
+
are held (reported in `SyncResult.held` and as a warning) until the local schema matches; create a new `Sync`
|
|
157
|
+
after changing the schema.
|
|
158
|
+
- Constraints other than the primary key are local. There are no transactions or constraints across machines.
|
|
159
|
+
- A row that `INSERT OR REPLACE`, `UPDATE OR REPLACE` or `REPLACE` deletes because the new row has its unique values
|
|
160
|
+
is recorded as deleted. The exception is a unique index that is partial or on an expression: a row deleted through
|
|
161
|
+
one of those is not seen as a delete unless `PRAGMA recursive_triggers` is on.
|
|
162
|
+
- A table that is left out of `tables` is no longer recorded from then on. Its earlier history stays in the database.
|
|
163
|
+
- A table that already has rows when `Sync` is first attached has them recorded as written at that moment.
|
|
164
|
+
- Meant for 2 to around 20 replicas, databases up to a few GB and bursty writes, not sustained high write rates.
|
|
165
|
+
- Requires Python 3.12 or later, SQLite 3.33 or later, and a UTF-8 database.
|
|
166
|
+
|
|
167
|
+
## Development
|
|
168
|
+
|
|
169
|
+
```
|
|
170
|
+
uv sync
|
|
171
|
+
uv run pytest
|
|
172
|
+
uv run pytest test_dsqlite/test_sync.py::test_a_write_reaches_the_other_replica
|
|
173
|
+
uv run ruff check src test_dsqlite example
|
|
174
|
+
uv run ruff format src test_dsqlite example
|
|
175
|
+
uv run mypy
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Tests run against [moto](https://github.com/getmoto/moto), so they need no AWS account. Set
|
|
179
|
+
`AWSIMPLE_USE_MOTO_MOCK=0` to run them against real AWS.
|
|
180
|
+
|
|
181
|
+
MIT License.
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.13.4,<0.14.0"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dsqlite"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Row-level last-write-wins sync of a local SQLite database through an S3 bucket"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
keywords = [
|
|
13
|
+
"sqlite",
|
|
14
|
+
"sync",
|
|
15
|
+
"s3",
|
|
16
|
+
"replication",
|
|
17
|
+
"last-write-wins",
|
|
18
|
+
"local-first",
|
|
19
|
+
]
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Development Status :: 3 - Alpha",
|
|
22
|
+
"Programming Language :: Python :: 3",
|
|
23
|
+
"Programming Language :: Python :: 3.12",
|
|
24
|
+
"Programming Language :: Python :: 3.13",
|
|
25
|
+
"Programming Language :: Python :: 3.14",
|
|
26
|
+
"Programming Language :: Python :: 3.15",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
"Operating System :: OS Independent",
|
|
29
|
+
]
|
|
30
|
+
requires-python = ">=3.12"
|
|
31
|
+
dependencies = [
|
|
32
|
+
"msqlite>=1.3.0",
|
|
33
|
+
"awsimple>=9.5.0",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
[[project.authors]]
|
|
37
|
+
name = "James Abel"
|
|
38
|
+
email = "j@abel.co"
|
|
39
|
+
|
|
40
|
+
[project.urls]
|
|
41
|
+
Source = "https://github.com/jamesabel/dsqlite"
|
|
42
|
+
Changelog = "https://github.com/jamesabel/dsqlite/blob/main/CHANGELOG.md"
|
|
43
|
+
|
|
44
|
+
[dependency-groups]
|
|
45
|
+
dev = [
|
|
46
|
+
"pytest",
|
|
47
|
+
"pytest-cov",
|
|
48
|
+
"moto[s3,sns,sqs,dynamodb]",
|
|
49
|
+
"ruff",
|
|
50
|
+
"mypy",
|
|
51
|
+
]
|
|
52
|
+
example = [
|
|
53
|
+
"balsa>=0.27.0",
|
|
54
|
+
"humanize>=4.17.0",
|
|
55
|
+
"ismain>=0.0.4",
|
|
56
|
+
"platformdirs>=4.14.9",
|
|
57
|
+
"pref>=0.5.0",
|
|
58
|
+
"pyside6-essentials>=6.12.1",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
[tool.ruff]
|
|
62
|
+
line-length = 192
|
|
63
|
+
target-version = "py312"
|
|
64
|
+
|
|
65
|
+
[tool.ruff.lint]
|
|
66
|
+
select = [
|
|
67
|
+
"E",
|
|
68
|
+
"F",
|
|
69
|
+
"W",
|
|
70
|
+
"I",
|
|
71
|
+
"UP",
|
|
72
|
+
"B",
|
|
73
|
+
"SIM",
|
|
74
|
+
"RUF",
|
|
75
|
+
"PYI",
|
|
76
|
+
]
|
|
77
|
+
ignore = ["E501"]
|
|
78
|
+
|
|
79
|
+
[tool.mypy]
|
|
80
|
+
files = ["src/dsqlite"]
|
|
81
|
+
python_version = "3.12"
|
|
82
|
+
strict = true
|
|
83
|
+
|
|
84
|
+
[[tool.mypy.overrides]]
|
|
85
|
+
module = ["botocore.*"]
|
|
86
|
+
ignore_missing_imports = true
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["uv_build>=0.13.4,<0.14.0"]
|
|
3
|
+
build-backend = "uv_build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "dsqlite"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Row-level last-write-wins sync of a local SQLite database through an S3 bucket"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
authors = [{ name = "James Abel", email = "j@abel.co" }]
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
13
|
+
keywords = ["sqlite", "sync", "s3", "replication", "last-write-wins", "local-first"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Development Status :: 3 - Alpha",
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"Programming Language :: Python :: 3.12",
|
|
18
|
+
"Programming Language :: Python :: 3.13",
|
|
19
|
+
"Programming Language :: Python :: 3.14",
|
|
20
|
+
"Programming Language :: Python :: 3.15",
|
|
21
|
+
"Typing :: Typed",
|
|
22
|
+
"Operating System :: OS Independent",
|
|
23
|
+
]
|
|
24
|
+
requires-python = ">=3.12"
|
|
25
|
+
dependencies = [
|
|
26
|
+
"msqlite>=1.3.0",
|
|
27
|
+
"awsimple>=9.5.0",
|
|
28
|
+
]
|
|
29
|
+
|
|
30
|
+
[project.urls]
|
|
31
|
+
Source = "https://github.com/jamesabel/dsqlite"
|
|
32
|
+
Changelog = "https://github.com/jamesabel/dsqlite/blob/main/CHANGELOG.md"
|
|
33
|
+
|
|
34
|
+
[dependency-groups]
|
|
35
|
+
dev = [
|
|
36
|
+
# testing
|
|
37
|
+
"pytest",
|
|
38
|
+
"pytest-cov",
|
|
39
|
+
# awsimple runs against moto when AWSIMPLE_USE_MOTO_MOCK=1 (see test_dsqlite/conftest.py); dynamodb is for Pub/Sub.
|
|
40
|
+
"moto[s3,sns,sqs,dynamodb]",
|
|
41
|
+
# linters
|
|
42
|
+
"ruff",
|
|
43
|
+
"mypy",
|
|
44
|
+
]
|
|
45
|
+
# For the example/todo package; install with `uv sync --group example` (not --no-build).
|
|
46
|
+
# Built from source: sqlitedict (needed by pref) and mttkinter (needed by balsa) publish no wheel. Both are pure Python
|
|
47
|
+
# and build with a plain install (verified on Windows, Python 3.15: sqlitedict 2026-12-02, mttkinter 2026-12-05).
|
|
48
|
+
# Revisit if a wheel appears.
|
|
49
|
+
example = [
|
|
50
|
+
"balsa>=0.27.0",
|
|
51
|
+
"humanize>=4.17.0",
|
|
52
|
+
"ismain>=0.0.4",
|
|
53
|
+
"platformdirs>=4.14.9",
|
|
54
|
+
"pref>=0.5.0",
|
|
55
|
+
"pyside6-essentials>=6.12.1",
|
|
56
|
+
]
|
|
57
|
+
|
|
58
|
+
[tool.ruff]
|
|
59
|
+
line-length = 192
|
|
60
|
+
target-version = "py312"
|
|
61
|
+
|
|
62
|
+
[tool.ruff.lint]
|
|
63
|
+
select = ["E", "F", "W", "I", "UP", "B", "SIM", "RUF", "PYI"]
|
|
64
|
+
ignore = ["E501"] # line length is the formatter's job
|
|
65
|
+
|
|
66
|
+
[tool.mypy]
|
|
67
|
+
files = ["src/dsqlite"]
|
|
68
|
+
python_version = "3.12"
|
|
69
|
+
strict = true
|
|
70
|
+
|
|
71
|
+
[[tool.mypy.overrides]]
|
|
72
|
+
module = ["botocore.*"] # ships no type information
|
|
73
|
+
ignore_missing_imports = true
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""dsqlite: row-level last-write-wins sync of a local SQLite database through an S3 bucket."""
|
|
2
|
+
|
|
3
|
+
from ._bucket import FormatError, ReplicaIdConflictError
|
|
4
|
+
from ._sync import Sync
|
|
5
|
+
from ._types import Change, DSQLiteError, HistoryError, SchemaMismatchError, Stamp, Stored, SyncResult, SyncStatus, TableError, Trigger, Usage
|
|
6
|
+
from ._uuid import uuid7
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"Change",
|
|
10
|
+
"DSQLiteError",
|
|
11
|
+
"FormatError",
|
|
12
|
+
"HistoryError",
|
|
13
|
+
"ReplicaIdConflictError",
|
|
14
|
+
"SchemaMismatchError",
|
|
15
|
+
"Stamp",
|
|
16
|
+
"Stored",
|
|
17
|
+
"Sync",
|
|
18
|
+
"SyncResult",
|
|
19
|
+
"SyncStatus",
|
|
20
|
+
"TableError",
|
|
21
|
+
"Trigger",
|
|
22
|
+
"Usage",
|
|
23
|
+
"uuid7",
|
|
24
|
+
]
|