param (

            [string] $_instanceName,

            [string] $_databaseName

)

 

$currentPath = [System.IO.Directory]::GetCurrentDirectory();

[void][System.Reflection.Assembly]::LoadFile([System.IO.Path]::Combine($currentPath,"log4net.dll"));

$log4netLogManager = "PowerShell";

$log4netConfigName = "powershell.config";

$script:log=[log4net.LogManager]::GetLogger("PowerShell");

$private:fileInfo = new-object System.IO.FileInfo([System.IO.Path]::Combine($currentPath,$log4netConfigName));

[log4net.Config.XmlConfigurator]::Configure($fileInfo);

 

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMO");

 

$currentPath = [System.IO.Path]::Combine($currentPath,"Customer\Update");

 

function ExecSqlScript ( $_db, [string]$_scriptPath )

{

            $sr=new-object System.IO.StreamReader($_scriptPath);

            $script=$sr.ReadToEnd();

            $_db.ExecuteNonQuery($script);

            trap

            {

                        $log.Error("$error[0].exception");

            }

            $sr.Close();

}

 

function ExecSqlStatement ( $_db, [string]$_statement )

{

            $_db.ExecuteNonQuery($_statement);

            trap

            {

                        $log.Error("$error[0].exception");

            }

}

 

$log.Info("ApplySprintUpdates started.");

 

$private:server = new-object ( 'Microsoft.SqlServer.Management.Smo.Server') $_instanceName

$private:db = $server.Databases[$_databaseName]

 

$sprintUpdateXmlFullPath = [System.IO.Path]::Combine($currentPath,"SchemaUpdate.xml");

$updateXml = [xml] [string]::join("`n", (gc -read 10kb $sprintUpdateXmlFullPath ) );

$log.Info("  Script Path [$currentPath]");

$log.Info("  Database Name [$_databaseName]");

foreach ( $n in $updateXml.SelectNodes("/SchemaSprintUpdates/SprintUpdate") )

{

            $includeItem = $n.include;

            $updateType = $n.type;

            $sprint = $n.sprint;

            $sbi = $n.sbi;

            $filePath = $n.text;

            if ( [System.String]::Compare($includeItem.Trim(), "yes") -eq 0 )

            {

                        $fullPath = [System.IO.Path]::Combine($currentPath,$filePath);

                        $log.Info("  Applying [$filePath] for BacklogItem [$sbi] from Sprint [$sprint]");

                        if ( $updateType -eq "statement" )

                        {

                                    ExecSqlStatement $db $filePath

                        }

                        else

                        {

                                    ExecSqlScript $db $fullPath

                        }

            }         

}

 

$log.Info("ApplySprintUpdates finished.");