Skip to content

Commit

Permalink
fix #184
Browse files Browse the repository at this point in the history
  • Loading branch information
smnorris committed Jun 28, 2024
1 parent 239d343 commit 090becc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
21 changes: 4 additions & 17 deletions bcdata/bc2pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ def bc2pg( # noqa: C901
timestamp=True,
schema_only=False,
append=False,
refresh=False,
):
"""Request table definition from bcdc and replicate in postgres"""
if schema_only and append:
Expand Down Expand Up @@ -228,22 +229,8 @@ def bc2pg( # noqa: C901
df = None

# once load complete, note date/time of load completion in bcdata.log
if timestamp:
log.info("Logging download date to bcdata.log")
db.execute(
"""CREATE SCHEMA IF NOT EXISTS bcdata;
CREATE TABLE IF NOT EXISTS bcdata.log (
table_name text PRIMARY KEY,
latest_download timestamp WITH TIME ZONE
);
"""
)
db.execute(
"""INSERT INTO bcdata.log (table_name, latest_download)
SELECT %s as table_name, NOW() as latest_download
ON CONFLICT (table_name) DO UPDATE SET latest_download = NOW();
""",
(schema_name + "." + table_name,),
)
# do not log refreshes here, they called by cli once loaded to target table
if timestamp and not refresh:
db.log(schema_name, table_name)

return schema_name + "." + table_name
2 changes: 2 additions & 0 deletions bcdata/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ def bc2pg(
timestamp=timestamp,
schema_only=schema_only,
append=append,
refresh=refresh,
)

# if refreshing, flush from temp bcdata schema to target schema
Expand All @@ -437,6 +438,7 @@ def bc2pg(
s, table = out_table.split(".")
db.refresh(schema_target, table)
out_table = schema_target + "." + table
db.log(schema_target, table)

# do not notify of data load completion when no data load has occured
if not schema_only:
Expand Down
18 changes: 18 additions & 0 deletions bcdata/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,21 @@ def get_columns(self, schema, table):
metadata_obj = MetaData(schema=schema)
table = Table(table, metadata_obj, schema=schema, autoload_with=self.engine)
return list(table.columns.keys())

def log(self, schema_name, table_name):
log.info("Logging download date to bcdata.log")
self.execute(
"""CREATE SCHEMA IF NOT EXISTS bcdata;
CREATE TABLE IF NOT EXISTS bcdata.log (
table_name text PRIMARY KEY,
latest_download timestamp WITH TIME ZONE
);
"""
)
self.execute(
"""INSERT INTO bcdata.log (table_name, latest_download)
SELECT %s as table_name, NOW() as latest_download
ON CONFLICT (table_name) DO UPDATE SET latest_download = NOW();
""",
(schema_name + "." + table_name,),
)

0 comments on commit 090becc

Please sign in to comment.