|
- '=======================================================================================================================
- ' MIGRATION GENERATOR
- '=======================================================================================================================
- ' Generates a new migration file with timestamp-based versioning.
- '
- ' Usage:
- ' cscript //nologo scripts\generateMigration.vbs migration_name
- '
- ' Example:
- ' cscript //nologo scripts\generateMigration.vbs create_users_table
- ' Creates: db/migrations/20260109153045_create_users_table.asp
- '
-
- Option Explicit
-
- Dim fso, migrationName, timestamp, filename, filepath, content
-
- Set fso = CreateObject("Scripting.FileSystemObject")
-
- ' Check arguments
- If WScript.Arguments.Count < 1 Then
- WScript.Echo "Usage: cscript //nologo scripts\generateMigration.vbs migration_name"
- WScript.Echo ""
- WScript.Echo "Example:"
- WScript.Echo " cscript //nologo scripts\generateMigration.vbs create_users_table"
- WScript.Quit 1
- End If
-
- migrationName = WScript.Arguments(0)
-
- ' Validate migration name (alphanumeric and underscore only)
- If Not IsValidMigrationName(migrationName) Then
- WScript.Echo "Error: Migration name must contain only letters, numbers, and underscores"
- WScript.Quit 1
- End If
-
- ' Generate timestamp (YYYYMMDDHHMMSS)
- timestamp = GetTimestamp()
-
- ' Create filename
- filename = timestamp & "_" & migrationName & ".asp"
-
- ' Ensure migrations directory exists
- Dim migrationsDir
- migrationsDir = fso.GetParentFolderName(WScript.ScriptFullName) & "\..\db\migrations"
- migrationsDir = fso.GetAbsolutePathName(migrationsDir)
-
- If Not fso.FolderExists(migrationsDir) Then
- CreateDirectoryPath migrationsDir
- End If
-
- filepath = migrationsDir & "\" & filename
-
- ' Check if file already exists
- If fso.FileExists(filepath) Then
- WScript.Echo "Error: Migration file already exists: " & filename
- WScript.Quit 1
- End If
-
- ' Generate migration content
- content = GenerateMigrationContent(migrationName)
-
- ' Write the file
- Dim file
- Set file = fso.CreateTextFile(filepath, True)
- file.Write content
- file.Close
-
- WScript.Echo "Migration created: " & filename
- WScript.Echo "Path: " & filepath
- WScript.Echo ""
- WScript.Echo "Edit the file to add your migration logic, then run:"
- WScript.Echo " cscript //nologo scripts\runMigrations.vbs"
-
- WScript.Quit 0
-
- '=======================================================================================================================
- ' HELPER FUNCTIONS
- '=======================================================================================================================
-
- Function GetTimestamp()
- Dim dtNow, sYear, sMonth, sDay, sHour, sMinute, sSecond
- dtNow = Now()
-
- sYear = Year(dtNow)
- sMonth = Right("0" & Month(dtNow), 2)
- sDay = Right("0" & Day(dtNow), 2)
- sHour = Right("0" & Hour(dtNow), 2)
- sMinute = Right("0" & Minute(dtNow), 2)
- sSecond = Right("0" & Second(dtNow), 2)
-
- GetTimestamp = sYear & sMonth & sDay & sHour & sMinute & sSecond
- End Function
-
- Function IsValidMigrationName(name)
- Dim regex
- Set regex = New RegExp
- regex.Pattern = "^[a-zA-Z0-9_]+$"
- IsValidMigrationName = regex.Test(name)
- End Function
-
- Function GenerateMigrationContent(name)
- Dim template
-
- template = "<%"& vbCrLf
- template = template & "'======================================================================================================================="& vbCrLf
- template = template & "' MIGRATION: " & name & vbCrLf
- template = template & "'======================================================================================================================="& vbCrLf
- template = template & "' This migration was auto-generated. Add your migration logic below."& vbCrLf
- template = template & "'"& vbCrLf
- template = template & "' The migration object provides these helper methods:"& vbCrLf
- template = template & "' - migration.ExecuteSQL(sql) - Execute raw SQL"& vbCrLf
- template = template & "' - migration.CreateTable(name, columns) - Create a table"& vbCrLf
- template = template & "' - migration.DropTable(name) - Drop a table"& vbCrLf
- template = template & "' - migration.AddColumn(table, column, type) - Add a column"& vbCrLf
- template = template & "' - migration.DropColumn(table, column) - Drop a column"& vbCrLf
- template = template & "' - migration.CreateIndex(name, table, columns) - Create an index"& vbCrLf
- template = template & "' - migration.DropIndex(name, table) - Drop an index"& vbCrLf
- template = template & "'"& vbCrLf
- template = template & "' For complex operations, use migration.DB to access the database directly:"& vbCrLf
- template = template & "' migration.DB.Execute ""INSERT INTO users (name) VALUES (?)"", Array(""John"")"& vbCrLf
- template = template & "'"& vbCrLf
- template = template & ""& vbCrLf
- template = template & "'-----------------------------------------------------------------------------------------------------------------------"& vbCrLf
- template = template & "' UP - Apply the migration"& vbCrLf
- template = template & "'-----------------------------------------------------------------------------------------------------------------------"& vbCrLf
- template = template & "Sub Migration_Up(migration)"& vbCrLf
- template = template & " ' Example: Create a table"& vbCrLf
- template = template & " ' migration.CreateTable ""users"", ""id AUTOINCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(255), created_at DATETIME"""& vbCrLf
- template = template & " "& vbCrLf
- template = template & " ' Example: Add an index"& vbCrLf
- template = template & " ' migration.CreateIndex ""idx_users_email"", ""users"", ""email"""& vbCrLf
- template = template & " "& vbCrLf
- template = template & " ' TODO: Add your migration logic here"& vbCrLf
- template = template & " "& vbCrLf
- template = template & "End Sub"& vbCrLf
- template = template & ""& vbCrLf
- template = template & "'-----------------------------------------------------------------------------------------------------------------------"& vbCrLf
- template = template & "' DOWN - Rollback the migration"& vbCrLf
- template = template & "'-----------------------------------------------------------------------------------------------------------------------"& vbCrLf
- template = template & "Sub Migration_Down(migration)"& vbCrLf
- template = template & " ' Example: Drop the table"& vbCrLf
- template = template & " ' migration.DropTable ""users"""& vbCrLf
- template = template & " "& vbCrLf
- template = template & " ' TODO: Add your rollback logic here (reverse the Up migration)"& vbCrLf
- template = template & " "& vbCrLf
- template = template & "End Sub"& vbCrLf
- template = template & "%>"& vbCrLf
-
- GenerateMigrationContent = template
- End Function
-
- Sub CreateDirectoryPath(path)
- Dim parentPath
- If Not fso.FolderExists(path) Then
- parentPath = fso.GetParentFolderName(path)
- If parentPath <> "" Then
- CreateDirectoryPath parentPath
- End If
- fso.CreateFolder path
- End If
- End Sub
|