sqlalchemy-query-helpers 1.0.9__tar.gz → 1.0.101__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sqlalchemy-query-helpers might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlalchemy-query-helpers
3
- Version: 1.0.9
3
+ Version: 1.0.101
4
4
  Summary: Some helpers for SQLAlchemy
5
5
  Author-email: vladiscripts <blagopoluchie12@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/vladiscripts/sqlalchemy-query-helpers
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "sqlalchemy-query-helpers"
7
- version = "1.0.9"
7
+ version = "1.0.101"
8
8
  authors = [
9
9
  { name = "vladiscripts", email = "blagopoluchie12@gmail.com" },
10
10
  ]
@@ -27,7 +27,8 @@ class DB:
27
27
  self.name = self.engine.url.database
28
28
 
29
29
  def __del__(self):
30
- self.session.close()
30
+ if self.session:
31
+ self.session.close()
31
32
 
32
33
  @staticmethod
33
34
  def make_engine_str(use_os_env) -> str:
@@ -224,15 +225,25 @@ class DB:
224
225
  self.session.commit()
225
226
  return is_updated, is_inserted
226
227
 
227
- def upsert(self, t, rows: Union[list[dict], tuple[dict]], mfields=None, do_commit=True):
228
+ def upsert(self, t, rows: Union[list[dict], tuple[dict]], mfields=None, do_commit=True, filter_unque_primary_keys=True):
228
229
  rows_to_insert = [self.__to_dict(row, mfields) for row in rows]
229
230
  stmt = insert(t).values(rows_to_insert)
230
- update_dict = {x.name: x for x in stmt.inserted}
231
+ # need to remove primary or unique keys on using, else will error
232
+ if filter_unque_primary_keys:
233
+ update_dict = {x.name: x for x in stmt.inserted for c in t._sa_class_manager.mapper.columns._all_columns
234
+ if x.name == c.name and c.unique is not True and c.primary_key is not True}
235
+ else:
236
+ update_dict = {x.name: x for x in stmt.inserted}
237
+ if not update_dict:
238
+ return
231
239
  upsert_query = stmt.on_duplicate_key_update(update_dict)
232
240
  with self.Session() as session:
233
241
  session.execute(upsert_query)
234
242
  if do_commit:
235
- session.commit()
243
+ try:
244
+ session.commit()
245
+ except Exception as e:
246
+ session.rollback()
236
247
 
237
248
  # def upsert(self, t, row, mfields=None):
238
249
  # row = self.to_dict(row, mfields)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlalchemy-query-helpers
3
- Version: 1.0.9
3
+ Version: 1.0.101
4
4
  Summary: Some helpers for SQLAlchemy
5
5
  Author-email: vladiscripts <blagopoluchie12@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/vladiscripts/sqlalchemy-query-helpers