Consolidated ASP Classic MVC framework from best components
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 line
7.2KB

  1. '=======================================================================================================================
  2. ' MIGRATION GENERATOR
  3. '=======================================================================================================================
  4. ' Generates a new migration file with timestamp-based versioning.
  5. '
  6. ' Usage:
  7. ' scripts\gen.cmd migration migration_name (runs under the AxonASP CLI in the container)
  8. '
  9. ' Example:
  10. ' scripts\gen.cmd migration create_users_table
  11. ' Creates: db/migrations/20260109153045_create_users_table.asp
  12. '
  13. Option Explicit
  14. ' AxonASP CLI: provide the WScript object (see _cli.vbs)
  15. Dim shimFso
  16. Set shimFso = CreateObject("Scripting.FileSystemObject")
  17. ExecuteGlobal shimFso.OpenTextFile(Server.MapPath("/scripts/_cli.vbs"), 1).ReadAll
  18. Dim fso, migrationName, timestamp, filename, filepath, content
  19. Set fso = CreateObject("Scripting.FileSystemObject")
  20. ' Check arguments
  21. If WScript.Arguments.Count < 1 Then
  22. WScript.Echo "Usage: scripts\gen.cmd migration migration_name"
  23. WScript.Echo ""
  24. WScript.Echo "Example:"
  25. WScript.Echo " scripts\gen.cmd migration create_users_table"
  26. WScript.Quit 1
  27. End If
  28. migrationName = WScript.Arguments(0)
  29. ' Validate migration name (alphanumeric and underscore only)
  30. If Not IsValidMigrationName(migrationName) Then
  31. WScript.Echo "Error: Migration name must contain only letters, numbers, and underscores"
  32. WScript.Quit 1
  33. End If
  34. ' Generate timestamp (YYYYMMDDHHMMSS)
  35. timestamp = GetTimestamp()
  36. ' Create filename
  37. filename = timestamp & "_" & migrationName & ".asp"
  38. ' Ensure migrations directory exists
  39. Dim migrationsDir
  40. migrationsDir = Server.MapPath("/db/migrations")
  41. If Not fso.FolderExists(migrationsDir) Then
  42. CreateDirectoryPath migrationsDir
  43. End If
  44. filepath = migrationsDir & "/" & filename
  45. ' Check if file already exists
  46. If fso.FileExists(filepath) Then
  47. WScript.Echo "Error: Migration file already exists: " & filename
  48. WScript.Quit 1
  49. End If
  50. ' Generate migration content
  51. content = GenerateMigrationContent(migrationName)
  52. ' Write the file
  53. Dim file
  54. Set file = fso.CreateTextFile(filepath, True)
  55. file.Write content
  56. file.Close
  57. WScript.Echo "Migration created: " & filename
  58. WScript.Echo "Path: " & filepath
  59. WScript.Echo ""
  60. WScript.Echo "Edit the file to add your migration logic, then run:"
  61. WScript.Echo " scripts\migrate.cmd up"
  62. WScript.Quit 0
  63. '=======================================================================================================================
  64. ' HELPER FUNCTIONS
  65. '=======================================================================================================================
  66. Function GetTimestamp()
  67. Dim dtNow, sYear, sMonth, sDay, sHour, sMinute, sSecond
  68. dtNow = Now()
  69. sYear = Year(dtNow)
  70. sMonth = Right("0" & Month(dtNow), 2)
  71. sDay = Right("0" & Day(dtNow), 2)
  72. sHour = Right("0" & Hour(dtNow), 2)
  73. sMinute = Right("0" & Minute(dtNow), 2)
  74. sSecond = Right("0" & Second(dtNow), 2)
  75. GetTimestamp = sYear & sMonth & sDay & sHour & sMinute & sSecond
  76. End Function
  77. Function IsValidMigrationName(name)
  78. Dim regex
  79. Set regex = New RegExp
  80. regex.Pattern = "^[a-zA-Z0-9_]+$"
  81. IsValidMigrationName = regex.Test(name)
  82. End Function
  83. Function GenerateMigrationContent(name)
  84. Dim template
  85. template = "<%"& vbCrLf
  86. template = template & "'======================================================================================================================="& vbCrLf
  87. template = template & "' MIGRATION: " & name & vbCrLf
  88. template = template & "'======================================================================================================================="& vbCrLf
  89. template = template & "' This migration was auto-generated. Add your migration logic below."& vbCrLf
  90. template = template & "'"& vbCrLf
  91. template = template & "' The migration object provides these helper methods:"& vbCrLf
  92. template = template & "' - migration.ExecuteSQL(sql) - Execute raw SQL"& vbCrLf
  93. template = template & "' - migration.CreateTable(name, columns) - Create a table"& vbCrLf
  94. template = template & "' - migration.DropTable(name) - Drop a table"& vbCrLf
  95. template = template & "' - migration.AddColumn(table, column, type) - Add a column"& vbCrLf
  96. template = template & "' - migration.DropColumn(table, column) - Drop a column"& vbCrLf
  97. template = template & "' - migration.CreateIndex(name, table, columns) - Create an index"& vbCrLf
  98. template = template & "' - migration.DropIndex(name, table) - Drop an index"& vbCrLf
  99. template = template & "'"& vbCrLf
  100. template = template & "' For complex operations, use migration.DB to access the database directly:"& vbCrLf
  101. template = template & "' migration.DB.Execute ""INSERT INTO users (name) VALUES (?)"", Array(""John"")"& vbCrLf
  102. template = template & "'"& vbCrLf
  103. template = template & ""& vbCrLf
  104. template = template & "'-----------------------------------------------------------------------------------------------------------------------"& vbCrLf
  105. template = template & "' UP - Apply the migration"& vbCrLf
  106. template = template & "'-----------------------------------------------------------------------------------------------------------------------"& vbCrLf
  107. template = template & "Sub Migration_Up(migration)"& vbCrLf
  108. template = template & " ' Example: Create a table"& vbCrLf
  109. template = template & " ' migration.CreateTable ""users"", ""id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT"""& vbCrLf
  110. template = template & " "& vbCrLf
  111. template = template & " ' Example: Add an index"& vbCrLf
  112. template = template & " ' migration.CreateIndex ""idx_users_email"", ""users"", ""email"""& vbCrLf
  113. template = template & " "& vbCrLf
  114. template = template & " ' TODO: Add your migration logic here"& vbCrLf
  115. template = template & " "& vbCrLf
  116. template = template & "End Sub"& vbCrLf
  117. template = template & ""& vbCrLf
  118. template = template & "'-----------------------------------------------------------------------------------------------------------------------"& vbCrLf
  119. template = template & "' DOWN - Rollback the migration"& vbCrLf
  120. template = template & "'-----------------------------------------------------------------------------------------------------------------------"& vbCrLf
  121. template = template & "Sub Migration_Down(migration)"& vbCrLf
  122. template = template & " ' Example: Drop the table"& vbCrLf
  123. template = template & " ' migration.DropTable ""users"""& vbCrLf
  124. template = template & " "& vbCrLf
  125. template = template & " ' TODO: Add your rollback logic here (reverse the Up migration)"& vbCrLf
  126. template = template & " "& vbCrLf
  127. template = template & "End Sub"& vbCrLf
  128. template = template & "%>"& vbCrLf
  129. GenerateMigrationContent = template
  130. End Function
  131. Sub CreateDirectoryPath(path)
  132. Dim parentPath
  133. If Not fso.FolderExists(path) Then
  134. parentPath = fso.GetParentFolderName(path)
  135. If parentPath <> "" Then
  136. CreateDirectoryPath parentPath
  137. End If
  138. fso.CreateFolder path
  139. End If
  140. End Sub

Powered by TurnKey Linux.