Skip to content

Instantly share code, notes, and snippets.

@DazWilkin
Last active January 12, 2018 21:34
Show Gist options
  • Save DazWilkin/8796da83acd5181c9ace8de29a1e28c1 to your computer and use it in GitHub Desktop.
Save DazWilkin/8796da83acd5181c9ace8de29a1e28c1 to your computer and use it in GitHub Desktop.
# -*-coding=utf-8 -*-
import json
from apiclient.discovery import build
from datetime import datetime, date, time, timedelta
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
SERVICE_NAME = "bigquery"
SERVICE_VERSION = "v2"
PROJECT = [[YOUR-PROJECT]]
DATASET = [[YOUR-DATASET]]
EPOCH = datetime.utcfromtimestamp(0)
EXPIRATION_DAYS = +60
MIDNIGHT = datetime.combine(datetime.utcnow(), time.min)
NEW_EXPIRATION = long((MIDNIGHT + timedelta(days=EXPIRATION_DAYS) - EPOCH).total_seconds()) * 1000
service = build(
SERVICE_NAME,
SERVICE_VERSION,
credentials=credentials
)
tables = service.tables()
tables_list_request = tables.list(
projectId=PROJECT,
datasetId=DATASET,
)
while tables_list_request is not None:
tables_list_response = tables_list_request.execute()
for table in tables_list_response["tables"]:
tableId = table.get("tableReference").get("tableId")
creationTime = table.get("creationTime")
expirationTime = table.get("expirationTime")
if (NEW_EXPIRATION != long(expirationTime)):
# Patch the table's expiration time to NEW_EXPIRATION
tables_patch_request = tables.patch(
projectId=PROJECT,
datasetId=DATASET,
tableId=tableId,
body={
"expirationTime": NEW_EXPIRATION
}
)
tables_patch_response = tables_patch_request.execute()
patched = True
else:
patched = False
print("{} {} {} -- {}".format(
tableId,
datetime.fromtimestamp(int(creationTime)/1000.0),
"Never" if expirationTime is None else datetime.fromtimestamp(int(expirationTime)/1000.0),
"☒ - patched" if patched else "☑"
))
tables_list_request = tables.list_next(
tables_list_request,
tables_list_response
)
google-api-python-client==1.6.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment