migration_script_template.lua
上传用户:cccombo
上传日期:2021-01-31
资源大小:16445k
文件大小:7k
源码类别:

MySQL数据库

开发平台:

SQL

  1. -- ------------------------------------------------------------------
  2. -- MySQL Migration Toolkit - Migration script
  3. -- ------------------------------------------------------------------
  4. -- ------------------------------------------------------------------
  5. -- Initialize the migration environment
  6. Migration:initMigration()
  7. -- set options
  8. doWriteCreateScript= false
  9. doWriteInsertScript= false
  10. grtV.setGlobal("/migration/applicationData/reverseEngineerOnlyTableObjects", %reverseEngineerOnlyTableObjects%)
  11. -- ------------------------------------------------------------------
  12. -- checkpoint 0
  13. -- Set source and target connection
  14. do
  15.   -- Set source connection
  16.   print("Set source connection.")
  17.   grtV.setGlobal("/migration/sourceConnection", %sourceConnection%);
  18.   -- set struct and types
  19.   grtS.set(grtV.getGlobal("/migration/sourceConnection"), "db.mgmt.Connection")
  20.   grtV.setContentType(grtV.getGlobal("/migration/sourceConnection/parameterValues"), "string")
  21.   grtV.setContentType(grtV.getGlobal("/migration/sourceConnection/modules"), "string")
  22.   sourceConn= grtV.getGlobal("/migration/sourceConnection")
  23.   sourceRdbmsName= grtV.toLua(sourceConn.driver.owner.name)
  24.   -- Set target connection
  25.   print("Set target connection.")
  26.   grtV.setGlobal("/migration/targetConnection", %targetConnection%);
  27.   -- set struct and types
  28.   grtS.set(grtV.getGlobal("/migration/targetConnection"), "db.mgmt.Connection")
  29.   grtV.setContentType(grtV.getGlobal("/migration/targetConnection/parameterValues"), "string")
  30.   grtV.setContentType(grtV.getGlobal("/migration/targetConnection/modules"), "string")
  31.   targetConn= grtV.getGlobal("/migration/targetConnection")
  32.   targetRdbmsName= grtV.toLua(targetConn.driver.owner.name)
  33.   -- Test connections
  34.   print("Test source connection to " .. sourceRdbmsName .. " ...")
  35.   res= grtM.callFunction(grtV.toLua(sourceConn.modules.ReverseEngineeringModule), "getVersion", sourceConn)
  36.   grt.exitOnError("The connection to the source " .. sourceRdbmsName .. " database could not be established.")
  37.   print("Test target connection to " .. targetRdbmsName .. " ...")
  38.   res= grtM.callFunction(grtV.toLua(targetConn.modules.ReverseEngineeringModule), "getVersion", targetConn)
  39.   grt.exitOnError("The connection to the target " .. targetRdbmsName .. " database could not be established.")
  40.   -- store target version for the migration process
  41.   grtV.setGlobal("/migration/targetVersion", res)
  42. end
  43. -- ------------------------------------------------------------------
  44. -- checkpoint 1
  45. -- Do the reverse engineering
  46. do
  47.   print("Reverse engineering " .. sourceRdbmsName .. " ...")
  48.   res= grtM.callFunction(grtV.toLua(sourceConn.modules.ReverseEngineeringModule), "reverseEngineer",
  49.     {sourceConn, {%sourceSchemataList%}}
  50.   )
  51.   grt.exitOnError("The source " .. sourceRdbmsName .. " database could not be reverse engineered")
  52.   grtV.setGlobal("/migration/sourceCatalog", res)
  53. end
  54. -- ------------------------------------------------------------------
  55. -- checkpoint 2
  56. -- Migration methods and ignore list
  57. do
  58.   print("Get available migration methods.")
  59.   res= grtM.callFunction(grtV.toLua(sourceConn.modules.MigrationModule), "migrationMethods", nil)
  60.   grt.exitOnError("The migration methods cannot be fetched.")
  61.   grtV.setGlobal("/migration/migrationMethods", res)
  62.   -- generate an ignore list
  63.   print("Setting up ignore list.")
  64.   grtV.setGlobal("/migration/ignoreList", {%ignoreList%})
  65. end
  66. -- ------------------------------------------------------------------
  67. -- checkpoint 3
  68. -- Set object mappings and to migration
  69. do
  70.   print("Set object mappings.")
  71.   grtV.setGlobal("/migration/mappingDefaults", %mappingDefaults%)
  72.   grtV.setContentType(grtV.getGlobal("/migration/mappingDefaults"), "dict", "db.migration.Mapping")
  73.   %mappingDefaultsStructs%
  74.   -- update _ids
  75.   local mappingDefaults= grtV.getGlobal("/migration/mappingDefaults")
  76.   local migrationMethods= grtV.getGlobal("/migration/migrationMethods")
  77.   for i= 1, grtV.getn(mappingDefaults) do
  78.     for j= 1, grtV.getn(migrationMethods) do
  79.       if grtV.toLua(mappingDefaults[i].moduleName) == grtV.toLua(migrationMethods[j].moduleName) and
  80.         grtV.toLua(mappingDefaults[i].moduleName) == grtV.toLua(migrationMethods[j].moduleName) then
  81.           mappingDefaults[i].method= migrationMethods[j]
  82.       end
  83.     end
  84.   end
  85.   print("Do the migration.")
  86.   grtM.callFunction(
  87.     grtV.toLua(sourceConn.modules.MigrationModule), "migrate",
  88.     {"global::/migration", "global::/rdbmsMgmt/rdbms/" .. grtV.toLua(targetConn.driver.owner.name),
  89.       "global::/migration/targetVersion"
  90.     }
  91.   )
  92.   grt.exitOnError("The source objects cannot be migrated.")
  93. end
  94. -- ------------------------------------------------------------------
  95. -- checkpoint 4
  96. -- Generate and execute sql create statements
  97. do
  98.   print("Generate sql create statements.")
  99.   -- Set migration options.
  100.   %objectCreationParams%
  101.   grtM.callFunction(
  102.     grtV.toLua(targetConn.modules.TransformationModule), "generateSqlCreateStatements",
  103.     {"global::/migration/targetCatalog",
  104.       grtV.getGlobal("/migration/objectCreationParams")
  105.     }
  106.   )
  107.   grt.exitOnError("The SQL create statements cannot be created.")
  108.   -- write sql create script to file
  109.   if doWriteCreateScript then
  110.     print("Write create script.")
  111.     res= grtM.callFunction(
  112.       grtV.toLua(targetConn.modules.TransformationModule), "getSqlScript",
  113.       {"global::/migration/targetCatalog"}
  114.     )
  115.     grt.exitOnError("The SQL create script cannot be created.")
  116.     local f= io.open("creates.sql", "w+")
  117.     if f ~= nil then
  118.       f:write(grtV.toLua(res))
  119.       f:flush()
  120.       f:close()
  121.     end
  122.   end
  123.   -- create database objects online
  124.   print("Create database objects.")
  125.   grtM.callFunction(
  126.     grtV.toLua(targetConn.modules.TransformationModule), "executeSqlStatements",
  127.     {targetConn, "global::/migration/targetCatalog", "global::/migration/creationLog"}
  128.   )
  129.   grt.exitOnError("The SQL create script cannot be executed.")
  130. end
  131. -- ------------------------------------------------------------------
  132. -- checkpoint 5
  133. -- Bulk data transfer
  134. do
  135.   -- set transfer parameters
  136.   grtV.setGlobal("/migration/dataBulkTransferParams", {
  137.       CreateScript= doWriteInsertScript and "yes" or "no",
  138.       ScriptFileName="inserts.sql"
  139.     }
  140.   )
  141.   grtV.setContentType(grtV.getGlobal("/migration/dataBulkTransferParams"), "string")
  142.   print("Execute bulk data transfer")
  143.   grtM.callFunction(
  144.     grtV.toLua(sourceConn.modules.MigrationModule), "dataBulkTransfer",
  145.     {sourceConn, "global::/migration/sourceCatalog",
  146.       targetConn, "global::/migration/targetCatalog",
  147.       "global::/migration/dataBulkTransferParams",
  148.       "global::/migration/dataTransferLog"
  149.     }
  150.   )
  151.   grt.exitOnError("The bulk data transfer returned an error.")
  152.   print("Migration completed.")
  153. end
  154. -- ------------------------------------------------------------------
  155. -- checkpoint 6
  156. -- End of script