Skip to content

Instantly share code, notes, and snippets.

@GuillaumePressiat
Last active April 18, 2019 09:59
Show Gist options
  • Save GuillaumePressiat/6d8610cd3ce1b0682adb09f9cc6360ce to your computer and use it in GitHub Desktop.
Save GuillaumePressiat/6d8610cd3ce1b0682adb09f9cc6360ce to your computer and use it in GitHub Desktop.
write .er entity relation Database file with R for erd https://github.com/BurntSushi/erd
library(dplyr, warn.conflicts = FALSE)
con_obj <- odbc::dbConnect(odbc::odbc(), "mydatabase")
liste_tables <- DBI::dbListTables(con_obj) %>% .[grepl('some|filter', .)]
erd_write <- function(tab){
d <- capture.output(glimpse(tbl(con_obj, tab)))
noms <- d %>% stringr::str_extract_all('^\\$ .+\\s*\\<') %>%
purrr::flatten_chr() %>%
stringr::str_remove_all('\\s|\\$|\\<')
types <- d %>% stringr::str_extract_all('\\<.*\\>') %>%
purrr::flatten_chr() %>%
stringr::str_remove_all('\\>|\\<')
c(paste0('\n[', tab, '] {bgcolor: "#fcfcfc"}'),
paste0(" ", noms, " {label: \"", types, "\"}")) %>%
paste0(collapse = "\n")
}
erd_write('my_example_table') %>% cat()
descript_tables <- liste_tables %>%
purrr::map(erd_write)
readr::write_lines(descript_tables %>% paste0(collapse = "\n"), '~/mydatabase/myschema/schema_temp.er')
# make a start file, goal is to use https://github.com/BurntSushi/erd after
@GuillaumePressiat
Copy link
Author

nice example of erd script output here:
https://burntsushi.net/stuff/erd/example-nfldb.pdf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment