## screen -xRR CarveOuts





      This file just describes the tables
        and outputs the description to an excel sheet. 

      Dave has the SQL files that are actually creating the tables
      











setScience(proj="CarveOuts", create=FALSE, subl=FALSE)
setGitBranchToSystem(); .g()


setDBall("MySql")
tbls_all <- runQry("Show tables")

not_used_tables <- c(
    "agreement_dms_master_restrictions"  ## "biz dev stuff"
  , "release_default_dms_master_restriction"
  , "vendor_proposed_dms_master_restriction"

  ## TEMP TABLES
  , "TEMP_VC_209_vendor_dms_master_restriction" 
  , "TMP_release_territory_restriction"
  , "vendor_dms_master_restriction_TEMP"
  , "vendor_dms_master_restriction_TMP"
)


tbls <- c(
    "release_dms_master_restriction"
  , "release_dms_restriction"
  , "release_territory_restriction"
  , "subaccount_dms_master_restriction"
  , "subaccount_dms_restriction"
  , "subaccount_territory_restriction"
  , "vendor_dms_master_restriction"
  , "track_restriction"
  , "ui_restrictions"
  ## Vendor Contract View
  , "vw_active_vendor_contract"
  ## distribution Type
  , "distribution_type"
)

wh.other_tbls <- extract("restriction", tbls_all[[1]]) %>% setdiff(.,c(tbls, not_used_tables))
if (any(wh.other_tbls)) {
  warning("\nThere are other 'restriction' tables not currently accounted for: ", pasteC(c("", wh.other_tbls), C="\n\t") )
}

ll_DTs.descr <- emptylist(tbls)
for (tbl in tbls) {
  ll_DTs.descr[[tbl]] <- runQry(sprintf("DESC %s", tbl))
}

ll_DTs.data <- emptylist(tbls)
for (tbl in tbls) {
  ll_DTs.data[[tbl]] <- runQry(sprintf("SELECT * FROM %s LIMIT 800", tbl))
}

jesusForData(ll_DTs.data)
jesusForData(ll_DTs.descr)

.ljo("~rsaporta/git/orch/data/CarveOuts/ll_DTs.data-20160505_1730-10_len.RDS")
.ljo("~rsaporta/git/orch/data/CarveOuts/ll_DTs.descr-20160506_0900-11_len.RDS")

ll_DTs.data
ll_DTs.descr

## add a "common_name" column (to be filled out manually) to each description
manual_columns_to_add <- c("description", "common_name", "foreign_key_to", "used_in_Snowflake", "maps_to_in_snowflake")
for (nm in names(ll_DTs.descr)) {
  ll_DTs.descr[[nm]][, (manual_columns_to_add) := ""] [, common_name := Field][, description := "            "]
    # setcolorderpt(endCols=manual_columns_to_add)
}

## One Time
f.notes <- notes.p("carveout_inputtable_descriptions", ext="xlsx")
if (!file.exists(f.notes)) {
  exportXLS.usingXLConnect(f.out=f.notes, DTs.list=ll_DTs.descr)
}
.o(f.notes)




============================= 

Move to Snowflake

f.outs <- emptylist(tbls)
for (tbl in tbls) {
  DT <- runQry(sprintf("SELECT * FROM %s", tbl))

}

