diff --git a/docs/cli.rst b/docs/cli.rst index addf7c6..49d5d92 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -230,7 +230,7 @@ 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``. +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. .. _cli_add_foreign_key: diff --git a/docs/python-api.rst b/docs/python-api.rst index d3d05d3..8171629 100644 --- a/docs/python-api.rst +++ b/docs/python-api.rst @@ -232,9 +232,12 @@ You can add a new column to a table using the ``.add_column(col_name, col_type)` db["dogs"].add_column("weight", float) db["dogs"].add_column("dob", datetime.date) db["dogs"].add_column("image", "BLOB") + db["dogs"].add_column("website") # str by default You can specify the ``col_type`` argument either using a SQLite type as a string, or by directly passing a Python type e.g. ``str`` or ``float``. +The ``col_type`` is optional - if you omit it the type of ``TEXT`` will be used. + SQLite types you can specify are ``"TEXT"``, ``"INTEGER"``, ``"FLOAT"`` or ``"BLOB"``. If you pass a Python type, it will be mapped to SQLite types as shown here:: diff --git a/sqlite_utils/cli.py b/sqlite_utils/cli.py index 665aa32..63fcffb 100644 --- a/sqlite_utils/cli.py +++ b/sqlite_utils/cli.py @@ -148,6 +148,7 @@ def optimize(path, no_vacuum): type=click.Choice( ["integer", "float", "blob", "text", "INTEGER", "FLOAT", "BLOB", "TEXT"] ), + required=False, ) def add_column(path, table, col_name, col_type): "Add a column to the specified table" diff --git a/sqlite_utils/db.py b/sqlite_utils/db.py index 725ba53..9b9170f 100644 --- a/sqlite_utils/db.py +++ b/sqlite_utils/db.py @@ -276,7 +276,9 @@ class Table: self.db.conn.execute(sql) return self - def add_column(self, col_name, col_type): + def add_column(self, col_name, col_type=None): + if col_type is None: + col_type = str sql = "ALTER TABLE [{table}] ADD COLUMN [{col_name}] {col_type};".format( table=self.name, col_name=col_name, col_type=COLUMN_TYPE_MAPPING[col_type] ) diff --git a/tests/test_cli.py b/tests/test_cli.py index 7f2d17f..ad2bec7 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -183,6 +183,7 @@ def test_create_index(db_path): ), ("float", "FLOAT", "CREATE TABLE [dogs] ( [name] TEXT , [float] FLOAT)"), ("blob", "blob", "CREATE TABLE [dogs] ( [name] TEXT , [blob] BLOB)"), + ("default", None, "CREATE TABLE [dogs] ( [name] TEXT , [default] TEXT)"), ), ) def test_add_column(db_path, col_name, col_type, expected_schema): @@ -191,12 +192,10 @@ def test_add_column(db_path, col_name, col_type, expected_schema): assert "CREATE TABLE [dogs] ( [name] TEXT )" == collapse_whitespace( db["dogs"].schema ) - assert ( - 0 - == CliRunner() - .invoke(cli.cli, ["add-column", db_path, "dogs", col_name, col_type]) - .exit_code - ) + args = ["add-column", db_path, "dogs", col_name] + if col_type is not None: + args.append(col_type) + assert 0 == CliRunner().invoke(cli.cli, args).exit_code assert expected_schema == collapse_whitespace(db["dogs"].schema) diff --git a/tests/test_create.py b/tests/test_create.py index 7aaae5d..24f4e97 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -145,6 +145,11 @@ def test_create_error_if_invalid_foreign_keys(fresh_db): ), ("float", "FLOAT", "CREATE TABLE [dogs] ( [name] TEXT , [float] FLOAT)"), ("blob", "blob", "CREATE TABLE [dogs] ( [name] TEXT , [blob] BLOB)"), + ( + "default_str", + None, + "CREATE TABLE [dogs] ( [name] TEXT , [default_str] TEXT)", + ), ), ) def test_add_column(fresh_db, col_name, col_type, expected_schema):