Skip to content

fix #284: mising param handling for versioned_table statement(s) in positional_args_to_dict L485 in manager.py - #283

Closed
indiVar0508 wants to merge 2 commits into
sqlalchemy-continuum:masterfrom
indiVar0508:fix_association_sqlite_datetime
Closed

indiVar0508 wants to merge 2 commits into
sqlalchemy-continuum:masterfrom
indiVar0508:fix_association_sqlite_datetime

Conversation

@indiVar0508

Copy link
Copy Markdown
Contributor

Hi,

Raising PR to address #282 , could you please review it and let know of feedback on comments in code or any other changes needed

Before Change pytest run summary

346 failed, 308 passed, 14 skipped, 5398 warnings, 90 errors in 439.71s

After Change pytest run summary

346 failed, 308 passed, 14 skipped, 5399 warnings, 90 errors in 422.88s

Thanks

… versioned_table in positional_args_to_dict
@indiVar0508 indiVar0508 changed the title fix #282: datetime handling for versioned_table statement(s) in positional_args_to_dict L485 in manager.py fix #282: datetime and mising param(#284) handling for versioned_table statement(s) in positional_args_to_dict L485 in manager.py Aug 22, 2022
@marksteward

Copy link
Copy Markdown
Collaborator

I'm not convinced by these fixes. I think the function needs rewriting really, as INSERT can take many forms under SQLA these days.

@marksteward
marksteward marked this pull request as draft August 22, 2022 18:22
elif op == Operation.INSERT:
columns = table.columns.values()
for index, column in enumerate(columns):
if column.type.__class__.__name__.lower() in ('datetime', ...): # todo add any other type of datetime column?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When checking for class - prefer using isinstance(column.type, CLASSNAME)
In this case - isinstance(column.type, sa.DateTime)

But even if you do that - there is no reliable way to get the "type" here. Some DBs use TIMESTAMP, some use DATETIME, TIMETZ, DATE, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks for the feedback tried to address this in #295

if column.type.__class__.__name__.lower() in ('datetime', ...): # todo add any other type of datetime column?
# from dateutil.parser import parse # dateutil not part of requirement in setup.py so commenting
# parameters[column.name] = parse(params[index])
parameters[column.name] = datetime.fromisoformat(params[index]) # '2022-08-18 04:45:36.942711' params contains ISO Formated date

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We are assuming the params is an ISO formatted date here - but that is only in sqlite
So, this may be breaking in mysql, postgres, etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks for the feedback tried to address this in #295

@indiVar0508 indiVar0508 changed the title fix #282: datetime and mising param(#284) handling for versioned_table statement(s) in positional_args_to_dict L485 in manager.py fix #284: mising param handling for versioned_table statement(s) in positional_args_to_dict L485 in manager.py Aug 27, 2022
@indiVar0508

indiVar0508 commented Aug 27, 2022 •

Copy link
Copy Markdown
Contributor Author

separated the PR for #282 in #295

I'm not convinced by these fixes. I think the function needs rewriting really, as INSERT can take many forms under SQLA these days.

Was trying to make an attempt to write a new version for this method but have couple of doubts, so far i made WIP changes which look like this


    def positional_args_to_dict(self, op, statement, params, tablename):
        """
        On some drivers (eg sqlite) generated INSERT statements use positional
        args instead of key value dictionary. This method converts positional
        args to key value dict.

        :param statement: SQL statement string
        :param params: tuple or dict of statement parameters

        returns: dict with parameters
        """
        if isinstance(params, tuple) and params:
            parameters = {}
            table = self.metadata.tables[tablename]
            if op == Operation.DELETE:
                columns = table.primary_key.columns.values()  ## what happens if not all primary key are not used while making a delete query with SQLA
            else:
                columns = table.columns.values()
            for index, column in enumerate(columns):
                parameters[column] = params[index]
            return parameters
        return params if params else {}

I had a couple doubts

  1. I am facing an issue which i think exists in existing function in master also, that if we try to delete an assoc table entry for one specific primary key and i have two or more primary key(s) defined in assoc table i get index out of bound error

book_author_table = Table(
'book_author',
Base.metadata,
Column('book_id', Integer, ForeignKey('book.id'), primary_key=True, nullable=False),
Column('author_id', Integer, ForeignKey('author.id'), primary_key=True, nullable=False),
...
)
book_author_table.delete(whereclause=book_author_table.book_id==XYZ)

parameters[column.name] = params[index]
IndexError: tuple index out of range

and is it ok to always pull primary_key lookup to delete a association entry as a deletion can also happen based on create date or any other non-primary column e.g "Delete all assoc entries prior to 2020 or delete all assoc entries where event was = ABC"

  1. another doubt that i had was existing method detects the table name using regex is it ok if we use the same table_name from track_association_operations for a given set of params.

@indiVar0508

Copy link
Copy Markdown
Contributor Author

Closing this as #295 addresses this issue

@indiVar0508 indiVar0508 closed this Sep 3, 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.

3 participants