Skip to content

Commit 3a297d2

Browse files
authored
Merge branch 'main' into last-vestiges-of-old-republic
2 parents d5fc8a6 + 64d0b7a commit 3a297d2

File tree

6 files changed

+40
-30
lines changed

6 files changed

+40
-30
lines changed

apswutils/db.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
# This file is from sqlite-utils and copyright and license is the same as that project
22
__all__ = ['Database', 'Queryable', 'Table', 'View']
33

4-
from .utils import ( chunks, hash_record, OperationalError, suggest_column_types, types_for_column_types, column_affinity, find_spatialite,)
5-
import binascii
4+
from .utils import chunks, hash_record, OperationalError, suggest_column_types, types_for_column_types, column_affinity, find_spatialite
65
from collections import namedtuple
76
from collections.abc import Mapping
8-
import contextlib, datetime, decimal, inspect, itertools, json, os, pathlib, re, secrets, textwrap
9-
from typing import ( cast, Any, Callable, Dict, Generator, Iterable, Union, Optional, List, Tuple,Iterator)
7+
from typing import cast, Any, Callable, Dict, Generator, Iterable, Union, Optional, List, Tuple, Iterator
108
from functools import cache
11-
import uuid
12-
import apsw
13-
import apsw.ext
14-
import apsw.bestpractice
9+
import contextlib, datetime, decimal, inspect, itertools, json, os, pathlib, re, secrets, textwrap, binascii, uuid, logging
10+
import apsw.ext, apsw.bestpractice
1511

16-
# We don't use apsw.bestpractice.connection_dqs because sqlite-utils
17-
# allowed doublequotes
12+
logger = logging.getLogger('apsw')
13+
logger.setLevel(logging.ERROR)
14+
apsw.ext.log_sqlite(logger=logger)
15+
16+
# We don't use bestpractice.connection_dqs because sqlite-utils allowed doublequotes
1817
apsw.bestpractice.apply((
1918
apsw.bestpractice.connection_busy_timeout,
2019
apsw.bestpractice.connection_enable_foreign_keys,
2120
apsw.bestpractice.connection_optimize,
2221
apsw.bestpractice.connection_recursive_triggers,
23-
apsw.bestpractice.connection_wal,
24-
apsw.bestpractice.library_logging
22+
apsw.bestpractice.connection_wal
2523
))
2624

2725
try: from sqlite_dump import iterdump
@@ -446,6 +444,18 @@ def executescript(self, sql: str) -> apsw.Cursor:
446444
self._tracer(sql, None)
447445
return self.conn.execute(sql)
448446

447+
def modify_table_schema(self, tbl_name:str, schema:str):
448+
"""
449+
Modify table `tbl_name` to use `schema`.
450+
451+
:param tbl_name: Table to modify schema
452+
:param schema: New schema
453+
"""
454+
self.execute("PRAGMA writable_schema=ON")
455+
self.execute(f"UPDATE sqlite_master SET sql = '{schema}' WHERE type='table' AND name='{tbl_name}'")
456+
self.execute("PRAGMA writable_schema=OFF")
457+
self.execute("VACUUM") # force sqlite to reload connections
458+
449459
def __hash__(self): return hash(self.conn)
450460

451461
def convert_lists_to_tuples(function):

tests/test_create.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def test_self_referential_foreign_key(fresh_db):
303303
assert (
304304
"CREATE TABLE [test_table] (\n"
305305
" [id] INTEGER PRIMARY KEY,\n"
306-
" [ref] INTEGER REFERENCES [test_table]([id])\n"
306+
" [ref] INTEGER REFERENCES [test_table]([id]) ON UPDATE CASCADE ON DELETE CASCADE\n"
307307
")"
308308
) == table.schema
309309

@@ -491,7 +491,7 @@ def test_add_column_foreign_key(fresh_db):
491491
assert fresh_db["dogs"].schema == (
492492
'CREATE TABLE "dogs" (\n'
493493
" [name] TEXT,\n"
494-
" [breed_id] INTEGER REFERENCES [breeds]([rowid])\n"
494+
" [breed_id] INTEGER REFERENCES [breeds]([rowid]) ON UPDATE CASCADE ON DELETE CASCADE\n"
495495
")"
496496
)
497497
# And again with an explicit primary key column
@@ -500,8 +500,8 @@ def test_add_column_foreign_key(fresh_db):
500500
assert fresh_db["dogs"].schema == (
501501
'CREATE TABLE "dogs" (\n'
502502
" [name] TEXT,\n"
503-
" [breed_id] INTEGER REFERENCES [breeds]([rowid]),\n"
504-
" [subbreed_id] TEXT REFERENCES [subbreeds]([primkey])\n"
503+
" [breed_id] INTEGER REFERENCES [breeds]([rowid]) ON UPDATE CASCADE ON DELETE CASCADE,\n"
504+
" [subbreed_id] TEXT REFERENCES [subbreeds]([primkey]) ON UPDATE CASCADE ON DELETE CASCADE\n"
505505
")"
506506
)
507507

@@ -514,7 +514,7 @@ def test_add_foreign_key_guess_table(fresh_db):
514514
assert fresh_db["dogs"].schema == (
515515
'CREATE TABLE "dogs" (\n'
516516
" [name] TEXT,\n"
517-
" [breed_id] INTEGER REFERENCES [breeds]([id])\n"
517+
" [breed_id] INTEGER REFERENCES [breeds]([id]) ON UPDATE CASCADE ON DELETE CASCADE\n"
518518
")"
519519
)
520520

tests/test_extract.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_extract_single_column(fresh_db, table, fk_column):
2626
'CREATE TABLE "tree" (\n'
2727
" [id] INTEGER PRIMARY KEY,\n"
2828
" [name] TEXT,\n"
29-
" [{}] INTEGER REFERENCES [{}]([id]),\n".format(expected_fk, expected_table)
29+
" [{}] INTEGER REFERENCES [{}]([id]) ON UPDATE CASCADE ON DELETE CASCADE,\n".format(expected_fk, expected_table)
3030
+ " [end] INTEGER\n"
3131
+ ")"
3232
)
@@ -73,7 +73,7 @@ def test_extract_multiple_columns_with_rename(fresh_db):
7373
'CREATE TABLE "tree" (\n'
7474
" [id] INTEGER PRIMARY KEY,\n"
7575
" [name] TEXT,\n"
76-
" [common_name_latin_name_id] INTEGER REFERENCES [common_name_latin_name]([id])\n"
76+
" [common_name_latin_name_id] INTEGER REFERENCES [common_name_latin_name]([id]) ON UPDATE CASCADE ON DELETE CASCADE\n"
7777
")"
7878
)
7979
assert fresh_db["common_name_latin_name"].schema == (
@@ -123,7 +123,7 @@ def test_extract_rowid_table(fresh_db):
123123
assert fresh_db["tree"].schema == (
124124
'CREATE TABLE "tree" (\n'
125125
" [name] TEXT,\n"
126-
" [common_name_latin_name_id] INTEGER REFERENCES [common_name_latin_name]([id])\n"
126+
" [common_name_latin_name_id] INTEGER REFERENCES [common_name_latin_name]([id]) ON UPDATE CASCADE ON DELETE CASCADE\n"
127127
")"
128128
)
129129
assert (
@@ -153,14 +153,14 @@ def test_reuse_lookup_table(fresh_db):
153153
assert fresh_db["sightings"].schema == (
154154
'CREATE TABLE "sightings" (\n'
155155
" [id] INTEGER PRIMARY KEY,\n"
156-
" [species_id] INTEGER REFERENCES [species]([id])\n"
156+
" [species_id] INTEGER REFERENCES [species]([id]) ON UPDATE CASCADE ON DELETE CASCADE\n"
157157
")"
158158
)
159159
assert fresh_db["individuals"].schema == (
160160
'CREATE TABLE "individuals" (\n'
161161
" [id] INTEGER PRIMARY KEY,\n"
162162
" [name] TEXT,\n"
163-
" [species_id] INTEGER REFERENCES [species]([id])\n"
163+
" [species_id] INTEGER REFERENCES [species]([id]) ON UPDATE CASCADE ON DELETE CASCADE\n"
164164
")"
165165
)
166166
assert list(fresh_db["species"].rows) == [

tests/test_extracts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def test_extracts(fresh_db, kwargs, expected_table, use_table_factory):
3636
== fresh_db[expected_table].schema
3737
)
3838
assert (
39-
"CREATE TABLE [Trees] (\n [id] INTEGER,\n [species_id] INTEGER REFERENCES [{}]([id])\n)".format(
39+
"CREATE TABLE [Trees] (\n [id] INTEGER,\n [species_id] INTEGER REFERENCES [{}]([id]) ON UPDATE CASCADE ON DELETE CASCADE\n)".format(
4040
expected_table
4141
)
4242
== fresh_db["Trees"].schema

tests/test_lookup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ def test_lookup_with_extra_insert_parameters(fresh_db):
121121
" [type] TEXT,\n"
122122
" [first_seen] TEXT,\n"
123123
" [make_not_null] INTEGER NOT NULL,\n"
124-
" [fk_to_other] INTEGER REFERENCES [other_table]([id]),\n"
124+
" [fk_to_other] INTEGER REFERENCES [other_table]([id]) ON UPDATE CASCADE ON DELETE CASCADE,\n"
125125
" [default_is_dog] TEXT DEFAULT 'dog',\n"
126-
" [extract_this] INTEGER REFERENCES [extract_this]([id]),\n"
126+
" [extract_this] INTEGER REFERENCES [extract_this]([id]) ON UPDATE CASCADE ON DELETE CASCADE,\n"
127127
" [convert_to_upper] TEXT,\n"
128128
" [make_this_integer] INTEGER\n"
129129
")"

tests/test_transform.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ def test_transform_add_foreign_keys_from_scratch(fresh_db):
434434
'CREATE TABLE "places" (\n'
435435
" [id] INTEGER,\n"
436436
" [name] TEXT,\n"
437-
" [country] INTEGER REFERENCES [country]([id]),\n"
438-
" [continent] INTEGER REFERENCES [continent]([id]),\n"
439-
" [city] INTEGER REFERENCES [city]([id])\n"
437+
" [country] INTEGER REFERENCES [country]([id]) ON UPDATE CASCADE ON DELETE CASCADE,\n"
438+
" [continent] INTEGER REFERENCES [continent]([id]) ON UPDATE CASCADE ON DELETE CASCADE,\n"
439+
" [city] INTEGER REFERENCES [city]([id]) ON UPDATE CASCADE ON DELETE CASCADE\n"
440440
")"
441441
)
442442

@@ -505,8 +505,8 @@ def test_transform_replace_foreign_keys(fresh_db, foreign_keys):
505505
'CREATE TABLE "places" (\n'
506506
" [id] INTEGER,\n"
507507
" [name] TEXT,\n"
508-
" [country] INTEGER REFERENCES [country]([id]),\n"
509-
" [continent] INTEGER REFERENCES [continent]([id]),\n"
508+
" [country] INTEGER REFERENCES [country]([id]) ON UPDATE CASCADE ON DELETE CASCADE,\n"
509+
" [continent] INTEGER REFERENCES [continent]([id]) ON UPDATE CASCADE ON DELETE CASCADE,\n"
510510
" [city] INTEGER\n"
511511
")"
512512
)

0 commit comments

Comments
 (0)