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

Overhaul schema command, remove database name #7344

Merged
merged 1 commit into from
Dec 4, 2022

Conversation

rgwood
Copy link
Contributor

@rgwood rgwood commented Dec 4, 2022

This PR changes the schema command for viewing the schema of a SQLite database file. It removes 1 level of nesting (intended to handle multiple databases in the same connection) that I believe is unnecessary.

Before

image

After

image

Rationale

A SQLite database connection can technically be associated with multiple non-temporary databases using the ATTACH DATABASE command. But it's not possible to do that in the context of Nushell, and so I believe that there is no benefit to displaying the schema as if there could be multiple databases.

I initially raised this concern back in April, but we decided to keep the database nesting because at the time we were still looking into more generalized database functionality (i.e. not just SQLite). I believe that rationale no longer applies.

Also, the existing code would not have worked correctly even if a connection had multiple databases; for every database, it was looking up tables without filtering them by database:

pub fn get_databases_and_tables(&self, conn: &Connection) -> Result<Vec<Db>, rusqlite::Error> {
let mut db_query = conn.prepare("SELECT name FROM pragma_database_list")?;
let databases = db_query.query_map([], |row| {
let name: String = row.get(0)?;
Ok(Db::new(name, self.get_tables(conn)?))
})?;
let mut db_list = vec![];
for db in databases {
db_list.push(db?);
}
Ok(db_list)
}

Future Work

I'd like to add information on views+triggers to the schema output. I'm also working on making it possible to ctrl+c reading from a database (which is turning into a massive yak shave).

@rgwood
Copy link
Contributor Author

rgwood commented Dec 4, 2022

@fdncred are you OK with this change?

@fdncred
Copy link
Collaborator

fdncred commented Dec 4, 2022

I like being able to see the file name but I can live with your changes.

@rgwood
Copy link
Contributor Author

rgwood commented Dec 4, 2022

Thanks. My thinking was that the user probably knows the file name already (they probably typed open filename | schema) and it's not really part of the schema.

Maybe later I can upgrade describe so it includes the file path? Currently open foo.db | describe just returns SQLiteDatabase

@rgwood rgwood merged commit e8a55aa into nushell:main Dec 4, 2022
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.

None yet

2 participants