Skip to content

Commit

Permalink
*Fixed:* The database code-generation support for replacement is now …
Browse files Browse the repository at this point in the history
…optional (see new `Replace` property) versus always (defaults to `true`).
  • Loading branch information
chullybun committed Oct 23, 2024
1 parent a0da263 commit 726721e
Show file tree
Hide file tree
Showing 27 changed files with 79 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Represents the **NuGet** versions.
## v5.17.1
- *Fixed:* The database console `Script` command execution has been updated to output to the correct directory path.
- *Fixed:* The `Agent` code-generation artefacts have been further simplified/improved.
- *Fixed:* The database code-generation support for replacement is now optional (see new `Replace` property) versus always (defaults to `true`).

## v5.17.0
- *Enhancement:* The `Common`-project related code-generated artefacts have all been updated to leverage `global using` and file scoped namespace declarations.
Expand Down
1 change: 1 addition & 0 deletions docs/Database-Query-Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Property | Description
**`name`** | The name of the primary table of the query. [Mandatory]
`schema` | The schema name of the primary table of the view.<br/>&dagger; Defaults to `CodeGeneration.Schema`.
`alias` | The `Schema.Table` alias name.<br/>&dagger; Will automatically default where not specified.
`viewReplace` | Indicates whether the existing `View` database object should be replaced/altered or whether the object is dropped and recreated.<br/>&dagger; Defaults to `CodeGeneration.Replace`.

<br/>

Expand Down
1 change: 1 addition & 0 deletions docs/Database-StoredProcedure-Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Property | Description
**`type`** | The stored procedure operation type. Valid options are: `Get`, `GetColl`, `Create`, `Update`, `Upsert`, `Delete`, `Merge`.<br/>&dagger; Defaults to `GetColl`.
**`paging`** | Indicates whether standardized paging support should be added.<br/>&dagger; This only applies where the stored procedure operation `Type` is `GetColl`.
`storedProcedureName` | The `StoredProcedure` name in the database.<br/>&dagger; Defaults to `sp` + `Table.Name` + `Name`; e.g. `spTableName` or `spPersonGet`.
`replace` | Indicates whether the existing `StoredProcedure` database object should be replaced/altered or whether the object is dropped and recreated.<br/>&dagger; Defaults to `CodeGeneration.Replace`.

<br/>

Expand Down
1 change: 1 addition & 0 deletions docs/Database-Table-Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Property | Description
**`name`** | The name of the `Table` in the database. [Mandatory]
**`schema`** | The name of the `Schema` where the `Table` is defined in the database.<br/>&dagger; Defaults to `CodeGeneration.Schema`.
`alias` | The `Schema.Table` alias name.<br/>&dagger; Will automatically default where not specified.
`replace` | Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.<br/>&dagger; Defaults to `CodeGeneration.Replace`.

