Skip to content

Commit 3cab079

Browse files
committed
add-column col_type now optional, defaults to str
1 parent 557dc3f commit 3cab079

File tree

6 files changed

+18
-8
lines changed

6 files changed

+18
-8
lines changed

docs/cli.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ You can add a column using the ``add-column`` command::
230230

231231
$ sqlite-utils add-column mydb.db mytable nameofcolumn text
232232

233-
The last argument here is the type of the column to be created. You can use one of ``text``, ``integer``, ``float`` or ``blob``.
233+
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.
234234

235235
.. _cli_add_foreign_key:
236236

docs/python-api.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,12 @@ You can add a new column to a table using the ``.add_column(col_name, col_type)`
232232
db["dogs"].add_column("weight", float)
233233
db["dogs"].add_column("dob", datetime.date)
234234
db["dogs"].add_column("image", "BLOB")
235+
db["dogs"].add_column("website") # str by default
235236
236237
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``.
237238
239+
The ``col_type`` is optional - if you omit it the type of ``TEXT`` will be used.
240+
238241
SQLite types you can specify are ``"TEXT"``, ``"INTEGER"``, ``"FLOAT"`` or ``"BLOB"``.
239242
240243
If you pass a Python type, it will be mapped to SQLite types as shown here::

sqlite_utils/cli.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ def optimize(path, no_vacuum):
148148
type=click.Choice(
149149
["integer", "float", "blob", "text", "INTEGER", "FLOAT", "BLOB", "TEXT"]
150150
),
151+
required=False,
151152
)
152153
def add_column(path, table, col_name, col_type):
153154
"Add a column to the specified table"

sqlite_utils/db.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ def create_index(self, columns, index_name=None, unique=False, if_not_exists=Fal
276276
self.db.conn.execute(sql)
277277
return self
278278

279-
def add_column(self, col_name, col_type):
279+
def add_column(self, col_name, col_type=None):
280+
if col_type is None:
281+
col_type = str
280282
sql = "ALTER TABLE [{table}] ADD COLUMN [{col_name}] {col_type};".format(
281283
table=self.name, col_name=col_name, col_type=COLUMN_TYPE_MAPPING[col_type]
282284
)

tests/test_cli.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def test_create_index(db_path):
183183
),
184184
("float", "FLOAT", "CREATE TABLE [dogs] ( [name] TEXT , [float] FLOAT)"),
185185
("blob", "blob", "CREATE TABLE [dogs] ( [name] TEXT , [blob] BLOB)"),
186+
("default", None, "CREATE TABLE [dogs] ( [name] TEXT , [default] TEXT)"),
186187
),
187188
)
188189
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):
191192
assert "CREATE TABLE [dogs] ( [name] TEXT )" == collapse_whitespace(
192193
db["dogs"].schema
193194
)
194-
assert (
195-
0
196-
== CliRunner()
197-
.invoke(cli.cli, ["add-column", db_path, "dogs", col_name, col_type])
198-
.exit_code
199-
)
195+
args = ["add-column", db_path, "dogs", col_name]
196+
if col_type is not None:
197+
args.append(col_type)
198+
assert 0 == CliRunner().invoke(cli.cli, args).exit_code
200199
assert expected_schema == collapse_whitespace(db["dogs"].schema)
201200

202201

tests/test_create.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ def test_create_error_if_invalid_foreign_keys(fresh_db):
145145
),
146146
("float", "FLOAT", "CREATE TABLE [dogs] ( [name] TEXT , [float] FLOAT)"),
147147
("blob", "blob", "CREATE TABLE [dogs] ( [name] TEXT , [blob] BLOB)"),
148+
(
149+
"default_str",
150+
None,
151+
"CREATE TABLE [dogs] ( [name] TEXT , [default_str] TEXT)",
152+
),
148153
),
149154
)
150155
def test_add_column(fresh_db, col_name, col_type, expected_schema):

0 commit comments

Comments
 (0)