Skip to content

Commit

Permalink
str, int, bytes aliases for column types, closes #606
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Dec 6, 2023
1 parent 9286c1b commit 88bd372
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/cli-reference.rst
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion docs/cli.rst
Expand Up @@ -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:

Expand Down
11 changes: 10 additions & 1 deletion sqlite_utils/cli.py
Expand Up @@ -60,6 +60,14 @@
maximize_csv_field_size_limit()


class CaseInsensitiveChoice(click.Choice):

This comment has been minimized.

Copy link
@fynsta

fynsta Dec 10, 2023

This seems unused

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(
(
Expand Down Expand Up @@ -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,
)
Expand Down
3 changes: 3 additions & 0 deletions sqlite_utils/db.py
Expand Up @@ -197,9 +197,12 @@ class Default:
"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:
Expand Down
10 changes: 10 additions & 0 deletions tests/test_cli.py
Expand Up @@ -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)"),
),
)
Expand Down

0 comments on commit 88bd372

Please sign in to comment.