diff --git a/docs/cli-reference.rst b/docs/cli-reference.rst index 02f66cd..b717461 100644 --- a/docs/cli-reference.rst +++ b/docs/cli-reference.rst @@ -1157,7 +1157,7 @@ See :ref:`cli_add_column`. :: Usage: sqlite-utils add-column [OPTIONS] PATH TABLE COL_NAME - [[integer|float|blob|text|INTEGER|FLOAT|BLOB|TEXT]] + [[integer|int|float|text|str|blob|bytes]] Add a column to the specified table diff --git a/docs/cli.rst b/docs/cli.rst index 5d9deb0..736e7d6 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -2310,7 +2310,14 @@ You can add a column using the ``add-column`` command: sqlite-utils add-column mydb.db mytable nameofcolumn text -The last argument here is the type of the column to be created. You can use one of ``text``, ``integer``, ``float`` or ``blob``. If you leave it off, ``text`` will be used. +The last argument here is the type of the column to be created. This can be one of: + +- ``text`` or ``str`` +- ``integer`` or ``int`` +- ``float`` +- ``blob`` or ``bytes`` + +This argument is optional and defaults to ``text``. You can add a column that is a foreign key reference to another table using the ``--fk`` option: diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 5821db6..47da407 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -60,6 +60,14 @@ It's often worth trying: --encoding=latin-1 maximize_csv_field_size_limit() +class CaseInsensitiveChoice(click.Choice): + def __init__(self, choices): + super().__init__([choice.lower() for choice in choices]) + + def convert(self, value, param, ctx): + return super().convert(value.lower(), param, ctx) + + def output_options(fn): for decorator in reversed( ( @@ -412,7 +420,8 @@ def dump(path, load_extension): @click.argument( "col_type", type=click.Choice( - ["integer", "float", "blob", "text", "INTEGER", "FLOAT", "BLOB", "TEXT"] + ["integer", "int", "float", "text", "str", "blob", "bytes"], + case_sensitive=False, ), required=False, ) diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 1baa32e..50e26e1 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -197,9 +197,12 @@ COLUMN_TYPE_MAPPING = { "FLOAT": "FLOAT", "BLOB": "BLOB", "text": "TEXT", + "str": "TEXT", "integer": "INTEGER", + "int": "INTEGER", "float": "FLOAT", "blob": "BLOB", + "bytes": "BLOB", } # If numpy is available, add more types if np: diff --git a/tests/test_cli.py b/tests/test_cli.py index 0a93504..5313589 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -272,13 +272,23 @@ def test_create_index_desc(db_path): "col_name,col_type,expected_schema", ( ("text", "TEXT", "CREATE TABLE [dogs] (\n [name] TEXT\n, [text] TEXT)"), + ("text", "str", "CREATE TABLE [dogs] (\n [name] TEXT\n, [text] TEXT)"), + ("text", "STR", "CREATE TABLE [dogs] (\n [name] TEXT\n, [text] TEXT)"), ( "integer", "INTEGER", "CREATE TABLE [dogs] (\n [name] TEXT\n, [integer] INTEGER)", ), + ( + "integer", + "int", + "CREATE TABLE [dogs] (\n [name] TEXT\n, [integer] INTEGER)", + ), ("float", "FLOAT", "CREATE TABLE [dogs] (\n [name] TEXT\n, [float] FLOAT)"), ("blob", "blob", "CREATE TABLE [dogs] (\n [name] TEXT\n, [blob] BLOB)"), + ("blob", "BLOB", "CREATE TABLE [dogs] (\n [name] TEXT\n, [blob] BLOB)"), + ("blob", "bytes", "CREATE TABLE [dogs] (\n [name] TEXT\n, [blob] BLOB)"), + ("blob", "BYTES", "CREATE TABLE [dogs] (\n [name] TEXT\n, [blob] BLOB)"), ("default", None, "CREATE TABLE [dogs] (\n [name] TEXT\n, [default] TEXT)"), ), )