Commit 1613efdf authored by dwentzel's avatar dwentzel

added DatabaseBuildProcess

parent 81b1f8fb
This diff is collapsed.
' VBScript source code
msgbox "starting script"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
'procs section
folder = "C:\Documents and Settings\dwentzel\My Documents\personal\HIT\Scripts\2Tables"
Set f = fso.GetFolder(folder)
Set fc = f.Files
'now fix each file
For Each f1 in fc
CurrentFile = f1.name
CurrentExtension = Right(CurrentFile,3)
If UCASE(CurrentExtension) = "SQL" Then
'msgbox CurrentFile
'find the object name
ObjName = mid(CurrentFile,1,instr(5,CurrentFile,".")-1)
'msgbox ObjName
FileToOpen = folder & "\" & CurrentFile
'determine number of lines in file
Set FileToFix = fso.OpenTextFile(FileToOpen, ForReading)
iLineCounter = 0
do while FileToFix.AtEndofStream <> True
LineContents = FileToFix.ReadLine
iLineCounter = iLineCounter + 1
loop
FileToFix.Close
Set FileToFix = Nothing
Linecontents = ""
'new file
NewFileName = replace(CurrentFile,".Table","")
FileToCreate = folder & "\" & NewFileName
Set NewFile = fso.CreateTextFile(FileToCreate,ForWriting)
'add some stuff to the top
NewFile.WriteLine "---------------------------------------------------------------------- "
NewFile.WriteLine "--Whenever adding a column to a table ensure it is added to the CREATE "
NewFile.WriteLine "--and ALTER sections (for net new vs ported databases) "
NewFile.WriteLine "---------------------------------------------------------------------- "
'Open the file again, read it, begin sending output to a new file, but dont output the last two lines
iNewFileLineCounter = 0
Set FileToFix = fso.OpenTextFile(FileToOpen, ForReading)
do while (FileToFix.AtEndofStream <> True) AND (iNewFileLineCounter <= iLineCounter -2)
iNewFileLineCounter = iNewFileLineCounter + 1
LineContents = ""
LineContents = FileToFix.ReadLine
NewFile.WriteLine (LineContents)
loop
FileToFix.Close
Set FileToFix = Nothing
'now add the new "alter text"
NewFile.WriteBlankLines(1)
NewFile.WriteLine "---------------------------------------------------------------------- "
NewFile.WriteLine "-- Alter Statements begin here "
NewFile.WriteLine "-- All SQL MUST be rerunnable "
NewFile.WriteLine "---------------------------------------------------------------------- "
NewFile.WriteLine "ELSE BEGIN "
NewFile.WriteBlankLines(1)
strLine = " Print 'Table " & ObjName & " already exists, performing Alters (if required)...'"
NewFile.WriteLine strLine
NewFile.WriteBlankLines(1)
NewFile.WriteLine "END --ALTERS "
NewFile.WriteLine "GO "
NewFile.WriteBlankLines(1)
NewFile.Close
Set NewFile = Nothing
end if
Next
set fc = nothing
set f = nothing
'now look for all files with .Table in their name...delete them.
Set f = fso.GetFolder(folder)
Set fc = f.Files
'now fix each file
For Each f1 in fc
CurrentFile = f1.name
If instr(1,CurrentFile,".Table.") <> 0 Then
FileToDelete = folder & "\" & CurrentFile
set MyFile = fso.GetFile(FileToDelete)
MyFile.Delete
Set MyFile = Nothing
End if
Next
set fc = nothing
set f = nothing
msgbox "done"
' VBScript source code
msgbox "starting script"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
folder = "C:\scripts\scripts\maxwell_db_install\8SystemData"
Set f = fso.GetFolder(folder)
Set fc = f.Files
'create a dummy file
Set NewFile = fso.CreateTextFile(folder & "\dummy.txt")
'now fix each file
For Each f1 in fc
CurrentFile = f1.name
CurrentExtension = Right(CurrentFile,3)
If UCASE(CurrentExtension) = UCASE("sql") Then
if NOT (UCASE(CurrentFile) = UCASE("dbo.tblData.sql") OR UCASE(CurrentFile) = UCASE("MXW.CodeXCodeGroup.sql") OR UCASE(CurrentFile) = UCASE("MXW.ReportDefinition.sql")) then
'found a file to process, open it
FileToOpen = folder & "\" & CurrentFile
Set FileToFix = fso.OpenTextFile(FileToOpen, ForReading)
'create a new file with .new as extension
FileNameToCreate = folder & "\" & replace(CurrentFile,".sql",".new")
Set FileToCreate = fso.CreateTextFile (FileNameToCreate)
'mark for first INSERT statement in file
IsFirstInsert = 1
GoCounter = 0
'for each line in FileToFix...
do while FileToFix.AtEndofStream <> True
'read in the line
LineContents = FileToFix.ReadLine
'if line starts INSERT INTO then add the existence check
if left(LineContents,12) = "INSERT INTO " Then
'determine schema and tablename
schma = mid(LineContents,14,3)
tblname = mid(LineContents,20)
tblname = left(tblname,instr(1,tblname,"]")-1)
'get key col
keycol = mid(LineContents,(instr(1,LineContents,"([")+2))
keycol = left(keycol,instr(1,keycol,"]")-1)
'get keycolvalue
strKeycolvalue = cstr(mid(LineContents,(instr(1,LineContents,"VALUES (")+8)))
keycolvalue = left(strKeycolvalue,instr(1,strKeycolvalue,",")-1)
'if not the first insert then add the closing END
if IsFirstInsert = 0 Then
FileToCreate.WriteLine "END"
FileToCreate.WriteLine "ELSE"
FileToCreate.WriteLine "BEGIN"
FileToCreate.WriteLine " --code UPDATE statement HERE when model data is being changed. Example:"
FileToCreate.WriteLine " --UPDATE " & schma & "." & tblname & " SET ... WHERE " & keycol & " = <id>"
FileToCreate.WriteLine " PRINT 'No UPDATE needed.'"
FileToCreate.WriteLine "END"
FileToCreate.WriteLine "-------------------------------------------------------------------------------------------"
end if
FileToCreate.WriteLine "--Table: " & tblname & " ID: " & keycolvalue
FileToCreate.WriteLine "IF NOT EXISTS (SELECT * FROM " & schma & "." & tblname & " WHERE " & keycol & " = " & keycolvalue & ")"
FileToCreate.WriteLine "BEGIN "
FileToCreate.Write " "
'set the marker
IsFirstInsert = 0
'when we hit the 2nd GO in the file then we need another END
elseif left(LineContents,2) = "GO" Then
'increment the counter
GoCounter = GoCounter + 1
If GoCounter = 2 Then
FileToCreate.WriteLine "END"
End if
End if
'now just add the line to the new file
FileToCreate.WriteLine (LineContents)
loop
FileToFix.close
Set FileToFix = Nothing
FileToCreate.close
Set FileToCreate = Nothing
'this is an exception file, no work to do
else
i = 1
end if
end if
Next
set fc = nothing
set f = nothing
'now look for all files with .sql in their name...delete them.
Set f = fso.GetFolder(folder)
Set fc = f.Files
'delete all .sql
For Each f1 in fc
CurrentFile = f1.name
CurrentExtension = Right(CurrentFile,3)
If CurrentExtension = "sql" Then
FileToDelete = folder & "\" & CurrentFile
set MyFile = fso.GetFile(FileToDelete)
MyFile.Delete
Set MyFile = Nothing
End if
Next
set fc = nothing
set f = nothing
'rename all .new to .sql
Set f = fso.GetFolder(folder)
Set fc = f.Files
For Each f1 in fc
CurrentFile = f1.name
CurrentExtension = Right(CurrentFile,3)
If CurrentExtension = "new" Then
FileToRen = folder & "\" & CurrentFile
set MyFile = fso.GetFile(FileToRen)
MyFile.Name = left (CurrentFile,len(CurrentFile)-3) & "sql"
Set MyFile = Nothing
End if
Next
set fc = nothing
set f = nothing
msgbox "delete dummy.txt"
msgbox "done"
' VBScript source code
msgbox "starting script"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
folder = "C:\scripts\scripts\maxwell_db_install\8SystemData"
Set f = fso.GetFolder(folder)
Set fc = f.Files
'create a dummy file
Set NewFile = fso.CreateTextFile(folder & "\dummy.txt")
'now fix each file
For Each f1 in fc
CurrentFile = f1.name
CurrentExtension = Right(CurrentFile,3)
If UCASE(CurrentFile) = UCASE("MXW.CodeXCodeGroup.sql") Then
'found a file to process, open it
FileToOpen = folder & "\" & CurrentFile
Set FileToFix = fso.OpenTextFile(FileToOpen, ForReading)
'create a new file with .new as extension
FileNameToCreate = folder & "\" & replace(CurrentFile,".sql",".new")
Set FileToCreate = fso.CreateTextFile (FileNameToCreate)
'mark for first INSERT statement in file
IsFirstInsert = 1
GoCounter = 0
'for each line in FileToFix...
do while FileToFix.AtEndofStream <> True
'read in the line
LineContents = FileToFix.ReadLine
'if line starts INSERT INTO then add the existence check
if left(LineContents,12) = "INSERT INTO " Then
'get keycolvalues
strKeycolvalue1 = cstr(mid(LineContents,(instr(1,LineContents,"VALUES (")+8)))
tmpinstr = instr(1,strKeycolvalue1,",") -1
'msgbox tmpinstr
keycolvalue1 = left(strKeycolvalue1,tmpinstr)
keycolvalue1 = replace(keycolvalue1," ","")
keycolvalue2 = mid(strKeycolvalue1,instr(1,strKeycolvalue1,",")+1)
' msgbox "1: " & keycolvalue2
keycolvalue2 = left(keycolvalue2,instr(1,keycolvalue2,",")-1)
'msgbox "2: " & keycolvalue2
keycolvalue2 = replace(keycolvalue2," ","")
'if not the first insert then add the closing END
if IsFirstInsert = 0 Then
FileToCreate.WriteLine "END"
FileToCreate.WriteLine "ELSE"
FileToCreate.WriteLine "BEGIN"
FileToCreate.WriteLine " --code UPDATE statement HERE when model data is being changed. Example:"
FileToCreate.WriteLine " --UPDATE MXW.CodeXCodeGroup SET ... WHERE CodeGroupID = <id> AND CodeDefinitionID = <id>"
FileToCreate.WriteLine " PRINT 'No UPDATE needed.'"
FileToCreate.WriteLine "END"
FileToCreate.WriteLine "-------------------------------------------------------------------------------------------"
end if
FileToCreate.WriteLine "--Table: " & tblname & " ID: " & keycolvalue
FileToCreate.WriteLine "IF NOT EXISTS (SELECT * FROM MXW.CodeXCodeGroup WHERE CodeGroupID = " & keycolvalue1 & " AND CodeDefinitionID = " & keycolvalue2 & ")"
FileToCreate.WriteLine "BEGIN "
FileToCreate.Write " "
'set the marker
IsFirstInsert = 0
'when we hit the 2nd GO in the file then we need another END
elseif left(LineContents,2) = "GO" Then
'increment the counter
GoCounter = GoCounter + 1
If GoCounter = 2 Then
FileToCreate.WriteLine "END"
End if
End if
'now just add the line to the new file
FileToCreate.WriteLine (LineContents)
loop
FileToFix.close
Set FileToFix = Nothing
FileToCreate.close
Set FileToCreate = Nothing
end if
Next
set fc = nothing
set f = nothing
msgbox "delete dummy.txt and MXW.CodeXCodeGroup.sql...then rename MXW.CodeXCodeGroup.new to MXW.CodeXCodeGroup.sql"
msgbox "done"
' VBScript source code
msgbox "starting script"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
folder = "C:\scripts\scripts\maxwell_db_install\8SystemData"
Set f = fso.GetFolder(folder)
Set fc = f.Files
'create a dummy file
Set NewFile = fso.CreateTextFile(folder & "\dummy.txt")
'now fix each file
For Each f1 in fc
CurrentFile = f1.name
CurrentExtension = Right(CurrentFile,3)
If UCASE(CurrentFile) = UCASE("Synchronization.sql") Then
FileToOpen = folder & "\" & CurrentFile
Set FileToFix = fso.OpenTextFile(FileToOpen, ForReading)
do while FileToFix.AtEndofStream <> True
LineContents = FileToFix.ReadLine
'look for lines starting Add, let's start a new file
if left(LineContents,7) = "-- Add " Then
'add the lines to the bottom of existing file
Call AddLinesToBottom
'close the existing file
NewFile.Close
Set NewFile = Nothing
'determine the filename to create
StartPos = instr(1,LineContents,"[")
'msgbox StartPos
FileToCreate = mid(LineContents,StartPos)
FileToCreate = replace(FileToCreate,"[","")
FileToCreate = replace(FileToCreate,"]","")
schma = left (FileToCreate,3)
Tbl = mid(FileToCreate,5)
'msgbox FileToCreate
'create the file
Set NewFile = fso.CreateTextFile(folder & "\" & FileToCreate & ".sql")
'NewFile.WriteLine LineContents
Call AddLinesToTop
Else
'not a new line, just add to existing file
NewFile.WriteLine (LineContents)
End if
loop
Call AddLinesToBottom
FileToFix.Close
Set FileToFix = Nothing
end if
Next
set fc = nothing
set f = nothing
Public Sub AddLinesToTop
'add some stuff to the top
NewFile.WriteLine "---------------------------------------------------------------------- "
NewFile.WriteLine "--Remember...all files must be 'rerunnable'. This means..."
NewFile.WriteLine "--1)Whenever adding an INSERT statement to check first for its existence by KEY "
NewFile.WriteLine "--2)When changing a row using an UPDATE, also check first for its existence by KEY"
NewFile.WriteLine "--3)When you have a question be sure to ask"
NewFile.WriteLine "---------------------------------------------------------------------- "
NewFile.WriteLine " "
NewFile.WriteLine "--temporarily disable foreignkeys"
NewFile.WriteLine "EXEC DBA_ManipulateForeignKeys @operation = 'DISABLE' , @tablename = '" & Tbl & "' , @schemaName = '" & schma & "'"
NewFile.WriteLine "GO"
End Sub
Public Sub AddLinesToBottom
NewFile.WriteLine "GO"
NewFile.WriteLine "--enable all foreign keys"
NewFile.WriteLine "EXEC DBA_ManipulateForeignKeys @operation = 'ENABLE' , @tablename = '" & Tbl & "' , @schemaName = '" & schma & "'"
NewFile.WriteLine "GO"
End Sub
msgbox "now delete dummy.txt and synchronization.sql"
msgbox "done"
' VBScript source code
msgbox "starting script"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
'functions section
folder = "C:\Documents and Settings\dwentzel\My Documents\personal\HIT\Scripts\5Functions"
typ = "'TF','FN','IF'"
typ1 = "FUNCTION"
'views section
'folder = "C:\Documents and Settings\dwentzel\My Documents\personal\HIT\Scripts\6Views"
'typ = "'V'"
'typ1 = "VIEW"
'procs section
'folder = "C:\Documents and Settings\dwentzel\My Documents\personal\HIT\Scripts\7Procedures"
'typ = "'P'"
'typ1 = "PROCEDURE"
Set f = fso.GetFolder(folder)
Set fc = f.Files
'now fix each file
For Each f1 in fc
CurrentFile = f1.name
CurrentExtension = Right(CurrentFile,3)
If UCASE(CurrentExtension) = "SQL" Then
'msgbox CurrentFile
'find the object name
ObjName = mid(CurrentFile,1,instr(5,CurrentFile,".")-1)
'msgbox ObjName
FileToOpen = folder & "\" & CurrentFile
'read the script contents and store it
Set FileToFix = fso.OpenTextFile(FileToOpen, ForReading)
ReadAllTextFile = FileToFix.ReadAll
'add the code to the top of each script, will overwrite anything existing
Set FileToFix = fso.OpenTextFile(FileToOpen,ForWriting)
strToWrite = "IF EXISTS (SELECT * FROM sysobjects WHERE type in (" & typ & ") and id = object_id('" & ObjName & "'))"
FileToFix.WriteLine strToWrite
strToWrite = " BEGIN"
FileToFix.WriteLine strToWrite
strToWrite = " DROP " & typ1 & " " & ObjName
FileToFix.WriteLine strToWrite
strToWrite = " END"
FileToFix.WriteLine strToWrite
strToWrite = "GO"
FileToFix.WriteLine strToWrite
FileToFix.WriteBlankLines(1)
'now append the original script contents.
FileToFix.Write ReadAllTextFile
FileToFix.Close
Set FileToFix = Nothing
end if
Next
msgbox "done"
' VBScript source code
msgbox "starting script"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
'trigger section
folder = "C:\scripts\scripts\maxwell_db_install\2Triggers"
typ = "'TR'"
typ1 = "TRIGGER"
'(SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[trigCostItems_Delete]'))
Set f = fso.GetFolder(folder)
Set fc = f.Files
'now fix each file
For Each f1 in fc
CurrentFile = f1.name
CurrentExtension = Right(CurrentFile,3)
If UCASE(CurrentExtension) = "SQL" Then
'msgbox CurrentFile
'find the object name
ObjName = mid(CurrentFile,1,instr(1,CurrentFile,".")-1)
'msgbox ObjName
FileToOpen = folder & "\" & CurrentFile
'read the script contents and store it
Set FileToFix = fso.OpenTextFile(FileToOpen, ForReading)
ReadAllTextFile = FileToFix.ReadAll
'add the code to the top of each script, will overwrite anything existing
Set FileToFix = fso.OpenTextFile(FileToOpen,ForWriting)
strToWrite = "IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'" & Objname & "'))"
FileToFix.WriteLine strToWrite
strToWrite = " BEGIN"
FileToFix.WriteLine strToWrite
strToWrite = " DROP " & typ1 & " " & ObjName
FileToFix.WriteLine strToWrite
strToWrite = " END"
FileToFix.WriteLine strToWrite
strToWrite = "GO"
FileToFix.WriteLine strToWrite
FileToFix.WriteBlankLines(1)
'now append the original script contents.
FileToFix.Write ReadAllTextFile
FileToFix.Close
Set FileToFix = Nothing
end if
Next
msgbox "done"
' VBScript source code
msgbox "starting script"
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject")
folder = "C:\Documents and Settings\dwentzel\My Documents\personal\HIT\Scripts\3ForeignKeys"
Set f = fso.GetFolder(folder)
Set fc = f.Files
'now fix each file
For Each f1 in fc
CurrentFile = f1.name
CurrentExtension = Right(CurrentFile,3)
If UCASE(CurrentFile) = "ALL_FKS.SQL" Then
'open the file for reading
FileToOpen = folder & "\" & CurrentFile
Set FileToFix = fso.OpenTextFile(FileToOpen, ForReading)
'get me a dummy file
FileToCreate = folder & "\dummy.sql"
Set NewFile = fso.CreateTextFile(FileToCreate,ForWriting)
NewFile.WriteLine "------------------------------------------------"
do while FileToFix.AtEndofStream <> True
LineContents = FileToFix.ReadLine
'Look for the pattern
If InStr(1,LineContents,"--NextObject:") <> 0 Then
'found the start of a new file...close first file and build new file
NewFileName = mid(LineContents,16)
NewFile.Close
Set NewFile = Nothing
FileToCreate = folder & "\" & NewFileName & ".sql"
Set NewFile = fso.CreateTextFile(FileToCreate,ForWriting)
NewFile.WriteLine "------------------------------------------------"
NewFile.WriteLine LineContents
'ELSEIF InStr(1,LineContents,"GO") <> 0 Then
' NewFile.WriteLine LineContents
' NewFile.WriteLine "--------------------------------------------------------"
' NewFile.WriteBlankLines(3)
' NewFile.Close
' Set NewFile = Nothing
ELSE 'should be any other line
on error resume next
NewFile.WriteLine LineContents