Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return indexes when .schema is run #219

Merged
merged 3 commits into from
Mar 12, 2025

Conversation

kracekumar
Copy link
Contributor

@kracekumar kracekumar commented Mar 9, 2025

Description

  1. As per .schema: line breaks and missing index information #149. the .schema command doesnot return index associated with the table.
  2. The sqlite special command, .schemacommand returns so.
  3. This PR calls the special command list_indexes to adhere to the similar behaviour
  4. Breaking Change: Before when .index command is run only name was returned, now both name and SQL is returned that is similar to sqlite

Examples

  1. Sqlite
sqlite> .schema
CREATE TABLE u (name TEXT) STRICT;
CREATE TABLE u1 (name TEXT) STRICT;
CREATE TABLE u2 (name TEXT) STRICT;
CREATE TABLE u3 (name TEXT) STRICT;
CREATE TABLE u4 (name TEXT) STRICT;
CREATE TABLE people (
  person_id VARCHAR PRIMARY KEY,
  name VARCHAR,
  born INTEGER,
  died INTEGER
);
CREATE INDEX ix_people_name ON people (name);
CREATE TABLE people1(
  person_id VARCHAR PRIMARY KEY,
  name VARCHAR,
  born INTEGER,
  died INTEGER
);
CREATE INDEX ix_people_name_1 ON people (name);
CREATE INDEX ix_people_name_2 ON people1 (name);

sqlite> .schema people
CREATE TABLE people (
  person_id VARCHAR PRIMARY KEY,
  name VARCHAR,
  born INTEGER,
  died INTEGER
);
CREATE INDEX ix_people_name ON people (name);
CREATE INDEX ix_people_name_1 ON people (name);
  1. LiteCLI
./test.db> .schema
+-------------------------------------------------+
| sql                                             |
+-------------------------------------------------+
| CREATE TABLE people (                           |
|   person_id VARCHAR PRIMARY KEY,                |
|   name VARCHAR,                                 |
|   born INTEGER,                                 |
|   died INTEGER                                  |
| )                                               |
| CREATE INDEX ix_people_name ON people (name)    |
| CREATE INDEX ix_people_name_1 ON people (name)  |
| CREATE TABLE people1(                           |
|   person_id VARCHAR PRIMARY KEY,                |
|   name VARCHAR,                                 |
|   born INTEGER,                                 |
|   died INTEGER                                  |
| )                                               |
| CREATE INDEX ix_people_name_2 ON people1 (name) |
| CREATE TABLE u (name TEXT) STRICT               |
| CREATE TABLE u1 (name TEXT) STRICT              |
| CREATE TABLE u2 (name TEXT) STRICT              |
| CREATE TABLE u3 (name TEXT) STRICT              |
| CREATE TABLE u4 (name TEXT) STRICT              |
+-------------------------------------------------+
Time: 0.004s

+------------------+-------------------------------------------------+
| name             | sql                                             |
+------------------+-------------------------------------------------+
| ix_people_name   | CREATE INDEX ix_people_name ON people (name)    |
| ix_people_name_1 | CREATE INDEX ix_people_name_1 ON people (name)  |
| ix_people_name_2 | CREATE INDEX ix_people_name_2 ON people1 (name) |
+------------------+-------------------------------------------------+

../test.db> .schema people;
+----------------------------------+
| sql                              |
+----------------------------------+
| CREATE TABLE people (            |
|   person_id VARCHAR PRIMARY KEY, |
|   name VARCHAR,                  |
|   born INTEGER,                  |
|   died INTEGER                   |
| )                                |
+----------------------------------+
Time: 0.004s

+------------------+-------------------------------------------------+
| name             | sql                                             |
+------------------+-------------------------------------------------+
| ix_people_name   | CREATE INDEX ix_people_name ON people (name)    |
| ix_people_name_1 | CREATE INDEX ix_people_name_1 ON people (name)  |
| ix_people_name_2 | CREATE INDEX ix_people_name_2 ON people1 (name) |
+------------------+-------------------------------------------------+
Time: 0.003s
../test.db>

Checklist

  • I've added this contribution to the CHANGELOG.md file.

@@ -20,6 +20,8 @@
except ImportError:
llm = None
cli = None
LLM_CLI_COMMANDS = []
MODELS = {}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without these my tests were breaking.

WHERE type = 'index' AND tbl_name LIKE ? AND name NOT LIKE 'sqlite_%'
ORDER BY 1
"""
else:
args = tuple()
query = """
SELECT name FROM sqlite_master
SELECT name, sql FROM sqlite_master
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added an option to return SQL of the index. This is breaking change.


return [(None, tables, headers, status)] + _list_indexes(cur, arg=arg)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per my understanding, l1 + l2or l1.extend(l2)or [*l1, l2]` have similar performance hence used the most intutive one.

@kracekumar kracekumar requested a review from amjith March 9, 2025 22:42
@amjith
Copy link
Member

amjith commented Mar 11, 2025

@kracekumar I see that .schema without a table name returns the table schema and the indexes. The indexes are missing only when we call .schema table_name.

The implementation in this PR shows the indices twice when we run .schema. That's a bit confusing. I'd prefer to match what sqlite3 is doing and show the CREATE TABLE... and CREATE INDEX ... statements in the same output instead of the separate tables as your output shows.

I believe what we need is to change these lines to this query:

            SELECT sql FROM sqlite_master
            WHERE tbl_name=? AND sql IS NOT NULL
            ORDER BY tbl_name, type DESC, name

That should do what we want. Thoughts?

@kracekumar
Copy link
Contributor Author

That's a good call and consistent. I have updated the code to use tbl_nameover name. Can you take a look? Thanks!

@amjith
Copy link
Member

amjith commented Mar 12, 2025

Very nice! Thank you for the quick turnaround. 🥂

@amjith amjith merged commit 0085e60 into main Mar 12, 2025
7 checks passed
@amjith amjith deleted the krace/issue-149-add-index-command-to-schema branch March 12, 2025 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants