# This file is intended to ingest these three tables into snowflake

wh <- getWH_already_on(default="SCIENCE")
sfWarehouseOn(wh, size="M")

schema <- "hidden_content"
dbname <- "prod"

setSnowflake(wh=wh, dbname=dbname)
# sfCreateSchema(schema=schema, dbname="prod")

setGitBranchToSystem(); .g()

{
  if (!DT.merge.bak[, maxn(latest_infrac_by_upc) >= today() - 1])
    warning ("The max date in DT.merge.bak is:  ", DT.merge.bak[, maxn(latest_infrac_by_upc)])
  if (!DT.hidden[, maxn(latest_infrac_by_upc) >= today() - 1])
    warning ("The max date in DT.hidden is:  ", DT.hidden[, maxn(latest_infrac_by_upc)])
  if (!DT.casted[, maxn(latest_infrac_by_upc) >= today() - 1])
    warning ("The max date in DT.casted is:  ", DT.casted[, maxn(latest_infrac_by_upc)])
}

ingestIntoSQL(DT=DT.merge.bak, tbl="all_releases",               schema=schema, datetime_type='TIMESTAMP_TZ', snowflake=TRUE, append=FALSE, drop=TRUE, add.ingestDate=TRUE, wh=wh, dbname=dbname)
ingestIntoSQL(DT=DT.hidden,    tbl="hidden_content_itunes",      schema=schema, datetime_type='TIMESTAMP_TZ', snowflake=TRUE, append=FALSE, drop=TRUE, add.ingestDate=TRUE, wh=wh, dbname=dbname)
ingestIntoSQL(DT=DT.casted,    tbl="hidden_content_itunes_wide", schema=schema, datetime_type='TIMESTAMP_TZ', snowflake=TRUE, append=FALSE, drop=TRUE, add.ingestDate=TRUE, wh=wh, dbname=dbname)


## UPDATE LOOKML
{
  setScienceIfNot(proj="ContentAnalysis", subProj="Hidden")
  sourceSupportFns(proj="Looker")
  create_lookml_model_from_schema(schema=schema, dbname=dbname)

  ## reminder on Lookml
  warning("When updating Lookml, watch for the manual JOINS on the model")
}


## RUN LOCALY BRING IT OVER
if (FALSE)
{
  setScienceIfNot(proj='misc')
  sourceSupportFns(proj='Looker')
  bringAndCopyLookerFiles(f='~rsaporta/git/orch/out/ContentAnalysis/auto_generated_lookmls/hidden_content')

  .o('/Applications/Tower 2.0.app')
}
