tinykv 0.2.0__tar.gz → 0.2.1__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.
@@ -9,6 +9,13 @@ Entries marked as **BC BREAK** indicate backward-incompatible changes.
9
9
 
10
10
  ## [Unreleased]
11
11
 
12
+ ## [0.2.1] - 2026-03-11
13
+
14
+ ### Changed
15
+ - Added an optional `if_not_exists` parameter to `create_schema()` to support
16
+ idempotent table creation when needed while preserving the default error on
17
+ existing tables. Close #1.
18
+
12
19
  ## [0.2.0] - 2026-03-11
13
20
 
14
21
  ### Changed
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: tinykv
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  Summary: A lightweight Python SQLite key-value store built on sqlite3.
5
5
  Keywords: python,sqlite,sqlite3,key-value store,kv
6
6
  Author-email: Flavio Veloso Soares <flaviovs@magnux.com>
@@ -255,6 +255,10 @@ TinyKV requires the database table to exist before use. Create it with
255
255
  >>> conn = sqlite3.connect(':memory:')
256
256
  >>> tinykv.create_schema(conn)
257
257
 
258
+ If you need idempotent setup, pass `if_not_exists=True`:
259
+
260
+ >>> tinykv.create_schema(conn, if_not_exists=True)
261
+
258
262
  You can use a custom table name:
259
263
 
260
264
  >>> tinykv.create_schema(conn, table='my_keys')
@@ -226,6 +226,10 @@ TinyKV requires the database table to exist before use. Create it with
226
226
  >>> conn = sqlite3.connect(':memory:')
227
227
  >>> tinykv.create_schema(conn)
228
228
 
229
+ If you need idempotent setup, pass `if_not_exists=True`:
230
+
231
+ >>> tinykv.create_schema(conn, if_not_exists=True)
232
+
229
233
  You can use a custom table name:
230
234
 
231
235
  >>> tinykv.create_schema(conn, table='my_keys')
@@ -34,7 +34,7 @@ def _validate_key(key: str) -> None:
34
34
  raise ValueError('key must not be empty')
35
35
 
36
36
 
37
- __version__ = '0.2.0'
37
+ __version__ = '0.2.1'
38
38
 
39
39
  _DEF_TABLE = 'kv'
40
40
  _ALLOW_PICKLE_DEFAULT = object()
@@ -42,16 +42,20 @@ _ALLOW_PICKLE_DEFAULT = object()
42
42
  logger = logging.getLogger(__name__)
43
43
 
44
44
 
45
- def create_schema(conn: sqlite3.Connection, table: str = _DEF_TABLE) -> None:
45
+ def create_schema(conn: sqlite3.Connection, table: str = _DEF_TABLE,
46
+ if_not_exists: bool = False) -> None:
46
47
  """Create a database table for use with tinykv.
47
48
 
48
49
  Args:
49
50
  conn: The SQLite3 connection object.
50
51
  table: The table name (default: 'kv').
52
+ if_not_exists: If true, include SQLite's `IF NOT EXISTS` clause so
53
+ repeated schema creation is a no-op.
51
54
 
52
55
  """
53
56
  _validate_table_name(table)
54
- conn.execute(f'CREATE TABLE {table} ('
57
+ if_not_exists_sql = 'IF NOT EXISTS ' if if_not_exists else ''
58
+ conn.execute(f'CREATE TABLE {if_not_exists_sql}{table} ('
55
59
  'k TEXT NOT NULL, '
56
60
  't TINYINT NOT NULL CHECK (t BETWEEN 1 AND 7), '
57
61
  'v BLOB, '
@@ -21,7 +21,7 @@ _TEST_DATA = {
21
21
  }
22
22
 
23
23
 
24
- class TestKV(unittest.TestCase):
24
+ class TestKV(unittest.TestCase): # pylint: disable=too-many-public-methods
25
25
 
26
26
  def setUp(self) -> None:
27
27
  # pylint: disable-next=consider-using-with
@@ -42,6 +42,13 @@ class TestKV(unittest.TestCase):
42
42
  db.set(k, v)
43
43
  self.assertEqual(db.get(k), v)
44
44
 
45
+ def test_create_schema_existing_table_raises_by_default(self) -> None:
46
+ with self.assertRaises(sqlite3.OperationalError):
47
+ create_schema(self._conn)
48
+
49
+ def test_create_schema_if_not_exists(self) -> None:
50
+ create_schema(self._conn, if_not_exists=True)
51
+
45
52
  def test_set_replace(self) -> None:
46
53
  db = TinyKV(self._conn, allow_pickle=True)
47
54
 
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes