Skip to content

Commit

Permalink
Deprecate dbup dependency (#20)
Browse files Browse the repository at this point in the history
* Initial deprecation.

* Further tweaks.

* Update version and tests.

* Update CI

* Update documentation and dbup references.
  • Loading branch information
chullybun authored Sep 6, 2022
1 parent d44413f commit e2a80e9
Show file tree
Hide file tree
Showing 29 changed files with 385 additions and 252 deletions.
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
- name: Set EnvVar for Test
run: |
echo "DbEx_ConnectionStrings__NoneDb=Data Source=localhost, 1433;Initial Catalog=DbEx.None;User id=sa;Password=yourStrong(!)Password;TrustServerCertificate=true" >> $GITHUB_ENV
echo "DbEx_ConnectionStrings__ErrorDb=Data Source=localhost, 1433;Initial Catalog=DbEx.Error;User id=sa;Password=yourStrong(!)Password;TrustServerCertificate=true" >> $GITHUB_ENV
echo "DbEx_ConnectionStrings__EmptyDb=Data Source=localhost, 1433;Initial Catalog=DbEx.Empty;User id=sa;Password=yourStrong(!)Password;TrustServerCertificate=true" >> $GITHUB_ENV
echo "DbEx_ConnectionStrings__ConsoleDb=Data Source=localhost, 1433;Initial Catalog=DbEx.Console;User id=sa;Password=yourStrong(!)Password;TrustServerCertificate=true" >> $GITHUB_ENV
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

Represents the **NuGet** versions.

## v1.0.14
- *Enhancement:* Removed `DbUp` package dependencies and implemented equivalent (basics) that is compatible with `[dbo].[SchemaVersion]` journal management. Primary reason is related to the slow uptake of pull requests by the maintainers of `DbUp` that imposes limitations on `DbEx`.
- *Fixed:* `DbTypeMapper` updated to support `SMALLDATETIME` and `IMAGE` Microsoft Sql Server types.

## v1.0.13
- *Fixed:* `Int32.ToGuid` extension method changed to `DataValueConverter.IntToGuid` to be more explicit.

Expand Down
7 changes: 7 additions & 0 deletions DbEx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbEx.Test.Console", "tests\
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbEx.Test.OutboxConsole", "tests\DbEx.Test.OutboxConsole\DbEx.Test.OutboxConsole.csproj", "{959DD5E1-530A-42BA-82B8-F17A657AC351}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbEx.Test.Error", "tests\DbEx.Test.Error\DbEx.Test.Error.csproj", "{2069346C-9769-48DF-B71F-A58ED6A2192B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -55,6 +57,10 @@ Global
{959DD5E1-530A-42BA-82B8-F17A657AC351}.Debug|Any CPU.Build.0 = Debug|Any CPU
{959DD5E1-530A-42BA-82B8-F17A657AC351}.Release|Any CPU.ActiveCfg = Release|Any CPU
{959DD5E1-530A-42BA-82B8-F17A657AC351}.Release|Any CPU.Build.0 = Release|Any CPU
{2069346C-9769-48DF-B71F-A58ED6A2192B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2069346C-9769-48DF-B71F-A58ED6A2192B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2069346C-9769-48DF-B71F-A58ED6A2192B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2069346C-9769-48DF-B71F-A58ED6A2192B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -65,6 +71,7 @@ Global
{7B499943-7183-446F-92C0-D9DAD6237303} = {06385968-DFF7-4470-B87E-55D98CC4661C}
{117B6E86-2C88-446E-AC3A-AE1A2E84E2D8} = {06385968-DFF7-4470-B87E-55D98CC4661C}
{959DD5E1-530A-42BA-82B8-F17A657AC351} = {06385968-DFF7-4470-B87E-55D98CC4661C}
{2069346C-9769-48DF-B71F-A58ED6A2192B} = {06385968-DFF7-4470-B87E-55D98CC4661C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1A02148E-CFB1-43D0-8DC0-123232A179A7}
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
## Introduction

_DbEx_ provides database extensions for both:
- [DbUp-based](#DbUp-based) database migrations; and
- [DbUp-inspired](#DbUp-inspired) database migrations; and
- [ADO.NET](#ADO.NET) database access.

<br/>
Expand All @@ -20,12 +20,14 @@ The included [change log](CHANGELOG.md) details all key changes per published ve

<br/>

## DbUp-based
## DbUp-inspired

[DbUp](https://dbup.readthedocs.io/en/latest/) is a .NET library that is used to deploy changes to relational databases (supports multiple database technologies). It tracks which SQL scripts have been run already, and runs the change scripts in the order specified that are needed to get a database up to date. _DbEx_ provides additional functionality to improve the end-to-end experience of managing database migrations/updates leveraging DbUp.
[DbUp](https://dbup.readthedocs.io/en/latest/) is a .NET library that is used to deploy changes to relational databases (supports multiple database technologies). It tracks which SQL scripts have been run already, and runs the change scripts in the order specified that are needed to get a database up to date.

Traditionally, a [Data-tier Application (DAC)](https://docs.microsoft.com/en-us/sql/relational-databases/data-tier-applications/data-tier-applications) is used to provide a logical means to define all of the SQL Server objects - like tables, views, and instance objects, including logins - associated with a database. A DAC is a self-contained unit of SQL Server database deployment that enables data-tier developers and database administrators to package SQL Server objects into a portable artifact called a DAC package, also known as a DACPAC. This is largely specific to Microsoft SQL Server. Alternatively, there are other tools such as [redgate](https://www.red-gate.com/products/sql-development/sql-toolbelt-essentials/) that may be used. DbUp provides a more explicit approach, one that Microsoft also adopts with the likes of [EF Migrations](https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/).

_DbEx_ provides additional functionality to improve the end-to-end experience of managing database migrations/updates leveraging the concepts of DbUp. _DbEx_ prior to version `1.0.14` leveraged DbUb; however, due to the slow uptake of pull requests by the maintainers of DbUp that was starting to impose limitations on `DbEx` the decision was made to emulate functionality internally to achieve the functionality goals of `DbEx.` The changes are compatible with the underlying [journaling](./src/DbEx/Migration/SqlServer/SqlServerJournal.cs) that DbUp leverages (i.e. simulates the same).

<br/>

## Getting started
Expand All @@ -51,7 +53,7 @@ Command | Description
-|-
`Drop` | Drop the existing database (where it already exists).
`Create` | Create the database (where it does not already exist).
[`Migrate`](#Migrate) | Being the upgrading of a database overtime using order-based migration scripts; the tool leverages the philosophy and NuGet packages of [DbUp](https://dbup.readthedocs.io/en/latest/philosophy-behind-dbup/) to enable.
[`Migrate`](#Migrate) | Being the upgrading of a database overtime using order-based migration scripts; the tool is consistent with the philosophy of [DbUp](https://dbup.readthedocs.io/en/latest/philosophy-behind-dbup/) to enable.
[`Schema`](#Schema) | There are a number of database schema objects that can be managed outside of the above migrations, that are dropped and (re-)applied to the database using their native `Create` statement.
`Reset` | Resets the database by deleting all existing data (excludes `dbo` and `cdc` schema).
[`Data`](#Data) | There is data, for example *Reference Data* that needs to be applied to a database. This provides a simpler configuration than specifying the required SQL statements directly (which is also supported). This is _also_ useful for setting up Master and Transaction data for the likes of testing scenarios.
Expand All @@ -73,7 +75,7 @@ Command | Description

### Migrate

As stated, [DbUp](https://dbup.readthedocs.io/en/latest/) is used enabling a database to be dropped, created and migrated. The migration is managed by tracking order-based migration scripts. It tracks which SQL scripts have been run already, and runs the change scripts that are needed to get the database up to date.
As stated, the [DbUp](https://dbup.readthedocs.io/en/latest/) approach is used enabling a database to be dropped, created and migrated. The migration is managed by tracking order-based migration scripts. It tracks which SQL scripts have been run already, and runs the change scripts that are needed to get the database up to date.

Over time there will be more than one script updating a single object, for example a `Table`. In this case the first script operation will be a `Create`, followed by subsequent `Alter` operations. The scripts should be considered immutable, in that they cannot be changed once they have been applied; ongoing changes will need additional scripts.

Expand Down
2 changes: 1 addition & 1 deletion src/DbEx/Console/MigratorConsoleBaseT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace DbEx.Console
{
/// <summary>
/// Represents the base capabilities for the database migration orchestrator leveraging <see href="https://dbup.readthedocs.io/en/latest/">DbUp</see>.
/// Represents the base capabilities for the database migration orchestration.
/// </summary>
public abstract class MigratorConsoleBase<T> : MigratorConsoleBase where T : MigratorConsoleBase<T>
{
Expand Down
27 changes: 0 additions & 27 deletions src/DbEx/DatabaseExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/DbEx

using CoreEx.Database;
using CoreEx.Entities;
using CoreEx.Mapping;
using DbEx.Schema;
using OnRamp.Utility;
using System;
Expand Down Expand Up @@ -182,30 +180,5 @@ await database.SqlStatement(await sr6.ReadToEndAsync().ConfigureAwait(false)).Se

return tables;
}

private class DatabaseRecordMapper : IDatabaseMapper
{
public Type SourceType => throw new NotImplementedException();

public object? MapFromDb(DatabaseRecord record, OperationTypes operationType = OperationTypes.Unspecified)
{
throw new NotImplementedException();
}

public void MapPrimaryKeyParameters(DatabaseParameterCollection parameters, OperationTypes operationType, object? value)
{
throw new NotImplementedException();
}

public void MapPrimaryKeyParameters(DatabaseParameterCollection parameters, OperationTypes operationType, CompositeKey key)
{
throw new NotImplementedException();
}

public void MapToDb(object? value, DatabaseParameterCollection parameters, OperationTypes operationType = OperationTypes.Unspecified)
{
throw new NotImplementedException();
}
}
}
}
15 changes: 13 additions & 2 deletions src/DbEx/DbEx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
<WarningsAsErrors />
</PropertyGroup>

<ItemGroup>
<None Remove="Resources\SqlServer\DatabaseCreate.sql" />
<None Remove="Resources\SqlServer\DatabaseDrop.sql" />
<None Remove="Resources\SqlServer\JournalAuditScript.sql" />
<None Remove="Resources\SqlServer\JournalEnsureExists.sql" />
<None Remove="Resources\SqlServer\JournalGetExecuted.sql" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\Schema_sql.hbs" />
<EmbeddedResource Include="Resources\Alter_sql.hbs" />
Expand All @@ -52,7 +60,12 @@
<EmbeddedResource Include="Resources\SelectTableGeneratedColumns.sql" />
<EmbeddedResource Include="Resources\SelectTableIdentityColumns.sql" />
<EmbeddedResource Include="Resources\SelectTablePrimaryKey.sql" />
<EmbeddedResource Include="Resources\SqlServer\DatabaseDrop.sql" />
<EmbeddedResource Include="Resources\SqlServer\DatabaseCreate.sql" />
<EmbeddedResource Include="Resources\SqlServer\DeleteAllAndReset.sql" />
<EmbeddedResource Include="Resources\SqlServer\JournalAuditScript.sql" />
<EmbeddedResource Include="Resources\SqlServer\JournalEnsureExists.sql" />
<EmbeddedResource Include="Resources\SqlServer\JournalGetExecuted.sql" />
<EmbeddedResource Include="Resources\SqlServer\TableInsertOrMerge_sql.hb" />
<EmbeddedResource Include="Templates\SqlServer\EventOutboxDequeue_cs.hbs" />
<EmbeddedResource Include="Templates\SqlServer\EventOutboxEnqueue_cs.hbs" />
Expand All @@ -73,8 +86,6 @@

<ItemGroup>
<PackageReference Include="CoreEx.Database" Version="1.0.7" />
<PackageReference Include="dbup-sqlserver" Version="4.6.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="4.1.0" />
<PackageReference Include="OnRamp" Version="1.0.5" />
</ItemGroup>

Expand Down
42 changes: 26 additions & 16 deletions src/DbEx/Migration/DatabaseMigrationScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.IO;
using System.Reflection;
using System.Text;

namespace DbEx.Migration
{
Expand All @@ -11,54 +12,63 @@ namespace DbEx.Migration
/// </summary>
public class DatabaseMigrationScript
{
private readonly FileInfo? _file;
private readonly Assembly? _assembly;
private readonly string? _sql;

/// <summary>
/// Initializes a new <see cref="DatabaseMigrationScript"/> class for a file.
/// Initializes a new instance of the <see cref="DatabaseMigrationScript"/> class for a <paramref name="file"/>.
/// </summary>
/// <param name="file">The <see cref="FileInfo"/>.</param>
/// <param name="name">The resource name.</param>
/// <param name="name">The file name.</param>
public DatabaseMigrationScript(FileInfo file, string name)
{
IsResource = false;
File = file ?? throw new ArgumentNullException(nameof(file));
_file = file ?? throw new ArgumentNullException(nameof(file));
Name = name ?? throw new ArgumentNullException(nameof(name));
}

/// <summary>
/// Initializes a new <see cref="DatabaseMigrationScript"/> class for an embedded resource.
/// Initializes a new instance of the <see cref="DatabaseMigrationScript"/> class for an embedded resource.
/// </summary>
/// <param name="assembly">The <see cref="Assembly"/>.</param>
/// <param name="name">The resource name.</param>
public DatabaseMigrationScript(Assembly assembly, string name)
{
IsResource = true;
Assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));
_assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));
Name = name ?? throw new ArgumentNullException(nameof(name));
}

/// <summary>
/// Indicates whether the source of the SQL Script is an embedded <see cref="Assembly"/> resource; otherwise, it is a <see cref="File"/>.
/// Initializes a new instance of the <see cref="DatabaseMigrationScript"/> class for the specified <paramref name="sql"/>.
/// </summary>
public bool IsResource { get; }
/// <param name="sql"></param>
/// <param name="name">The sql name.</param>
/// <exception cref="ArgumentNullException"></exception>
public DatabaseMigrationScript(string sql, string name)
{
_sql = sql ?? throw new ArgumentNullException(nameof(sql));
Name = name ?? throw new ArgumentNullException(nameof(name));
}

/// <summary>
/// Gets the resource name.
/// Gets the name used for journaling.
/// </summary>
/// <remarks>The file name is also formatted as a resource name.</remarks>
public string Name { get; }

/// <summary>
/// Gets the <see cref="System.Reflection.Assembly"/> where <see cref="IsResource"/>.
/// Gets or sets the group order for the script.
/// </summary>
public Assembly? Assembly { get; }
public int GroupOrder { get; set; }

/// <summary>
/// Gets the <see cref="FileInfo"/> where not <see cref="IsResource"/>.
/// Indicates whether the script is run once or always.
/// </summary>
public FileInfo? File { get; }
/// <remarks><c>true</c> to run always; otherwise, <c>false</c> to run once (default).</remarks>
public bool RunAlways { get; set; }

/// <summary>
/// Gets the resource or file <see cref="System.IO.StreamReader"/>.
/// </summary>
public StreamReader GetStreamReader() => IsResource ? new StreamReader(Assembly!.GetManifestResourceStream(Name)!) : File!.OpenText();
public StreamReader GetStreamReader() =>_assembly is not null ? new StreamReader(_assembly!.GetManifestResourceStream(Name)!) : (_file is not null ? _file!.OpenText() : new StreamReader(new MemoryStream(Encoding.Default.GetBytes(_sql))));
}
}
Loading

0 comments on commit e2a80e9

Please sign in to comment.