simonw / db-to-sqlite

CLI tool for exporting tables or queries from any SQL database to a SQLite file
Apache License 2.0
370 stars 29 forks source link

"user" table in Postgres isn't copied #25

Closed laggingreflex closed 3 years ago

laggingreflex commented 4 years ago

If you have a table named "user" in PostgreSQL it isn't copied.

It does create the table, but the entries do not reflect the original table. The entries I see are just one row "1 postgres".

It seems the issue is that in Postgres user is a pseudo-function keyword

simonw commented 3 years ago

Presumably this is caused by: https://github.com/simonw/db-to-sqlite/blob/bd199449367e32650b399041eec837982f016da7/db_to_sqlite/cli.py#L88-L92

That select * from {table} query needs to somehow grow awareness of database engine specific escaping conventions for table names.

simonw commented 3 years ago

Looks like I need to figure out how to call quote_identifier in SQLAlchemy for the current engine's database dialect: https://docs.sqlalchemy.org/en/14/core/internals.html#sqlalchemy.sql.compiler.IdentifierPreparer.quote_identifier

simonw commented 3 years ago

Searched here: https://github.com/search?l=Python&q=engine+quote_identifier&type=Code

https://github.com/bluelabsio/records-mover/blob/e3b71d6c09d99d0bcd6a956b9d09d20f8abe98d2/records_mover/db/quoting.py#L60

def quote_group_name(db: Union[Connection, Engine], group_name: str) -> str:
    dialect = db.dialect
    return dialect.preparer(dialect).quote_identifier(group_name)

Or maybe https://github.com/sqlalchemy/sqlalchemy/blob/ea225c63f98dc3c250cee1b243762970b8e0932a/test/engine/test_reflection.py#L1111

        quoter = connection.dialect.identifier_preparer.quote_identifier