Skip to content

Commit

Permalink
V1.1.1 (#28)
Browse files Browse the repository at this point in the history
* Refactor to improve database agnostic and re-include DbUp.

* Rename scripts resource to have explicit Script prefix.

* Parameterize the Journal Schema and Table.
Improve extendibility of capabilities.

* Improved internals, and readying support for mulitple dbs.

* Move SQL Server capabilities to separate project/assembly.
  • Loading branch information
chullybun committed Nov 17, 2022
1 parent f7be3f0 commit 390f5c1
Show file tree
Hide file tree
Showing 77 changed files with 1,704 additions and 1,374 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Represents the **NuGet** versions.

## v1.1.1
- *Enhancement:* **Breaking change:** Refactored the implementation/internals/naming to be largely database agnostic, have improved extensibility, to simplify introduction of additional databases (beyond current SQL Server implementation). As part of this exercise `DbUp` was re-introduced to provide the likes of SQL command parsing. The database connection management and implementation of `IDatabaseJournal` remains custom (using `DbUp` convention to enable). The `SqlServerMigrationConsole` implementation moved to `DbEx.Console.SqlServer` for separation and consistency.

## v1.0.18
- *Fixed:* Corrected issue where comments were removed from the SQL statement when executed against the database; i.e. they were missing from the likes of stored procedures, etc.
- *Fixed:* Schema create template updated to remove transaction wrap which was invalid.
Expand Down
38 changes: 38 additions & 0 deletions Common.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project>
<PropertyGroup>
<Version>1.1.1</Version>
<LangVersion>preview</LangVersion>
<Authors>Avanade</Authors>
<Company>Avanade</Company>
<Copyright>Avanade (c)</Copyright>
<PackageProjectUrl>https://github.com/Avanade/DbEx</PackageProjectUrl>
<RepositoryUrl>https://github.com/Avanade/DbEx</RepositoryUrl>
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>strong-name-key.snk</AssemblyOriginatorKeyFile>
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<PackageIconUrl>https://github.com/Avanade/DbEx/raw/main/images/Logo256x256.png</PackageIconUrl>
<PackageIcon>Logo256x256.png</PackageIcon>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<RunAnalyzersDuringBuild>true</RunAnalyzersDuringBuild>
<Nullable>enable</Nullable>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<IncludeSymbols>true</IncludeSymbols>
<EmbedAllSources>true</EmbedAllSources>
<IsPackable>true</IsPackable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\images\Logo256x256.png" Link="Logo256x256.png">
<PackagePath>\</PackagePath>
<Pack>true</Pack>
</None>
</ItemGroup>
</Project>
7 changes: 7 additions & 0 deletions DbEx.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
.gitignore = .gitignore
CHANGELOG.md = CHANGELOG.md
CODE_OF_CONDUCT.md = CODE_OF_CONDUCT.md
Common.targets = Common.targets
CONTRIBUTING.md = CONTRIBUTING.md
LICENSE = LICENSE
nuget-publish.ps1 = nuget-publish.ps1
Expand All @@ -36,6 +37,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{BB01353C-4
docs\SqlServerEventOutbox.md = docs\SqlServerEventOutbox.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbEx.SqlServer", "src\DbEx.SqlServer\DbEx.SqlServer.csproj", "{74DF6A5E-4F95-467A-8F64-5A4DBC3AAFFC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -66,6 +69,10 @@ Global
{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
{74DF6A5E-4F95-467A-8F64-5A4DBC3AAFFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{74DF6A5E-4F95-467A-8F64-5A4DBC3AAFFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74DF6A5E-4F95-467A-8F64-5A4DBC3AAFFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74DF6A5E-4F95-467A-8F64-5A4DBC3AAFFC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
36 changes: 23 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ _DbEx_ provides database extensions for [DbUp-inspired](#DbUp-inspired) database

## Status

[![CI](https://github.com/Avanade/DbEx/workflows/CI/badge.svg)](https://github.com/Avanade/DbEx/actions?query=workflow%3ACI) [![NuGet version](https://badge.fury.io/nu/DbEx.svg)](https://badge.fury.io/nu/DbEx)
The build status is [![CI](https://github.com/Avanade/DbEx/workflows/CI/badge.svg)](https://github.com/Avanade/DbEx/actions?query=workflow%3ACI) with the NuGet package status as follows, including links to the underlying source code and documentation:

Package | Status | Source & documentation
-|-|-
`DbEx` | [![NuGet version](https://badge.fury.io/nu/DbEx.svg)](https://badge.fury.io/nu/DbEx) | [Link](./src/DbEx)
`DbEx.SqlServer` | [![NuGet version](https://badge.fury.io/nu/DbEx.SqlServer.svg)](https://badge.fury.io/nu/DbEx.SqlServer) | [Link](./src/DbEx.SqlServer)


The included [change log](CHANGELOG.md) details all key changes per published version.

Expand Down Expand Up @@ -45,27 +51,31 @@ Currently, the easiest way of generating scripts from an existing database, is t

### Commands (functions)

The _DbEx_ [`DatabaseMigratorBase`](./src/DbEx/Migration/DatabaseMigratorBase.cs) provides the base database provider agnostic capability, with the [`SqlServerMigrator`](./src/DbEx/Migration/SqlServer/SqlServerMigrator.cs) providing the specific Microsoft SQL Server implementation, that automates the functionality as specified by the [`MigrationCommand`](./src/DbEx/Migration/MigrationCommand.cs). One or more commands can be specified, and they will be executed in the order listed.
The _DbEx_ [`DatabaseMigrationBase`](./src/DbEx/Migration/DatabaseMigrationBase.cs) provides the base database provider agnostic capability, with the [`SqlServerMigrator`](./src/DbEx.SqlServer/Migration/SqlServerMigration.cs) providing the specific Microsoft SQL Server implementation, that automates the functionality as specified by the [`MigrationCommand`](./src/DbEx/MigrationCommand.cs). One or more commands can be specified, and they will be executed in the order listed.

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 is consistent with the philosophy of [DbUp](https://dbup.readthedocs.io/en/latest/philosophy-behind-dbup/) to enable.
`CodeGen` | Provides opportunity to integrate a code-generation step where applicable (none by default).
[`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` schemas).
`Reset` | Resets the database by deleting all existing data (exclusions can be configured).
[`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.

Additional commands available are:

Command | Description
-|-
`All` | Performs _all_ the primary commands as follows; `Create`, `Migrate`, `Schema` and `Data`.
`All` | Performs _all_ the primary commands as follows; `Create`, `Migrate`, `CodeGen`, `Schema` and `Data`.
`Database` | Performs `Create`, `Migrate`, `CodeGen` and `Data`.
`Deploy` | Performs `Migrate` and `Schema`.
`DeployWithData` | Performs `Deploy` and `Data`.
`DropAndAll` | Performs `Drop` and `All`.
`DropAndDatabase` | Performs `Drop` and `Database`.
`ResetAndAll` | Performs `Reset` and `All` (designed primarily for testing).
`ResetAndData` | Performs `Reset` and `Data` (designed primarily for testing).
`ResetAndDatabase` | Performs `Reset` and `Database` (designed primarily for testing).
`Execute` | Executes the SQL statement(s) passed as additional arguments.
`Script` | Creates a new [`migration`](#Migrate) script file using the defined naming convention.

Expand Down Expand Up @@ -173,7 +183,7 @@ Demo:

### Console application

[`DbEx`](./src/DbEx/Console/MigratorConsoleBase.cs) has been optimized so that a new console application can reference and inherit the underlying capabilities.
[`DbEx`](./src/DbEx/Console/MigrationConsoleBase.cs) has been optimized so that a new console application can reference and inherit the underlying capabilities.

Where executing directly the default command-line options are as follows.

Expand Down Expand Up @@ -230,17 +240,17 @@ _Tip:_ To ensure all files are included as embedded resources add the following

To simplify the process for the developer _DbEx_ enables the creation of new migration script files into the `Migrations` folder. This will name the script file correctly and output the basic SQL statements to perform the selected function. The date and time stamp will use [DateTime.UtcNow](https://docs.microsoft.com/en-us/dotnet/api/system.datetime.utcnow) as this should avoid conflicts where being co-developed across time zones.

This requires the usage of the `Script` command, plus zero or more optional arguments where the first is the sub-command (these are will depend on the script being created). The optional arguments must appear in the order listed; where not specified it will default within the script file.
This requires the usage of the `Script` command, plus zero or more optional arguments where the first is the sub-command (these are will depend on the script being created). The optional arguments must appear in the order listed; where not specified it will default within the script file. Depending on the database provider not all of the following will be supported.

Sub-command | Argument(s) | Description
-|-|-
[N/A](./src/DbEx/Resources/Default_sql.hbs) | N/A | Creates a new empty skeleton script file.
[`Schema`](./src/DbEx/Resources/Schema_sql.hbs) | `Schema` and `Table` | Creates a new table create script file for the named schema and table.
[`Create`](./src/DbEx/Resources/Create_sql.hbs) | `Schema` and `Table` | Creates a new table create script file for the named schema and table.
[`RefData`](./src/DbEx/Resources/RefData_sql.hbs) | `Schema` and `Table` | Creates a new reference data table create script file for the named schema and table.
[`Alter`](./src/DbEx/Resources/Alter_sql.hbs) | `Schema` and `Table` | Creates a new table alter script file for the named schema and table.
[`CdcDb`](./src/DbEx/Resources/CdcDb_sql.hbs) | N/A | Creates a new `sys.sp_cdc_enable_db` script file for the database.
[`Cdc`](./src/DbEx/Resources/Cdc_sql.hbs) | `Schema` and `Table` | Creates a new `sys.sp_cdc_enable_table` script file for the named schema and table.
[N/A](./src/DbEx.SqlServer/Resources/ScriptDefault_sql.hbs) | N/A | Creates a new empty skeleton script file.
[`Schema`](./src/DbEx.SqlServer/Resources/ScriptSchema_sql.hbs) | `Schema` and `Table` | Creates a new table create script file for the named schema and table.
[`Create`](./src/DbEx.SqlServer/Resources/ScriptCreate_sql.hbs) | `Schema` and `Table` | Creates a new table create script file for the named schema and table.
[`RefData`](./src/DbEx.SqlServer/Resources/ScriptRefData_sql.hbs) | `Schema` and `Table` | Creates a new reference data table create script file for the named schema and table.
[`Alter`](./src/DbEx.SqlServer/Resources/ScriptAlter_sql.hbs) | `Schema` and `Table` | Creates a new table alter script file for the named schema and table.
[`CdcDb`](./src/DbEx.SqlServer/Resources/ScriptCdcDb_sql.hbs) | N/A | Creates a new `sys.sp_cdc_enable_db` script file for the database.
[`Cdc`](./src/DbEx.SqlServer/Resources/ScriptCdc_sql.hbs) | `Schema` and `Table` | Creates a new `sys.sp_cdc_enable_table` script file for the named schema and table.

Examples as follows.

Expand Down
3 changes: 2 additions & 1 deletion nuget-publish.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ param(
[switch]$DeleteFromlocalCache,
[string]$NugetCacheFolder = "$($env:USERPROFILE)\.nuget\packages",
[String[]]$ProjectsToPublish = @(
"src\DbEx")
"src\DbEx",
"src\DbEx.SqlServer")
)

$ShouldPublishRemote = (![string]::IsNullOrEmpty($apiKey) -and ![string]::IsNullOrEmpty($NugetServer))
Expand Down
44 changes: 44 additions & 0 deletions src/DbEx.SqlServer/Console/SqlServerMigrationConsole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/DbEx

using DbEx.Console;
using DbEx.Migration;
using DbEx.SqlServer.Migration;
using System;
using System.Reflection;

namespace DbEx.SqlServer.Console
{
/// <summary>
/// Console that facilitates the <see cref="SqlServerMigration"/> by managing the standard console command-line arguments/options.
/// </summary>
public sealed class SqlServerMigrationConsole : MigrationConsoleBase<SqlServerMigrationConsole>
{
/// <summary>
/// Creates a new <see cref="SqlServerMigrationConsole"/> using <typeparamref name="T"/> to default the probing <see cref="Assembly"/>.
/// </summary>
/// <typeparam name="T">The <see cref="Type"/>.</typeparam>
/// <param name="connectionString">The database connection string.</param>
/// <returns>A new <see cref="SqlServerMigrationConsole"/>.</returns>
public static SqlServerMigrationConsole Create<T>(string connectionString) => new(new MigrationArgs { ConnectionString = connectionString }.AddAssembly(typeof(T).Assembly));

/// <summary>
/// Initializes a new instance of the <see cref="SqlServerMigrationConsole"/> class.
/// </summary>
/// <param name="args">The default <see cref="MigrationArgs"/> that will be overridden/updated by the command-line argument values.</param>
public SqlServerMigrationConsole(MigrationArgs? args = null) : base(args ?? new MigrationArgs()) { }

/// <summary>
/// Initializes a new instance of the <see cref="SqlServerMigrationConsole"/> class that provides a default for the <paramref name="connectionString"/>.
/// </summary>
/// <param name="connectionString">The database connection string.</param>
public SqlServerMigrationConsole(string connectionString) : base(new MigrationArgs { ConnectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)) }) { }

/// <summary>
/// Gets the <see cref="MigrationArgs"/>.
/// </summary>
public new MigrationArgs Args => (MigrationArgs)base.Args;

/// <inheritdoc/>
protected override DatabaseMigrationBase CreateMigrator() => new SqlServerMigration(Args);
}
}
63 changes: 63 additions & 0 deletions src/DbEx.SqlServer/DbEx.SqlServer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RootNamespace>DbEx.SqlServer</RootNamespace>
<Product>DbEx</Product>
<Title>DbEx Database Migration Tool.</Title>
<Description>DbEX Database Migration tool for SQL Server.</Description>
<PackageTags>dbex database dbup db-up sqlserver sql</PackageTags>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="Resources\**\*" />
<EmbeddedResource Include="Migrations\**\*" />
<EmbeddedResource Include="Data\**\*" />
<EmbeddedResource Include="Templates\**\*" />
</ItemGroup>

<ItemGroup>
<None Remove="Resources\DatabaseCreate.sql" />
<None Remove="Resources\DatabaseData_sql.hbs" />
<None Remove="Resources\DatabaseDrop.sql" />
<None Remove="Resources\DatabaseReset_sql.hbs" />
<None Remove="Resources\DatabaseTemporal.sql" />
<None Remove="Resources\JournalAudit.sql" />
<None Remove="Resources\JournalCreate.sql" />
<None Remove="Resources\JournalExists.sql" />
<None Remove="Resources\JournalPrevious.sql" />
<None Remove="Resources\ScriptAlter_sql.hbs" />
<None Remove="Resources\ScriptCdcDb_sql.hbs" />
<None Remove="Resources\ScriptCdc_sql.hbs" />
<None Remove="Resources\ScriptCreate_sql.hbs" />
<None Remove="Resources\ScriptDefault_sql.hbs" />
<None Remove="Resources\ScriptRefData_sql.hbs" />
<None Remove="Resources\ScriptSchema_sql.hbs" />
<None Remove="Templates\EventOutboxDequeue_cs.hbs" />
<None Remove="Templates\EventOutboxEnqueue_cs.hbs" />
<None Remove="Templates\SchemaEventOutbox_sql.hbs" />
<None Remove="Templates\SpEventOutboxDequeue_sql.hbs" />
<None Remove="Templates\SpEventOutboxEnqueue_sql.hbs" />
<None Remove="Templates\TableEventOutboxData_sql.hbs" />
<None Remove="Templates\TableEventOutbox_sql.hbs" />
<None Remove="Templates\UdtEventOutbox_sql.hbs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="CoreEx.Database.SqlServer" Version="1.0.12" />
<PackageReference Include="dbup-sqlserver" Version="4.6.0" />
<PackageReference Include="OnRamp" Version="1.0.6" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DbEx\DbEx.csproj" />
</ItemGroup>

<Import Project="..\..\Common.targets" />

</Project>
Loading

0 comments on commit 390f5c1

Please sign in to comment.