Skip to content

Commit

Permalink
Merge pull request #2017 from sbwalker/dev
Browse files Browse the repository at this point in the history
moved AlterStringColumn to IDatabase interface so that it can be overridden in the Sqlite provider rather than requiring conditional logic in the migrations
  • Loading branch information
sbwalker authored Feb 22, 2022
2 parents c5f5bf0 + 9ba356c commit f2bec9b
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 68 deletions.
5 changes: 5 additions & 0 deletions Oqtane.Database.Sqlite/SqliteDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public override void DropColumn(MigrationBuilder builder, string name, string ta
// not implemented as SQLite does not support dropping columns
}

public override void AlterStringColumn(MigrationBuilder builder, string name, string table, int length, bool nullable, bool unicode)
{
// not implemented as SQLite does not support altering columns
}

public override string ConcatenateSql(params string[] values)
{
var returnValue = String.Empty;
Expand Down
5 changes: 5 additions & 0 deletions Oqtane.Server/Databases/DatabaseBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public virtual void DropColumn(MigrationBuilder builder, string name, string tab
builder.DropColumn(name, table);
}

public virtual void AlterStringColumn(MigrationBuilder builder, string name, string table, int length, bool nullable, bool unicode)
{
builder.AlterColumn<string>(RewriteName(name), RewriteName(table), maxLength: length, nullable: nullable, unicode: unicode);
}

public abstract DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString);
}
}
2 changes: 2 additions & 0 deletions Oqtane.Server/Databases/Interfaces/IDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface IDatabase

public void DropColumn(MigrationBuilder builder, string name, string table);

public void AlterStringColumn(MigrationBuilder builder, string name, string table, int length, bool nullable, bool unicode);

public DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected OperationBuilder<AddColumnOperation> AddStringColumn(ColumnsBuilder ta

public void AlterStringColumn(string name, int length, bool nullable = false, bool unicode = true)
{
_migrationBuilder.AlterColumn<string>(RewriteName(name), RewriteName(EntityTableName), maxLength: length, nullable: nullable, unicode: unicode);
ActiveDatabase.AlterStringColumn(_migrationBuilder, RewriteName(name), RewriteName(EntityTableName), length, nullable, unicode);
}

public void AddDecimalColumn(string name, int precision, int scale, bool nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,22 @@ public ChangeFolderNameAndPathColumnsSize(IDatabase database) : base(database)

protected override void Up(MigrationBuilder migrationBuilder)
{
if (ActiveDatabase.Name != "Sqlite")
{
var folderEntityBuilder = new FolderEntityBuilder(migrationBuilder, ActiveDatabase);

folderEntityBuilder.AlterStringColumn("Name", 256);

// Drop the index is needed because the Path is already associated with IX_Folder
folderEntityBuilder.DropForeignKey("FK_Folder_Site");
folderEntityBuilder.DropIndex("IX_Folder");
folderEntityBuilder.AlterStringColumn("Path", 512);
folderEntityBuilder.AddIndex("IX_Folder", new[] { "SiteId", "Path" }, true);
folderEntityBuilder.AddForeignKey("FK_Folder_Site");
}
var folderEntityBuilder = new FolderEntityBuilder(migrationBuilder, ActiveDatabase);
// Drop the index is needed because the Path is already associated with IX_Folder
folderEntityBuilder.DropIndex("IX_Folder");
folderEntityBuilder.AlterStringColumn("Name", 256);
folderEntityBuilder.AlterStringColumn("Path", 512);
folderEntityBuilder.AddIndex("IX_Folder", new[] { "SiteId", "Path" }, true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
if (ActiveDatabase.Name != "Sqlite")
{
var folderEntityBuilder = new FolderEntityBuilder(migrationBuilder, ActiveDatabase);

folderEntityBuilder.AlterStringColumn("Name", 50);

folderEntityBuilder.DropIndex("IX_Folder");
folderEntityBuilder.AlterStringColumn("Path", 50);
folderEntityBuilder.AddIndex("IX_Folder", new[] { "SiteId", "Path" }, true);
}
var folderEntityBuilder = new FolderEntityBuilder(migrationBuilder, ActiveDatabase);
// Drop the index is needed because the Path is already associated with IX_Folder
folderEntityBuilder.DropIndex("IX_Folder");
folderEntityBuilder.AlterStringColumn("Path", 50);
folderEntityBuilder.AlterStringColumn("Name", 50);
folderEntityBuilder.AddIndex("IX_Folder", new[] { "SiteId", "Path" }, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,20 @@ public ChangeFileNameColumnsSize(IDatabase database) : base(database)

protected override void Up(MigrationBuilder migrationBuilder)
{
if (ActiveDatabase.Name != "Sqlite")
{
var fileEntityBuilder = new FileEntityBuilder(migrationBuilder, ActiveDatabase);

// Drop the index is needed because the Name is already associated with IX_File
fileEntityBuilder.DropForeignKey("FK_File_Folder");
fileEntityBuilder.DropIndex("IX_File");
fileEntityBuilder.AlterStringColumn("Name", 256);
fileEntityBuilder.AddIndex("IX_File", new[] { "FolderId", "Name" }, true);
fileEntityBuilder.AddForeignKey("FK_File_Folder");
}
var fileEntityBuilder = new FileEntityBuilder(migrationBuilder, ActiveDatabase);
// Drop the index is needed because the Name is already associated with IX_File
fileEntityBuilder.DropIndex("IX_File");
fileEntityBuilder.AlterStringColumn("Name", 256);
fileEntityBuilder.AddIndex("IX_File", new[] { "FolderId", "Name" }, true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
if (ActiveDatabase.Name != "Sqlite")
{
var fileEntityBuilder = new FileEntityBuilder(migrationBuilder, ActiveDatabase);

fileEntityBuilder.DropIndex("IX_File");
fileEntityBuilder.AlterStringColumn("Name", 50);
fileEntityBuilder.AddIndex("IX_File", new[] { "FolderId", "Name" }, true);
}
var fileEntityBuilder = new FileEntityBuilder(migrationBuilder, ActiveDatabase);
// Drop the index is needed because the Name is already associated with IX_File
fileEntityBuilder.DropIndex("IX_File");
fileEntityBuilder.AlterStringColumn("Name", 50);
fileEntityBuilder.AddIndex("IX_File", new[] { "FolderId", "Name" }, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,28 @@ public ExpandVisitorAndUrlMappingUrls(IDatabase database) : base(database)

protected override void Up(MigrationBuilder migrationBuilder)
{
if (ActiveDatabase.Name != "Sqlite")
{
var visitorEntityBuilder = new VisitorEntityBuilder(migrationBuilder, ActiveDatabase);
visitorEntityBuilder.AlterStringColumn("Url", 2048);
var visitorEntityBuilder = new VisitorEntityBuilder(migrationBuilder, ActiveDatabase);
visitorEntityBuilder.AlterStringColumn("Url", 2048);

// Drop the index is needed because the Url is already associated with IX_UrlMapping
var urlMappingEntityBuilder = new UrlMappingEntityBuilder(migrationBuilder, ActiveDatabase);
urlMappingEntityBuilder.DropForeignKey("FK_UrlMapping_Site");
urlMappingEntityBuilder.DropIndex("IX_UrlMapping");
urlMappingEntityBuilder.AlterStringColumn("Url", 2048);
urlMappingEntityBuilder.AlterStringColumn("MappedUrl", 2048);
urlMappingEntityBuilder.AddIndex("IX_UrlMapping", new[] { "SiteId", "Url" }, true);
urlMappingEntityBuilder.AddForeignKey("FK_UrlMapping_Site");
}
var urlMappingEntityBuilder = new UrlMappingEntityBuilder(migrationBuilder, ActiveDatabase);
// Drop the index is needed because the Url is already associated with IX_UrlMapping
urlMappingEntityBuilder.DropIndex("IX_UrlMapping");
urlMappingEntityBuilder.AlterStringColumn("Url", 2048);
urlMappingEntityBuilder.AlterStringColumn("MappedUrl", 2048);
urlMappingEntityBuilder.AddIndex("IX_UrlMapping", new[] { "SiteId", "Url" }, true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
if (ActiveDatabase.Name != "Sqlite")
{
var visitorEntityBuilder = new VisitorEntityBuilder(migrationBuilder, ActiveDatabase);
visitorEntityBuilder.AlterStringColumn("Url", 500);
var visitorEntityBuilder = new VisitorEntityBuilder(migrationBuilder, ActiveDatabase);
visitorEntityBuilder.AlterStringColumn("Url", 500);

var urlMappingEntityBuilder = new UrlMappingEntityBuilder(migrationBuilder, ActiveDatabase);
urlMappingEntityBuilder.DropForeignKey("FK_UrlMapping_Site");
urlMappingEntityBuilder.DropIndex("IX_UrlMapping");
urlMappingEntityBuilder.AlterStringColumn("Url", 500);
urlMappingEntityBuilder.AlterStringColumn("MappedUrl", 500);
urlMappingEntityBuilder.AddIndex("IX_UrlMapping", new[] { "SiteId", "Url" }, true);
urlMappingEntityBuilder.AddForeignKey("FK_UrlMapping_Site");
}
var urlMappingEntityBuilder = new UrlMappingEntityBuilder(migrationBuilder, ActiveDatabase);
// Drop the index is needed because the Url is already associated with IX_UrlMapping
urlMappingEntityBuilder.DropIndex("IX_UrlMapping");
urlMappingEntityBuilder.AlterStringColumn("Url", 500);
urlMappingEntityBuilder.AlterStringColumn("MappedUrl", 500);
urlMappingEntityBuilder.AddIndex("IX_UrlMapping", new[] { "SiteId", "Url" }, true);
}
}
}
Binary file modified Oqtane.Server/wwwroot/Packages/Oqtane.Database.MySQL.nupkg.bak
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified Oqtane.Server/wwwroot/Packages/Oqtane.Database.Sqlite.nupkg.bak
Binary file not shown.

0 comments on commit f2bec9b

Please sign in to comment.