Starting to add ALTER COLUMN to SQLite.

This commit is contained in:
Keivan Beigi 2013-07-03 18:01:49 -07:00 committed by kay.one
commit 1c5a74df98
6 changed files with 151 additions and 4 deletions

View file

@ -0,0 +1,26 @@
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
/* [Tags("")]
[Migration(7)]
public class remove_backlog : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
var newSeriesTable = "CREATE TABLE [Series_new] ([Id] integer NOT NULL PRIMARY KEY AUTOINCREMENT, [TvdbId] integer NOT NULL, " +
"[TvRageId] integer NOT NULL, [ImdbId] text NOT NULL, [Title] text NOT NULL, [TitleSlug] text NOT NULL, " +
"[CleanTitle] text NOT NULL, [Status] integer NOT NULL, [Overview] text, [AirTime] text, " +
"[Images] text NOT NULL, [Path] text NOT NULL, [Monitored] integer NOT NULL, [QualityProfileId] integer NOT NULL, " +
"[SeasonFolder] integer NOT NULL, [LastInfoSync] datetime, [LastDiskSync] datetime, [Runtime] integer NOT NULL, " +
"[SeriesType] integer NOT NULL, [Network] text, [CustomStartDate] datetime, " +
"[UseSceneNumbering] integer NOT NULL, [FirstAired] datetime)";
Execute.Sql(newSeriesTable);
Execute.Sql("INSERT INTO Series_new SELECT * FROM Series");
}
}*/
}

View file

@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text.RegularExpressions;
using FluentMigrator.Builders.Execute;
namespace NzbDrone.Core.Datastore.Migration.Framework
{
public class SQLiteAlter
{
private readonly SQLiteConnection _connection;
private static readonly Regex SchemaRegex = new Regex(@"[\""\[](?<name>\w+)[\""\]]\s(?<schema>[\w-\s]+)",
RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
public SQLiteAlter(string connectionString)
{
_connection = new SQLiteConnection(connectionString);
_connection.Open();
}
private string GetOriginalSql(string tableName)
{
var command =
new SQLiteCommand(string.Format("SELECT sql FROM sqlite_master WHERE type='table' AND name ='{0}'",
tableName));
command.Connection = _connection;
return (string)command.ExecuteScalar();
}
public Dictionary<String, SQLiteColumn> GetColumns(string tableName)
{
var originalSql = GetOriginalSql(tableName);
var matches = SchemaRegex.Matches(originalSql);
return matches.Cast<Match>().ToDictionary(
match => match.Groups["name"].Value.Trim(),
match => new SQLiteColumn
{
Name = match.Groups["name"].Value.Trim(),
Schema = match.Groups["schema"].Value.Trim()
});
}
public void CreateTable(string tableName, Dictionary<string, SQLiteColumn>.ValueCollection values)
{
var columns = String.Join(",", values.Select(c => c.ToString()));
var command = new SQLiteCommand(string.Format("CREATE TABLE [{0}] ({1})", tableName, columns));
command.Connection = _connection;
command.ExecuteNonQuery();
}
}
public class SQLiteColumn
{
public string Name { get; set; }
public string Schema { get; set; }
public override string ToString()
{
return string.Format("[{0}] {1}", Name, Schema);
}
}
}