Skip to content

Commit

Permalink
add-column col_type now optional, defaults to str
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Feb 24, 2019
1 parent 557dc3f commit 3cab079
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
3 changes: 3 additions & 0 deletions docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
1 change: 1 addition & 0 deletions sqlite_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 3 additions & 1 deletion sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ def create_index(self, columns, index_name=None, unique=False, if_not_exists=Fal
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]
)
Expand Down
11 changes: 5 additions & 6 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)


Expand Down
5 changes: 5 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 3cab079

Please sign in to comment.