Skip to content

Instantly share code, notes, and snippets.

@freider
Created February 5, 2014 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freider/8824473 to your computer and use it in GitHub Desktop.
Save freider/8824473 to your computer and use it in GitHub Desktop.
class SmartPostgresTarget(PostgresTarget):
"""PostgresTarget with possibility to specify an existence query"""
def __init__(self, host, database, user, password,
table,
exists_query,
update_id=None):
update_id = update_id or exists_query
self.exists_query = exists_query
super(SmartPostgresTarget, self).__init__(
host, database, user, password,
table, update_id)
def exists(self, connection=None):
""" Checks if data exists according to normal PostgresTarget cache
If NOT, check it using a custom exists query and if this says data
exists, touch() the target to refresh the cache """
cache_complete = super(SmartPostgresTarget, self).complete(connection)
if cache_complete:
return True
if connection is None:
connection = self.connect()
connection.autocommit = True
cursor = connection.cursor()
cursor.execute(self.exists_query)
row = cursor.fetchone()
cursor.close()
exists = row is not None
if exists:
self.touch(connection)
return exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment