Skip to content

Commit 6526fdf

Browse files
Ja bist du narrischJa bist du narrisch
authored andcommitted
Updated AddColumn and ChangeColumn
1 parent 53cd93b commit 6526fdf

File tree

1 file changed

+21
-49
lines changed

1 file changed

+21
-49
lines changed

src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -515,67 +515,38 @@ private void RecreateTable(SQLiteTableInfo sqliteTableInfo)
515515

516516
public override void AddColumn(string table, Column column)
517517
{
518-
var backUp = column.ColumnProperty;
519-
column.ColumnProperty &= ~ColumnProperty.PrimaryKey;
520-
column.ColumnProperty &= ~ColumnProperty.Identity;
521-
base.AddColumn(table, column);
522-
column.ColumnProperty = backUp;
518+
if (!TableExists(table))
519+
{
520+
throw new Exception("Table does not exist.");
521+
}
523522

524-
if (backUp.HasFlag(ColumnProperty.PrimaryKey) || backUp.HasFlag(ColumnProperty.Identity))
523+
if (ColumnExists(table, column.Name))
525524
{
526-
ChangeColumn(table, column);
525+
throw new Exception("Column already exists.");
527526
}
527+
528+
var sqliteInfo = GetSQLiteTableInfo(table);
529+
530+
sqliteInfo.Columns.Add(column);
528531
}
529532

530533
public override void ChangeColumn(string table, Column column)
531534
{
532-
if (
533-
(column.ColumnProperty & ColumnProperty.PrimaryKey) != ColumnProperty.PrimaryKey &&
534-
(column.ColumnProperty & ColumnProperty.Unique) != ColumnProperty.Unique &&
535-
((column.ColumnProperty & ColumnProperty.NotNull) != ColumnProperty.NotNull || column.DefaultValue != null) &&
536-
(column.DefaultValue == null || column.DefaultValue.ToString() != "'CURRENT_TIME'" && column.DefaultValue.ToString() != "'CURRENT_DATE'" && column.DefaultValue.ToString() != "'CURRENT_TIMESTAMP'")
537-
)
535+
if (!TableExists(table))
538536
{
539-
var tempColumn = "temp_" + column.Name;
540-
RenameColumn(table, column.Name, tempColumn);
541-
AddColumn(table, column);
542-
543-
using (var cmd = CreateCommand())
544-
{
545-
ExecuteQuery(cmd, string.Format("UPDATE {0} SET {1}={2}", table, column.Name, tempColumn));
546-
}
547-
548-
RemoveColumn(table, tempColumn);
537+
throw new Exception("Table does not exist.");
549538
}
550-
else
551-
{
552-
var newColumns = GetColumns(table).ToArray();
553539

554-
for (var i = 0; i < newColumns.Count(); i++)
555-
{
556-
if (newColumns[i].Name == column.Name)
557-
{
558-
newColumns[i] = column;
559-
break;
560-
}
561-
}
540+
if (!ColumnExists(table, column.Name))
541+
{
542+
throw new Exception("Column does not exists.");
543+
}
562544

563-
AddTable(table + "_temp", null, newColumns);
545+
var sqliteInfo = GetSQLiteTableInfo(table);
564546

565-
var colNamesSql = string.Join(", ", newColumns.Select(x => x.Name).Select(x => QuoteColumnNameIfRequired(x)));
547+
sqliteInfo.Columns.Where(x => !x.Name.Equals(column.Name, StringComparison.InvariantCultureIgnoreCase));
566548

567-
using (var cmd = CreateCommand())
568-
{
569-
ExecuteQuery(cmd, string.Format("INSERT INTO {0}_temp SELECT {1} FROM {0}", table, colNamesSql));
570-
}
571-
572-
RemoveTable(table);
573-
574-
using (var cmd = CreateCommand())
575-
{
576-
ExecuteQuery(cmd, string.Format("ALTER TABLE {0}_temp RENAME TO {0}", table));
577-
}
578-
}
549+
sqliteInfo.Columns.Add(column);
579550
}
580551

581552
public override int TruncateTable(string table)
@@ -733,11 +704,12 @@ public override Column[] GetColumns(string tableName)
733704

734705
var indexListItems = GetPragmaIndexListItems(tableName);
735706
var uniqueConstraints = indexListItems.Where(x => x.Unique && x.Origin == "u");
707+
736708
foreach (var uniqueConstraint in uniqueConstraints)
737709
{
738710
var indexInfos = GetPragmaIndexInfo(uniqueConstraint.Name);
739711

740-
if (indexInfos.Count() == 1 && indexInfos.First().Name == column.Name)
712+
if (indexInfos.Count() == 1 && indexInfos.First().Name.Equals(column.Name, StringComparison.InvariantCultureIgnoreCase))
741713
{
742714
column.ColumnProperty |= ColumnProperty.Unique;
743715

0 commit comments

Comments
 (0)