Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more STRICT table support #604

Merged
merged 5 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/cli-reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ See :ref:`cli_inserting_data`, :ref:`cli_insert_csv_tsv`, :ref:`cli_insert_unstr
--analyze Run ANALYZE at the end of this operation
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
--silent Do not show progress bar
--strict Apply STRICT mode to created table
--ignore Ignore records if pk already exists
--replace Replace records if pk already exists
--truncate Truncate table before inserting records, if table
Expand Down Expand Up @@ -345,6 +346,7 @@ See :ref:`cli_upsert`.
--analyze Run ANALYZE at the end of this operation
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
--silent Do not show progress bar
--strict Apply STRICT mode to created table
-h, --help Show this message and exit.


Expand Down Expand Up @@ -920,6 +922,7 @@ See :ref:`cli_create_table`.
--replace If table already exists, replace it
--transform If table already exists, try to transform the schema
--load-extension TEXT Path to SQLite extension, with optional :entrypoint
--strict Apply STRICT mode to created table
-h, --help Show this message and exit.


Expand Down
21 changes: 20 additions & 1 deletion docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,25 @@ You can specify foreign key relationships between the tables you are creating us
[author_id] INTEGER REFERENCES [authors]([id])
)

You can create a table in `SQLite STRICT mode <https://www.sqlite.org/stricttables.html>`__ using ``--strict``:

.. code-block:: bash

sqlite-utils create-table mydb.db mytable id integer name text --strict

.. code-block:: bash

sqlite-utils tables mydb.db --schema -t

.. code-block:: output

table schema
------- ------------------------
mytable CREATE TABLE [mytable] (
[id] INTEGER,
[name] TEXT
) STRICT

If a table with the same name already exists, you will get an error. You can choose to silently ignore this error with ``--ignore``, or you can replace the existing table with a new, empty table using ``--replace``.

You can also pass ``--transform`` to transform the existing table to match the new schema. See :ref:`python_api_explicit_create` in the Python library documentation for details of how this option works.
Expand Down Expand Up @@ -2018,7 +2037,7 @@ Use ``--ignore`` to ignore the error if the table does not exist.
Transforming tables
===================

The ``transform`` command allows you to apply complex transformations to a table that cannot be implemented using a regular SQLite ``ALTER TABLE`` command. See :ref:`python_api_transform` for details of how this works.
The ``transform`` command allows you to apply complex transformations to a table that cannot be implemented using a regular SQLite ``ALTER TABLE`` command. See :ref:`python_api_transform` for details of how this works. The ``transform`` command preserves a table's ``STRICT`` mode.

.. code-block:: bash

Expand Down
18 changes: 17 additions & 1 deletion docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ By default, any :ref:`sqlite-utils plugins <plugins>` that implement the :ref:`p

db = Database(memory=True, execute_plugins=False)

You can pass ``strict=True`` to enable `SQLite STRICT mode <https://www.sqlite.org/stricttables.html>`__ for all tables created using this database object:

.. code-block:: python

db = Database("my_database.db", strict=True)

.. _python_api_attach:

Attaching additional databases
Expand Down Expand Up @@ -581,6 +587,15 @@ The ``transform=True`` option will update the table schema if any of the followi

Changes to ``foreign_keys=`` are not currently detected and applied by ``transform=True``.

You can pass ``strict=True`` to create a table in ``STRICT`` mode:

.. code-block:: python

db["cats"].create({
"id": int,
"name": str,
}, strict=True)

.. _python_api_compound_primary_keys:

Compound primary keys
Expand Down Expand Up @@ -661,7 +676,7 @@ You can set default values for these methods by accessing the table through the
# Now you can call .insert() like so:
table.insert({"id": 1, "name": "Tracy", "score": 5})

The configuration options that can be specified in this way are ``pk``, ``foreign_keys``, ``column_order``, ``not_null``, ``defaults``, ``batch_size``, ``hash_id``, ``hash_id_columns``, ``alter``, ``ignore``, ``replace``, ``extracts``, ``conversions``, ``columns``. These are all documented below.
The configuration options that can be specified in this way are ``pk``, ``foreign_keys``, ``column_order``, ``not_null``, ``defaults``, ``batch_size``, ``hash_id``, ``hash_id_columns``, ``alter``, ``ignore``, ``replace``, ``extracts``, ``conversions``, ``columns``, ``strict``. These are all documented below.

.. _python_api_defaults_not_null:

Expand Down Expand Up @@ -1011,6 +1026,7 @@ The first time this is called the record will be created for ``name="Palm"``. An
- ``extracts``
- ``conversions``
- ``columns``
- ``strict``

.. _python_api_extracts:

Expand Down
19 changes: 19 additions & 0 deletions sqlite_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,12 @@ def inner(fn):
),
load_extension_option,
click.option("--silent", is_flag=True, help="Do not show progress bar"),
click.option(
"--strict",
is_flag=True,
default=False,
help="Apply STRICT mode to created table",
),
)
):
fn = decorator(fn)
Expand Down Expand Up @@ -942,6 +948,7 @@ def insert_upsert_implementation(
silent=False,
bulk_sql=None,
functions=None,
strict=False,
):
db = sqlite_utils.Database(path)
_load_extensions(db, load_extension)
Expand Down Expand Up @@ -1057,6 +1064,7 @@ def insert_upsert_implementation(
"replace": replace,
"truncate": truncate,
"analyze": analyze,
"strict": strict,
}
if not_null:
extra_kwargs["not_null"] = set(not_null)
Expand Down Expand Up @@ -1177,6 +1185,7 @@ def insert(
truncate,
not_null,
default,
strict,
):
"""
Insert records from FILE into a table, creating the table if it
Expand Down Expand Up @@ -1255,6 +1264,7 @@ def insert(
silent=silent,
not_null=not_null,
default=default,
strict=strict,
)
except UnicodeDecodeError as ex:
raise click.ClickException(UNICODE_ERROR.format(ex))
Expand Down Expand Up @@ -1290,6 +1300,7 @@ def upsert(
analyze,
load_extension,
silent,
strict,
):
"""
Upsert records based on their primary key. Works like 'insert' but if
Expand Down Expand Up @@ -1334,6 +1345,7 @@ def upsert(
analyze=analyze,
load_extension=load_extension,
silent=silent,
strict=strict,
)
except UnicodeDecodeError as ex:
raise click.ClickException(UNICODE_ERROR.format(ex))
Expand Down Expand Up @@ -1502,6 +1514,11 @@ def create_database(path, enable_wal, init_spatialite, load_extension):
help="If table already exists, try to transform the schema",
)
@load_extension_option
@click.option(
"--strict",
is_flag=True,
help="Apply STRICT mode to created table",
)
def create_table(
path,
table,
Expand All @@ -1514,6 +1531,7 @@ def create_table(
replace,
transform,
load_extension,
strict,
):
"""
Add a table with the specified columns. Columns should be specified using
Expand Down Expand Up @@ -1561,6 +1579,7 @@ def create_table(
ignore=ignore,
replace=replace,
transform=transform,
strict=strict,
)


Expand Down
Loading
Loading