<br/>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR ALTER PROCEDURE [Test].[spTableCreate]
CREATE PROCEDURE [Test].[spTableCreate]
@TableId AS UNIQUEIDENTIFIER = NULL OUTPUT,
@Name AS NVARCHAR(50) NULL = NULL,
@Count AS INT NULL = NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR ALTER PROCEDURE [Test].[spTableDelete]
CREATE PROCEDURE [Test].[spTableDelete]
@TableId AS UNIQUEIDENTIFIER,
@UpdatedBy AS NVARCHAR(250) NULL = NULL,
@UpdatedDate AS DATETIME2 NULL = NULL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR ALTER PROCEDURE [Test].[spTableGet]
CREATE PROCEDURE [Test].[spTableGet]
@TableId AS UNIQUEIDENTIFIER
AS
BEGIN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR ALTER PROCEDURE [Test].[spTableGetAll]
CREATE PROCEDURE [Test].[spTableGetAll]
AS
BEGIN
/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR ALTER PROCEDURE [Test].[spTableGetByArgs]
CREATE PROCEDURE [Test].[spTableGetByArgs]
@Name AS NVARCHAR(50) NULL = NULL,
@MinCount AS INT,
@MaxCount AS INT NULL = NULL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR ALTER PROCEDURE [Test].[spTableMerge]
CREATE PROCEDURE [Test].[spTableMerge]
@List AS [Test].[udtTableList] READONLY
AS
BEGIN
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR ALTER PROCEDURE [Test].[spTableUpdate]
CREATE PROCEDURE [Test].[spTableUpdate]
@TableId AS UNIQUEIDENTIFIER,
@Name AS NVARCHAR(50) NULL = NULL,
@Count AS INT NULL = NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR ALTER PROCEDURE [Test].[spTableUpsert]
CREATE PROCEDURE [Test].[spTableUpsert]
@TableId AS UNIQUEIDENTIFIER,
@Name AS NVARCHAR(50) NULL = NULL,
@Count AS INT NULL = NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE OR ALTER VIEW [Test].[vwTestQuery]
CREATE VIEW [Test].[vwTestQuery]
AS
/*
* This is automatically generated; any changes will be lost.
Expand Down
4 changes: 2 additions & 2 deletions samples/Demo/Beef.Demo.Database/database.beef-5.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ collectionType: UDT

queries:
# New "Query" configuration.
- { name: Table, schema: Test, view: true, viewName: vwTestQuery, excludeColumns: [ CreatedBy, UpdatedBy ], permission: TestSec,
- { name: Table, schema: Test, view: true, viewName: vwTestQuery, viewReplace: false, excludeColumns: [ CreatedBy, UpdatedBy ], permission: TestSec,
joins: [
{ name: Person, schema: Demo, excludeColumns: [ CreatedDate, UpdatedDate ], aliasColumns: [ RowVersion^RowVersionP ],
on: [
Expand Down Expand Up @@ -35,7 +35,7 @@ tables:
- { name: Table3, schema: Test, get: true, create: true, update: true, delete: true, upsert: true }

# Special test to generate all variations.
- { name: Table, schema: Test, create: true, update: true, upsert: true, delete: true, merge: true, udt: true, getAll: true, getAllOrderBy: [ Name Des ], excludeColumns: [ Other ], orgUnitImmutable: true, permission: TestSec,
- { name: Table, schema: Test, create: true, update: true, upsert: true, delete: true, merge: true, udt: true, getAll: true, replace: false, getAllOrderBy: [ Name Des ], excludeColumns: [ Other ], orgUnitImmutable: true, permission: TestSec,
storedProcedures: [
{ name: GetByArgs, type: GetColl, excludeColumns: [ Count ],
parameters: [
Expand Down
9 changes: 9 additions & 0 deletions tools/Beef.CodeGen.Core/Config/Database/CodeGenConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ public class CodeGenConfig : ConfigRootBase<CodeGenConfig>, ISpecialColumnNames
Description = "This is used as the default `Schema` for all child objects.")]
public string? Schema { get; set; }

/// <summary>
/// Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.
/// </summary>
[JsonPropertyName("replace")]
[CodeGenProperty("Key", Title = "Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.",
Description = "Defaults to `true`.")]
public bool? Replace { get; set; }

#endregion

#region Infer
Expand Down Expand Up @@ -397,6 +405,7 @@ protected override async Task PrepareAsync()
await LoadDbTablesConfigAsync().ConfigureAwait(false);

Schema = DefaultWhereNull(Schema, () => "dbo");
Replace = DefaultWhereNull(Replace, () => true);
EfModel = DefaultWhereNull(EfModel, () => false);

PathBase = DefaultWhereNull(PathBase, () => $"{Company}.{AppName}");
Expand Down
9 changes: 9 additions & 0 deletions tools/Beef.CodeGen.Core/Config/Database/QueryConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ public class QueryConfig : ConfigBase<CodeGenConfig, CodeGenConfig>, ITableRefer
Description = "Defaults to `Schema`.")]
public string? ViewSchema { get; set; }

/// <summary>
/// Indicates whether the existing `View` database object should be replaced/altered or whether the object is dropped and recreated.
/// </summary>
[JsonPropertyName("viewReplace")]
[CodeGenProperty("Key", Title = "Indicates whether the existing `View` database object should be replaced/altered or whether the object is dropped and recreated.",
Description = "Defaults to `CodeGeneration.Replace`.")]
public bool? ViewReplace { get; set; }

#endregion

#region Auth
Expand Down Expand Up @@ -345,6 +353,7 @@ protected override async Task PrepareAsync()
Alias = DefaultWhereNull(Alias, () => new string(StringConverter.ToSentenceCase(Name)!.Split(' ').Select(x => x[..1].ToLower(System.Globalization.CultureInfo.InvariantCulture).ToCharArray()[0]).ToArray()));
ViewName = DefaultWhereNull(ViewName, () => "vw" + Name);
ViewSchema = DefaultWhereNull(ViewSchema, () => Schema);
ViewReplace = DefaultWhereNull(ViewReplace, () => Parent!.Replace);

if (!string.IsNullOrEmpty(Permission) && Permission.Split(".", StringSplitOptions.RemoveEmptyEntries).Length == 1)
Permission += ".Read";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ public class StoredProcedureConfig : ConfigBase<CodeGenConfig, TableConfig>
Description = "Defaults to `sp` + `Table.Name` + `Name`; e.g. `spTableName` or `spPersonGet`.")]
public string? StoredProcedureName { get; set; }

/// <summary>
/// Indicates whether the existing `StoredProcedure` database object should be replaced/altered or whether the object is dropped and recreated.
/// </summary>
[JsonPropertyName("replace")]
[CodeGenProperty("Key", Title = "Indicates whether the existing `StoredProcedure` database object should be replaced/altered or whether the object is dropped and recreated.",
Description = "Defaults to `CodeGeneration.Replace`.")]
public bool? Replace { get; set; }

#endregion

#region Additional
Expand Down Expand Up @@ -301,6 +309,7 @@ protected override async Task PrepareAsync()
{
CollectionType = DefaultWhereNull(CollectionType, () => Parent!.CollectionType);
StoredProcedureName = DefaultWhereNull(StoredProcedureName, () => $"sp{Parent!.Name}{Name}");
Replace = DefaultWhereNull(Replace, () => Parent!.Replace);
Type = DefaultWhereNull(Type, () => "GetColl");
OrgUnitImmutable = DefaultWhereNull(OrgUnitImmutable, () => Parent!.OrgUnitImmutable);
Permission = DefaultWhereNull(Permission?.ToUpperInvariant(), () => Parent!.Permission == null ? null : Parent!.Permission!.ToUpperInvariant() + "." + Type switch
Expand Down
9 changes: 9 additions & 0 deletions tools/Beef.CodeGen.Core/Config/Database/TableConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,14 @@ public class TableConfig : ConfigBase<CodeGenConfig, CodeGenConfig>, ITableRefer
Description = "Will automatically default where not specified.")]
public string? Alias { get; set; }

/// <summary>
/// Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.
/// </summary>
[JsonPropertyName("replace")]
[CodeGenProperty("Key", Title = "Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.",
Description = "Defaults to `CodeGeneration.Replace`.")]
public bool? Replace { get; set; }

#endregion

#region Columns
Expand Down Expand Up @@ -504,6 +512,7 @@ protected override async Task PrepareAsync()
throw new CodeGenException(this, nameof(Name), $"Specified Schema.Table '{CodeGenConfig.FormatSchemaTableName(Schema, Name)}' not found in database.");

Alias = DefaultWhereNull(Alias, () => new string(StringConverter.ToSentenceCase(Name)!.Split(' ').Select(x => x[..1].ToLower(System.Globalization.CultureInfo.InvariantCulture).ToCharArray()[0]).ToArray()));
Replace = DefaultWhereNull(Replace, () => Parent!.Replace);
EfModel = DefaultWhereNull(EfModel, () => Parent!.EfModel);
EfModelName = DefaultWhereNull(EfModelName, () => Root.RenameForDotNet(Name));
OrgUnitImmutable = DefaultWhereNull(OrgUnitImmutable, () => Parent!.OrgUnitImmutable);
Expand Down
20 changes: 20 additions & 0 deletions tools/Beef.CodeGen.Core/Schema/database.beef-5.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
"title": "The name of the \u0060Schema\u0060 where the artefacts are defined in, or should be created in, the database.",
"description": "This is used as the default \u0060Schema\u0060 for all child objects."
},
"replace": {
"type": "boolean",
"title": "Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.",
"description": "Defaults to \u0060true\u0060."
},
"columnNameIsDeleted": {
"type": "string",
"title": "The column name for the \u0060IsDeleted\u0060 capability.",
Expand Down Expand Up @@ -223,6 +228,11 @@
"title": "The \u0060Schema.Table\u0060 alias name.",
"description": "Will automatically default where not specified."
},
"replace": {
"type": "boolean",
"title": "Indicates whether the existing database object should be replaced/altered or whether the object is dropped and recreated.",
"description": "Defaults to \u0060CodeGeneration.Replace\u0060."
},
"includeColumns": {
"type": "array",
"title": "The list of \u0060Column\u0060 names to be included in the underlying generated output.",
Expand Down Expand Up @@ -440,6 +450,11 @@
"title": "The \u0060StoredProcedure\u0060 name in the database.",
"description": "Defaults to \u0060sp\u0060 \u002B \u0060Table.Name\u0060 \u002B \u0060Name\u0060; e.g. \u0060spTableName\u0060 or \u0060spPersonGet\u0060."
},
"replace": {
"type": "boolean",
"title": "Indicates whether the existing \u0060StoredProcedure\u0060 database object should be replaced/altered or whether the object is dropped and recreated.",
"description": "Defaults to \u0060CodeGeneration.Replace\u0060."
},
"reselectStatement": {
"type": "string",
"title": "The SQL statement to perform the reselect after a \u0060Create\u0060, \u0060Update\u0060 or \u0060Upsert\u0060 stored procedure operation \u0060Type\u0060.",
Expand Down Expand Up @@ -777,6 +792,11 @@
"title": "The schema name for the \u0060View\u0060.",
"description": "Defaults to \u0060Schema\u0060."
},
"viewReplace": {
"type": "boolean",
"title": "Indicates whether the existing \u0060View\u0060 database object should be replaced/altered or whether the object is dropped and recreated.",
"description": "Defaults to \u0060CodeGeneration.Replace\u0060."
},
"permission": {
"type": "string",
"title": "The permission to be used for security permission checking.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}},
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}}{{#unless @last}},{{/unless}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}}{{#if Parent.Paging}},{{else}}{{#unless @last}},{{/unless}}{{/if}}{{#if Collection}}{{#ifeq CollectionType 'JSON'}} -- JSON Array{{/ifeq}}{{/if}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}}{{#unless @last}},{{/unless}}
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}},
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}},
{{/each}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
CREATE OR ALTER PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
CREATE {{#if Replace}}OR ALTER {{/if}}PROCEDURE [{{Parent.Schema}}].[{{StoredProcedureName}}]
{{#each ArgumentParameters}}
{{ParameterSql}},
{{/each}}
Expand Down
2 changes: 1 addition & 1 deletion tools/Beef.Database.SqlServer/Templates/DbVwQuery_sql.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{! Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/Beef }}
CREATE OR ALTER VIEW [{{ViewSchema}}].[{{ViewName}}]
CREATE {{#if ViewReplace}}OR ALTER {{/if}}VIEW [{{ViewSchema}}].[{{ViewName}}]
AS
/*
* This is automatically generated; any changes will be lost.
Expand Down

0 comments on commit 726721e

Please sign in to comment.