Created
February 5, 2014 14:17
-
-
Save freider/8824473 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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