if (!exists("origin"))
  origin <- "1970-01-01"

# Pre-Clean
## ------------------------------ ##
try(dbDisconnect(con), silent=TRUE)
try(detach(package:RPostgreSQL), silent=TRUE)
library(RPostgreSQL)
## ------------------------------ ##


## ------------------------------ ##
##    Query
## ------------------------------ ##

upcusings <- DB.all.meta[!is.na(releaseDate) & !is.na(upc)
                  ][ releaseDate >= minReleaseDate & releaseDate <= maxReleaseDate, unique(upc)]

DB.using <- DB.all.count[.(upcusings)]


# First ensure character columns where needed
if (!is.character(DB.using[["upc"]]))
  DB.using[, upc := as.idcol(upc)]

if (!is.character(DB.using[["user_country"]]))
  DB.using[, user_country := as.character(user_country)]

setkeyIfNot(DB.using, "upc")

# ----------------------------


## TODO:  Might be able to just use DB.all.countmeta instead, and skip the next part.  oh well. not too slow. 

## Add in metadata
matchKey(DB.using, DB.all.meta, "upc")

DB.using[DB.all.meta, 
      `:=`(artist=artist
         , release=release
         , mkt_priority=mkt_priority
         , genre=genre
         , releaseDate=releaseDate)
      ]

# Clean up Date Cols
DB.using[, releaseDate := as.Date(releaseDate, origin=origin)]


### Drop any rows where download_date is NA
DB.using <- DB.using[!is.na(download_date)]



# ----------------------------
jesusForData(DB.using, git=TRUE)
# ----------------------------



## ----------------  This was the old QRY Pull for DB.using.  Keeping it here for reference --------------- ##
##
## NOTE:  `DB.using` (the raw QRY pull) is simply the (now corrected) `DB.all.count`
##         restricted to upcusings
##
##   OLD: 
##                  
## ## TODO:  Check Globally
## QRY.DB.using <- paste("SELECT upc, download_date, COUNT(customerid) AS count, country AS user_country",
##                        "FROM   production.staging_raw_spotify",
##                        "WHERE  upc in ", pasteQ(upcusings), 
## #  WRONG:  No date min max needed here
## #                       "AND    download_date >=", pasteQ(minReleaseDate), 
## #                       "AND    download_date <=", pasteQ(maxReleaseDate), 
##                        "AND    country = 'US'",
##                        "GROUP BY upc, download_date, country")
## 
## # ~ About 1 second per day.  
## DB.using <- runQry(qry=QRY.DB.using, verbose=TRUE)
## ## ------------------------------ ##
## 
## # Cleanup Again
## ## ------------------------------ ##
## try(dbDisconnect(con), silent=TRUE)
## try(detach(package:RPostgreSQL), silent=TRUE)
## ## ------------------------------ ##
##
## ---------------- END:   This was the old QRY Pull for DB.using.  Keeping it here for reference --------------- ##




