-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataMigrationManager.R
344 lines (315 loc) · 12.4 KB
/
DataMigrationManager.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# Copyright 2023 Observational Health Data Sciences and Informatics
#
# This file is part of CohortDiagnostics
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
isRmdCheck <- function() {
return(Sys.getenv("_R_CHECK_PACKAGE_NAME_", "") != "")
}
isUnitTest <- function() {
return(tolower(Sys.getenv("TESTTHAT", "")) == "true")
}
.defaultMigrationRegexp <- "(Migration_([0-9]+))-(.+).sql"
# Taken from database connector - may be better if the package exposed this list formally
.availableDbms <- c(
"oracle",
"hive",
"postgresql",
"redshift",
"sql server",
"pdw",
"netezza",
"impala",
"bigquery",
"sqlite",
"sqlite extended",
"spark",
"hive"
)
#' DataMigrationManager (DMM)
#' @description
#' R6 class for management of database migration
#'
#' @field migrationPath Path migrations exist in
#' @field databaseSchema Path migrations exist in
#' @field packageName packageName, can be null
#' @field tablePrefix packageName, can be null
#'
#' @importFrom ParallelLogger logError logInfo
#'
#' @export DataMigrationManager
DataMigrationManager <- R6::R6Class(
classname = "DataMigrationManager",
public = list(
migrationPath = NULL,
tablePrefix = "",
databaseSchema = NULL,
packageName = NULL,
#'
#' @param connectionDetails DatabaseConnector connection details object
#' @param databaseSchema Database Schema to execute on
#' @param tablePrefix Optional table prefix for all tables (e.g. plp, cm, cd etc)
#' @param packageName If in package mode, the name of the R package
#' @param migrationPath Path to location of migration sql files. If in package mode, this should just
#' be a folder (e.g. "migrations") that lives in the location "sql/sql_server" (and)
#' other database platforms.
#' If in folder model, the folder must include "sql_server" in the relative path,
#' (e.g if migrationPath = 'migrations' then the folder 'migrations/sql_server' should exists)
#' @param migrationRegexp (Optional) regular expression pattern default is `(Migration_([0-9]+))-(.+).sql`
initialize = function(connectionDetails,
databaseSchema,
tablePrefix = "",
migrationPath,
packageName = NULL,
migrationRegexp = .defaultMigrationRegexp) {
checkmate::checkString(databaseSchema)
checkmate::checkString(tablePrefix)
checkmate::checkString(migrationPath)
checkmate::checkClass(connectionDetails, "connectionDetails")
# Set required variables
self$tablePrefix <- tablePrefix
self$databaseSchema <- databaseSchema
private$migrationRegexp <- migrationRegexp
self$packageName <- packageName
private$connectionDetails <- connectionDetails
private$connectionHandler <- ConnectionHandler$new(connectionDetails, loadConnection = FALSE)
self$migrationPath <- migrationPath
if (self$isPackage()) {
private$logInfo("Migrator using SQL files in ", packageName)
} else {
private$logInfo("Migrator using SQL files in directory structure")
}
},
#' Migration table exists
#' @description
#' Check if migration table is present in schema
#' @return boolean
migrationTableExists = function() {
tables <- DatabaseConnector::getTableNames(private$connectionHandler$getConnection(), self$databaseSchema)
return(tolower(paste0(self$tablePrefix, "migration")) %in% tables)
},
#' Get path of migrations
#' @description
#' Get path to sql migration files
#' @param dbms Optionally specify the dbms that the migration fits under
getMigrationsPath = function(dbms = "sql server") {
checkmate::assertChoice(dbms, .availableDbms)
if (!self$isPackage()) {
path <- file.path(self$migrationPath, gsub(" ", "_", dbms))
} else {
path <- system.file(file.path("sql", gsub(" ", "_", dbms), self$migrationPath), package = self$packageName)
}
return(path)
},
#' Get status of result model
#' @description
#' Get status of all migrations (executed or not)
#' @returns data frame all migrations, including file name, order and execution status
getStatus = function() {
allMigrations <- list.files(self$getMigrationsPath(), pattern = "*.sql")
if (length(allMigrations) == 0) {
return(data.frame())
}
migrationsExecuted <- private$getCompletedMigrations()
if (nrow(migrationsExecuted) > 0) {
migrationsExecuted$executed <- TRUE
migrationsToExecute <- setdiff(allMigrations, migrationsExecuted$migrationFile)
migrations <- private$getMigrationOrder(migrationsToExecute)
if (nrow(migrations) > 0) {
migrations$executed <- FALSE
}
migrations <- rbind(migrationsExecuted, migrations)
migrations[order(migrations$migrationOrder), ]
} else {
migrations <- private$getMigrationOrder(allMigrations)
migrations$executed <- FALSE
}
return(migrations)
},
#' Get connection handler
#' @description
#' Return connection handler instance
#' @seealso[ConnectionHandler] for information on returned class
#' @return ConnectionHandler instance
getConnectionHandler = function() {
return(private$connectionHandler)
},
#' Check migrations in folder
#' @description
#' Check if file names are valid for migrations
check = function() {
baseFiles <- list.files(self$getMigrationsPath(), pattern = "*.sql")
allNameValidity <- c()
allDirValid <- c()
# Check if files exist for all db platforms
for (dbms in .availableDbms) {
# Check to see if files follow pattern
sqlFiles <- list.files(self$getMigrationsPath(dbms = dbms), pattern = "*.sql")
fileNameValidity <- grepl(private$migrationRegexp, sqlFiles)
if (any(fileNameValidity == 0)) {
private$logError(paste("File name not valid", sqlFiles[fileNameValidity == 0], collapse = "\n"))
}
allNameValidity <- c(allNameValidity, fileNameValidity)
if (dbms != "sql server") {
for (file in sqlFiles) {
if (!file %in% baseFiles) {
allDirValid <- c(allDirValid, FALSE)
private$logError(paste(
"File ", file.path(dbms, file),
"not found in sql server dir. Even if migration is platform specific ",
"it should be mached in sql_server with dummy sql"
))
} else {
allDirValid <- c(allDirValid, TRUE)
}
}
}
}
return(all(allNameValidity > 0))
},
#' Execute Migrations
#' @description
#' Execute any unexecuted migrations
#' @param stopMigrationVersion (Optional) Migrate to a specific migration number
executeMigrations = function(stopMigrationVersion = NULL) {
if (!self$check()) {
stop("Check failed, aborting migrations until issues are resolved")
}
# if migrations table doesn't exist, create it
if (!self$migrationTableExists()) {
private$createMigrationsTable()
}
# load list of migrations
migrations <- self$getStatus()
# execute migrations that haven't been executed yet
migrations <- migrations[!migrations$executed, ]
if (nrow(migrations) > 0) {
if (is.null(stopMigrationVersion)) {
stopMigrationVersion <- max(migrations$migrationOrder)
}
for (i in 1:nrow(migrations)) {
migration <- migrations[i, ]
if (isTRUE(migration$migrationOrder <= stopMigrationVersion)) {
private$executeMigration(migration)
}
}
}
},
#' isPackage
#' @description
#' is a package folder structure or not
isPackage = function() {
return(!is.null(self$packageName))
},
#' finalize
#' @description close database connection
finalize = function() {
private$connectionHandler$finalize()
}
),
private = list(
migrationRegexp = NULL,
connectionHandler = NULL,
connectionDetails = NULL,
executeMigration = function(migration) {
private$logInfo("Executing migration: ", migration$migrationFile)
# Load, render, translate and execute sql
if (self$isPackage()) {
sql <- SqlRender::loadRenderTranslateSql(file.path(self$migrationPath, migration$migrationFile),
dbms = private$connectionDetails$dbms,
database_schema = self$databaseSchema,
table_prefix = self$tablePrefix,
packageName = self$packageName
)
private$connectionHandler$executeSql(sql)
} else {
# Check to see if a file for database platform exists
if (file.exists(file.path(self$migrationPath, private$connectionDetails$dbms, migration$migrationFile))) {
sql <- SqlRender::readSql(file.path(self$migrationPath, private$connectionDetails$dbms, migration$migrationFile))
} else {
# Default to using sql_server as path
sql <- SqlRender::readSql(file.path(self$migrationPath, "sql_server", migration$migrationFile))
}
private$connectionHandler$executeSql(sql,
database_schema = self$databaseSchema,
table_prefix = self$tablePrefix
)
}
private$logInfo("Saving migration: ", migration$migrationFile)
# Save migration in set of migrations
iSql <- "
{DEFAULT @migration = migration}
INSERT INTO @database_schema.@table_prefix@migration (migration_file, migration_order)
VALUES ('@migration_file', @order);
"
private$connectionHandler$executeSql(iSql,
database_schema = self$databaseSchema,
migration_file = migration$migrationFile,
table_prefix = self$tablePrefix,
order = migration$migrationOrder
)
private$logInfo("Migration complete ", migration$migrationFile)
},
createMigrationsTable = function() {
private$logInfo("Creating migrations table")
sql <- "
{DEFAULT @migration = migration}
--HINT DISTRIBUTE ON RANDOM
CREATE TABLE @database_schema.@table_prefix@migration (
migration_file VARCHAR PRIMARY KEY, --string value represents file name
migration_order INT NOT NULL unique
);"
private$connectionHandler$executeSql(sql,
database_schema = self$databaseSchema,
table_prefix = self$tablePrefix
)
private$logInfo("Migrations table created")
},
getCompletedMigrations = function() {
if (!self$migrationTableExists()) {
return(data.frame())
}
sql <- "
{DEFAULT @migration = migration}
SELECT migration_file, migration_order FROM @database_schema.@table_prefix@migration ORDER BY migration_order;"
migrationsExecuted <- private$connectionHandler$queryDb(sql,
database_schema = self$databaseSchema,
table_prefix = self$tablePrefix
)
return(migrationsExecuted)
},
# Convert string patterns to int
getMigrationOrder = function(migrations) {
execution <- data.frame(
migrationFile = migrations,
migrationOrder = as.integer(gsub(private$migrationRegexp, "\\2", migrations))
)
return(execution[order(execution$migrationOrder), ])
},
# Wrapper calls to stop ParallelLogger outputting annoying messages
logError = function(...) {
if (isUnitTest() | isRmdCheck()) {
writeLines(text = .makeMessage(...))
} else {
ParallelLogger::logError(...)
}
},
logInfo = function(...) {
if (isUnitTest() | isRmdCheck()) {
writeLines(text = .makeMessage(...))
} else {
ParallelLogger::logInfo(...)
}
}
),
)