mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-07-23 01:14:31 +02:00
add-column col_type now optional, defaults to str
This commit is contained in:
parent
557dc3f9a7
commit
3cab079d3e
6 changed files with 18 additions and 8 deletions
|
|
@ -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:
|
||||
|
||||
|
|
|
|||
|
|
@ -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::
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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]
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue