diff --git a/docs/cli.rst b/docs/cli.rst index dc0c072..572b0e5 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -1342,6 +1342,8 @@ You can include named parameters in your where clause and populate them using on The ``--dry-run`` option will output a preview of the conversion against the first ten rows, without modifying the database. +By default any rows with a falsey value for the column - such as ``0`` or ``null`` - will be skipped. Use the ``--no-skip-false`` option to disable this behaviour. + .. _cli_convert_import: Importing additional modules diff --git a/docs/python-api.rst b/docs/python-api.rst index 206e5e6..4d883db 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -916,6 +916,8 @@ This will add the new column, if it does not already exist. You can pass ``outpu If you want to drop the original column after saving the results in a separate output column, pass ``drop=True``. +By default any rows with a falsey value for the column - such as ``0`` or ``None`` - will be skipped. Pass ``skip_false=False`` to disable this behaviour. + You can create multiple new columns from a single input column by passing ``multi=True`` and a conversion function that returns a Python dictionary. This example creates new ``upper`` and ``lower`` columns populated from the single ``title`` column: .. code-block:: python diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index c3c55e2..ce354e0 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -2811,6 +2811,7 @@ def _generate_convert_help(): type=click.Choice(["integer", "float", "blob", "text"]), ) @click.option("--drop", is_flag=True, help="Drop original column afterwards") +@click.option("--no-skip-false", is_flag=True, help="Don't skip falsey values") @click.option("-s", "--silent", is_flag=True, help="Don't show a progress bar") def convert( db_path, @@ -2825,6 +2826,7 @@ def convert( output, output_type, drop, + no_skip_false, silent, ): sqlite3.enable_callback_tracebacks(True) @@ -2882,6 +2884,7 @@ def convert( output=output, output_type=output_type, drop=drop, + skip_false=not no_skip_false, multi=multi, show_progress=not silent, ) diff --git a/tests/test_cli_convert.py b/tests/test_cli_convert.py index d26ab8c..97988b9 100644 --- a/tests/test_cli_convert.py +++ b/tests/test_cli_convert.py @@ -626,3 +626,30 @@ def test_convert_initialization_pattern(fresh_db_and_path): assert list(db["names"].rows) == [ {"id": 1, "name": "17"}, ] + + +@pytest.mark.parametrize( + "no_skip_false,expected", + ( + (True, 1), + (False, 0), + ), +) +def test_convert_no_skip_false(fresh_db_and_path, no_skip_false, expected): + db, db_path = fresh_db_and_path + args = [ + "convert", + db_path, + "t", + "x", + "-", + ] + if no_skip_false: + args.append("--no-skip-false") + db["t"].insert_all([{"x": 0}, {"x": 1}]) + assert db["t"].get(1)["x"] == 0 + assert db["t"].get(2)["x"] == 1 + result = CliRunner().invoke(cli.cli, args, input="value + 1") + assert 0 == result.exit_code, result.output + assert db["t"].get(1)["x"] == expected + assert db["t"].get(2)["x"] == 2