mirror of
https://github.com/simonw/sqlite-utils.git
synced 2026-08-01 15:04:11 +02:00
Ability to add descending order indexes (#262)
* DescIndex(column) for descending index columns, refs #260 * Ability to add desc indexes using CLI, closes #260
This commit is contained in:
parent
b2302875c9
commit
51d01da30d
6 changed files with 69 additions and 4 deletions
|
|
@ -5,7 +5,7 @@ from datetime import datetime
|
|||
import hashlib
|
||||
import pathlib
|
||||
import sqlite_utils
|
||||
from sqlite_utils.db import AlterError
|
||||
from sqlite_utils.db import AlterError, DescIndex
|
||||
import textwrap
|
||||
import io
|
||||
import itertools
|
||||
|
|
@ -450,11 +450,21 @@ def index_foreign_keys(path, load_extension):
|
|||
)
|
||||
@load_extension_option
|
||||
def create_index(path, table, column, name, unique, if_not_exists, load_extension):
|
||||
"Add an index to the specified table covering the specified columns"
|
||||
"""
|
||||
Add an index to the specified table covering the specified columns.
|
||||
Use "sqlite-utils create-index mydb -- -column" to specify descending
|
||||
order for a column.
|
||||
"""
|
||||
db = sqlite_utils.Database(path)
|
||||
_load_extensions(db, load_extension)
|
||||
# Treat -prefix as descending for columns
|
||||
columns = []
|
||||
for col in column:
|
||||
if col.startswith("-"):
|
||||
col = DescIndex(col[1:])
|
||||
columns.append(col)
|
||||
db[table].create_index(
|
||||
column, index_name=name, unique=unique, if_not_exists=if_not_exists
|
||||
columns, index_name=name, unique=unique, if_not_exists=if_not_exists
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue