Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dm_from_con(learn_keys = TRUE) works for MariaDB #1123

Merged
merged 23 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Suggests:
pixarfilms,
pool,
progress,
RMariaDB (>= 1.0.10),
RMariaDB (>= 1.2.2),
rmarkdown,
RPostgres,
RSQLite (>= 2.2.8),
Expand Down
2 changes: 1 addition & 1 deletion R/db-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ repair_table_names_for_db <- function(table_names, temporary, con, schema = NULL
names <- unique_db_table_name(names)
} else {
# permanent tables
if (!is.null(schema) && !is_mssql(con) && !is_postgres(con)) {
if (!is.null(schema) && !is_mssql(con) && !is_postgres(con) && !is_mariadb(con)) {
krlmlr marked this conversation as resolved.
Show resolved Hide resolved
abort_no_schemas_supported(con = con)
}
names <- table_names
Expand Down
5 changes: 5 additions & 0 deletions R/global.R
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ utils::globalVariables(c(
"remote_table_unquoted",
"unique_def",
#
# meta
"referenced_column_name",
"referenced_table_name",
"referenced_table_schema",
#
# keep this to avoid dealing with trailing commas
NULL
))
109 changes: 86 additions & 23 deletions R/meta.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,50 @@ dm_meta_raw <- function(con, catalog) {

local_options(digits.secs = 6)

schemata <- tbl_lc(src, "information_schema.schemata", vars = c(
"catalog_name", "schema_name", "schema_owner", "default_character_set_catalog",
"default_character_set_schema", "default_character_set_name"
schemata <- tbl_lc(src, "information_schema.schemata", vars = vec_c(
"catalog_name", "schema_name", "default_character_set_name",
# Optional, not MySQL:
# "schema_owner", "default_character_set_catalog", "default_character_set_schema",
))
tables <- tbl_lc(src, "information_schema.tables", vars = c(
"table_catalog", "table_schema", "table_name", "table_type"
tables <- tbl_lc(src, "information_schema.tables", vars = vec_c(
"table_catalog", "table_schema", "table_name", "table_type",
))
columns <- tbl_lc(src, "information_schema.columns", vars = c(
columns <- tbl_lc(src, "information_schema.columns", vars = vec_c(
"table_catalog", "table_schema", "table_name", "column_name",
"ordinal_position", "column_default", "is_nullable", "data_type",
"character_maximum_length", "character_octet_length", "numeric_precision",
"numeric_precision_radix", "numeric_scale", "datetime_precision",
"character_set_catalog", "character_set_schema", "character_set_name",
"collation_catalog", "collation_schema", "collation_name", "domain_catalog",
"domain_schema", "domain_name"
))
table_constraints <- tbl_lc(src, "information_schema.table_constraints", vars = c(
"constraint_catalog", "constraint_schema", "constraint_name",
"table_catalog", "table_schema", "table_name", "constraint_type",
"is_deferrable", "initially_deferred"
"numeric_scale", "datetime_precision",
"character_set_name", "collation_name",

# Optional, not RMySQL:
# "numeric_precision_radix",
# "character_set_catalog", "character_set_schema",
# "collation_catalog", "collation_schema", "domain_catalog",
# "domain_schema", "domain_name"
))
key_column_usage <- tbl_lc(src, "information_schema.key_column_usage", vars = c(

if (is_mariadb(src)) {
table_constraints <- tbl_lc(src, "information_schema.table_constraints", vars = vec_c(
"constraint_catalog", "constraint_schema", "constraint_name",
"table_name", "constraint_type"
)) %>%
mutate(table_catalog = constraint_catalog, table_schema = constraint_schema, .before = table_name) %>%
mutate(constraint_name = if_else(constraint_type == "PRIMARY KEY", paste0("pk_", table_name), constraint_name)) %>%
# WAT
mutate(constraint_schema = tolower(constraint_schema)) %>%
mutate(table_schema = tolower(table_schema))
} else {
table_constraints <- tbl_lc(src, "information_schema.table_constraints", vars = vec_c(
"constraint_catalog", "constraint_schema", "constraint_name",
"table_catalog", "table_schema", "table_name", "constraint_type",
"is_deferrable", "initially_deferred",
))
}

key_column_usage <- tbl_lc(src, "information_schema.key_column_usage", vars = vec_c(
"constraint_catalog", "constraint_schema", "constraint_name",
"table_catalog", "table_schema", "table_name", "column_name",
"ordinal_position"
"ordinal_position",
))

if (is_postgres(src)) {
Expand All @@ -82,6 +101,39 @@ dm_meta_raw <- function(con, catalog) {
))
} else if (is_mssql(src)) {
constraint_column_usage <- mssql_constraint_column_usage(src, table_constraints, catalog)
} else {
# Alternate constraint names for uniqueness
key_column_usage <-
key_column_usage %>%
left_join(
tbl_lc(src, "information_schema.table_constraints", vars = vec_c(
"constraint_catalog", "constraint_schema", "constraint_name",
"table_name", "constraint_type"
)),
by = vec_c(
"constraint_catalog", "constraint_schema", "constraint_name",
"table_name",
)
) %>%
mutate(constraint_name = if_else(constraint_type == "PRIMARY KEY", paste0("pk_", table_name), constraint_name)) %>%
select(-constraint_type)

constraint_column_usage <-
tbl_lc(src, "information_schema.key_column_usage", vars = c(
"table_catalog",
"referenced_table_schema", "referenced_table_name", "referenced_column_name",
"constraint_catalog", "constraint_schema", "constraint_name",
"ordinal_position"
)) %>%
filter(!is.na(referenced_table_name)) %>%
rename(
table_schema = referenced_table_schema,
table_name = referenced_table_name,
column_name = referenced_column_name,
) %>%
# WAT
mutate(constraint_schema = tolower(constraint_schema)) %>%
mutate(table_schema = tolower(table_schema))
}

dm(schemata, tables, columns, table_constraints, key_column_usage, constraint_column_usage) %>%
Expand Down Expand Up @@ -145,13 +197,24 @@ dm_meta_simple_add_keys <- function(dm_meta) {
}

tbl_lc <- function(con, name, vars) {
from <- paste0(
"SELECT ",
paste0(DBI::dbQuoteIdentifier(con_from_src_or_con(con), vars), collapse = ", "),
"\nFROM ", name
)
# For discovery only!
if (is.null(vars)) {
from <- name
} else {
from <- sql(paste0(
"SELECT ",
paste0(DBI::dbQuoteIdentifier(con_from_src_or_con(con), vars), collapse = ", "),
"\nFROM ", name
))
}

tbl(con, sql(from), vars = vars)
out <- tbl(con, from, vars = vars)
if (is.null(vars)) {
out <-
out %>%
rename(!!!set_names(colnames(out), tolower(colnames(out))))
}
out
}

select_dm_meta <- function(dm_meta) {
Expand Down
Loading