|
1 | 1 | # This file is from sqlite-utils and copyright and license is the same as that project |
2 | 2 | __all__ = ['Database', 'Queryable', 'Table', 'View'] |
3 | 3 |
|
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 |
6 | 5 | from collections import namedtuple |
7 | 6 | 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 |
10 | 8 | 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 |
15 | 11 |
|
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 |
18 | 17 | apsw.bestpractice.apply(( |
19 | 18 | apsw.bestpractice.connection_busy_timeout, |
20 | 19 | apsw.bestpractice.connection_enable_foreign_keys, |
21 | 20 | apsw.bestpractice.connection_optimize, |
22 | 21 | apsw.bestpractice.connection_recursive_triggers, |
23 | | - apsw.bestpractice.connection_wal, |
24 | | - apsw.bestpractice.library_logging |
| 22 | + apsw.bestpractice.connection_wal |
25 | 23 | )) |
26 | 24 |
|
27 | 25 | try: from sqlite_dump import iterdump |
@@ -446,6 +444,18 @@ def executescript(self, sql: str) -> apsw.Cursor: |
446 | 444 | self._tracer(sql, None) |
447 | 445 | return self.conn.execute(sql) |
448 | 446 |
|
| 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 | + |
449 | 459 | def __hash__(self): return hash(self.conn) |
450 | 460 |
|
451 | 461 | def convert_lists_to_tuples(function): |
|
0 commit comments