mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-11-27 12:29:43 +01:00
'sqlite-utils add-column name type' command, closes #15
This commit is contained in:
parent
05a85b358f
commit
0bc49e938e
5 changed files with 80 additions and 8 deletions
11
docs/cli.rst
11
docs/cli.rst
|
|
@ -221,6 +221,17 @@ After running the above ``dogs.json`` example, try running this::
|
||||||
|
|
||||||
This will replace the record for id=2 (Pancakes) with a new record with an updated age.
|
This will replace the record for id=2 (Pancakes) with a new record with an updated age.
|
||||||
|
|
||||||
|
.. _cli_add_column:
|
||||||
|
|
||||||
|
Adding columns
|
||||||
|
==============
|
||||||
|
|
||||||
|
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``.
|
||||||
|
|
||||||
.. _cli_create_index:
|
.. _cli_create_index:
|
||||||
|
|
||||||
Creating indexes
|
Creating indexes
|
||||||
|
|
|
||||||
|
|
@ -134,6 +134,26 @@ def optimize(path, no_vacuum):
|
||||||
db.vacuum()
|
db.vacuum()
|
||||||
|
|
||||||
|
|
||||||
|
@cli.command(name="add-column")
|
||||||
|
@click.argument(
|
||||||
|
"path",
|
||||||
|
type=click.Path(exists=True, file_okay=True, dir_okay=False, allow_dash=False),
|
||||||
|
required=True,
|
||||||
|
)
|
||||||
|
@click.argument("table")
|
||||||
|
@click.argument("col_name")
|
||||||
|
@click.argument(
|
||||||
|
"col_type",
|
||||||
|
type=click.Choice(
|
||||||
|
["integer", "float", "blob", "text", "INTEGER", "FLOAT", "BLOB", "TEXT"]
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def add_column(path, table, col_name, col_type):
|
||||||
|
"Add a column to the specified table"
|
||||||
|
db = sqlite_utils.Database(path)
|
||||||
|
db[table].add_column(col_name, col_type)
|
||||||
|
|
||||||
|
|
||||||
@cli.command(name="create-index")
|
@cli.command(name="create-index")
|
||||||
@click.argument(
|
@click.argument(
|
||||||
"path",
|
"path",
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@ import os
|
||||||
import pytest
|
import pytest
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
from .utils import collapse_whitespace
|
||||||
|
|
||||||
|
|
||||||
CREATE_TABLES = """
|
CREATE_TABLES = """
|
||||||
create table Gosh (c1 text, c2 text, c3 text);
|
create table Gosh (c1 text, c2 text, c3 text);
|
||||||
|
|
@ -170,6 +172,44 @@ def test_create_index(db_path):
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"col_name,col_type,expected_schema",
|
||||||
|
(
|
||||||
|
("text", "TEXT", "CREATE TABLE [dogs] ( [name] TEXT , [text] TEXT)"),
|
||||||
|
(
|
||||||
|
"integer",
|
||||||
|
"INTEGER",
|
||||||
|
"CREATE TABLE [dogs] ( [name] TEXT , [integer] INTEGER)",
|
||||||
|
),
|
||||||
|
("float", "FLOAT", "CREATE TABLE [dogs] ( [name] TEXT , [float] FLOAT)"),
|
||||||
|
("blob", "blob", "CREATE TABLE [dogs] ( [name] TEXT , [blob] BLOB)"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
def test_add_column(db_path, col_name, col_type, expected_schema):
|
||||||
|
db = Database(db_path)
|
||||||
|
db.create_table("dogs", {"name": str})
|
||||||
|
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
|
||||||
|
)
|
||||||
|
assert expected_schema == collapse_whitespace(db["dogs"].schema)
|
||||||
|
|
||||||
|
|
||||||
|
def test_add_column_error_invalid_type(db_path):
|
||||||
|
db = Database(db_path)
|
||||||
|
db.create_table("dogs", {"name": str})
|
||||||
|
result = CliRunner().invoke(
|
||||||
|
cli.cli, ["add-column", db_path, "dogs", "blah", "badtype"]
|
||||||
|
)
|
||||||
|
assert 0 != result.exit_code
|
||||||
|
assert 'Invalid value for "col_type"' in result.output
|
||||||
|
|
||||||
|
|
||||||
def test_enable_fts(db_path):
|
def test_enable_fts(db_path):
|
||||||
assert None == Database(db_path)["Gosh"].detect_fts()
|
assert None == Database(db_path)["Gosh"].detect_fts()
|
||||||
result = CliRunner().invoke(
|
result = CliRunner().invoke(
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,10 @@ import datetime
|
||||||
import json
|
import json
|
||||||
import pathlib
|
import pathlib
|
||||||
import pytest
|
import pytest
|
||||||
import re
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
|
||||||
|
from .utils import collapse_whitespace
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
@ -364,10 +365,3 @@ def test_create_table_numpy(fresh_db):
|
||||||
"np.uint8": 8,
|
"np.uint8": 8,
|
||||||
}
|
}
|
||||||
] == list(fresh_db["types"].rows)
|
] == list(fresh_db["types"].rows)
|
||||||
|
|
||||||
|
|
||||||
COLLAPSE_RE = re.compile("\s+")
|
|
||||||
|
|
||||||
|
|
||||||
def collapse_whitespace(s):
|
|
||||||
return COLLAPSE_RE.sub(" ", s)
|
|
||||||
|
|
|
||||||
7
tests/utils.py
Normal file
7
tests/utils.py
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import re
|
||||||
|
|
||||||
|
COLLAPSE_RE = re.compile(r"\s+")
|
||||||
|
|
||||||
|
|
||||||
|
def collapse_whitespace(s):
|
||||||
|
return COLLAPSE_RE.sub(" ", s)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue