Last active
August 29, 2015 14:18
-
-
Save DominicFinn/1e5da3876e0073d9667e to your computer and use it in GitHub Desktop.
Gets all your databases that don't have a number on the end such as 1 or 1.0 and versions them up for you ;-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open System.Data | |
open System.Data.SqlClient | |
let query sql f = | |
seq { | |
use cn = new SqlConnection("") | |
let da = new SqlDataAdapter(new SqlCommand(sql, cn)) | |
let ds = new DataSet() | |
cn.Open() | |
let i = da.Fill(ds) | |
let rowCol = ds.Tables.[0].Rows | |
let rowCount = rowCol.Count | |
for i in 0 .. (rowCount - 1) do | |
yield f (rowCol.[i]) | |
} | |
let command sql = | |
use cn = new SqlConnection("") | |
use command = new SqlCommand(sql, cn) | |
cn.Open() | |
command.EndExecuteNonQuery |> ignore | |
cn.Close() | |
// find those pesky unversioned databases. | |
let databases = query "select name from sys.databases where TRY_PARSE(right(name, 1) AS int) is null'" (fun row -> row.["name"].ToString()) | |
// sort them reet out. | |
databases |> Seq.iter (fun name -> command(System.String.Format("ALTER DATABASE {0} Modify Name = {1} ;", name, name + "_lol_1.0"))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment