Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Last active August 29, 2015 14:18
Show Gist options
  • Save DominicFinn/1e5da3876e0073d9667e to your computer and use it in GitHub Desktop.
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 ;-)
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