cleanup of old autoid serial stuff
This commit is contained in:
@@ -158,14 +158,14 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reset serial number
|
||||
/// ReStart serial number
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="newSerial">Next starting value for auto generated serial numbers</param>
|
||||
/// <param name="apiVersion">From route path</param>
|
||||
/// <returns>Widget</returns>
|
||||
[HttpPost("reset-serial/{newSerial}")]
|
||||
public async Task<IActionResult> ResetSerial([FromRoute] long newSerial, ApiVersion apiVersion)
|
||||
[HttpPost("restart-serial/{newSerial}")]
|
||||
public async Task<IActionResult> ReStartSerial([FromRoute] long newSerial, ApiVersion apiVersion)
|
||||
{
|
||||
if (!serverState.IsOpen)
|
||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||
@@ -174,13 +174,14 @@ namespace AyaNova.Api.Controllers
|
||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(new ApiErrorResponse(ModelState));
|
||||
bool result=await biz.ResetSerial(newSerial);
|
||||
bool result=await biz.RestartSerial(newSerial);
|
||||
if (!result)
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
else
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////
|
||||
//TEST ROUTES
|
||||
//
|
||||
|
||||
@@ -166,6 +166,30 @@ namespace AyaNova.Api.Controllers
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ReStart serial number
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="newSerial">Next starting value for auto generated serial numbers</param>
|
||||
/// <param name="apiVersion">From route path</param>
|
||||
/// <returns>Widget</returns>
|
||||
[HttpPost("restart-serial/{newSerial}")]
|
||||
public async Task<IActionResult> ReStartSerial([FromRoute] long newSerial, ApiVersion apiVersion)
|
||||
{
|
||||
if (!serverState.IsOpen)
|
||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||
WorkOrderBiz biz = WorkOrderBiz.GetBiz(ct, HttpContext);
|
||||
if (!Authorized.HasCreateRole(HttpContext.Items, biz.BizType))
|
||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(new ApiErrorResponse(ModelState));
|
||||
bool result=await biz.RestartSerial(newSerial);
|
||||
if (!result)
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
else
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
#endregion WorkOrderTopLevel routes
|
||||
|
||||
|
||||
|
||||
@@ -505,10 +505,6 @@ namespace AyaNova
|
||||
//TESTING
|
||||
|
||||
|
||||
|
||||
//AUTOID VALUES INITIALIZATION
|
||||
ServerBootConfig.SetMostRecentAutoIdValuesFromDatabase(dbContext);
|
||||
|
||||
//SPA FALLBACK ROUTE
|
||||
app.Use(async (context, next) =>
|
||||
{
|
||||
|
||||
@@ -81,7 +81,7 @@ namespace AyaNova.Biz
|
||||
// {
|
||||
//do stuff with PM
|
||||
PM o = new PM();
|
||||
o.Serial = serial ?? ServerBootConfig.PM_SERIAL.GetNext();
|
||||
// o.Serial = serial ?? ServerBootConfig.PM_SERIAL.GetNext();
|
||||
|
||||
//TODO: template
|
||||
//TODO: CUSTOMER ID
|
||||
|
||||
@@ -81,8 +81,7 @@ namespace AyaNova.Biz
|
||||
// {
|
||||
//do stuff with Quote
|
||||
Quote o = new Quote();
|
||||
o.Serial = serial ?? ServerBootConfig.QUOTE_SERIAL.GetNext();
|
||||
|
||||
|
||||
//TODO: template
|
||||
//TODO: CUSTOMER ID
|
||||
|
||||
|
||||
@@ -42,8 +42,7 @@ namespace AyaNova.Biz
|
||||
if (HasErrors)
|
||||
return null;
|
||||
else
|
||||
{
|
||||
// newObject.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext();
|
||||
{
|
||||
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||
await ct.Widget.AddAsync(newObject);
|
||||
@@ -77,7 +76,6 @@ namespace AyaNova.Biz
|
||||
NotUnique = await ct.Widget.AnyAsync(m => m.Name == newUniqueName);
|
||||
} while (NotUnique);
|
||||
newObject.Name = newUniqueName;
|
||||
//newObject.Serial = ServerBootConfig.WIDGET_SERIAL.GetNext();
|
||||
newObject.Id = 0;
|
||||
newObject.Concurrency = 0;
|
||||
await ct.Widget.AddAsync(newObject);
|
||||
@@ -171,18 +169,11 @@ namespace AyaNova.Biz
|
||||
}
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//RESET SERIAL
|
||||
//RESTART SERIAL
|
||||
//
|
||||
internal async Task<bool> ResetSerial(long newSerial)
|
||||
{
|
||||
/*
|
||||
ALTER TABLE table_name
|
||||
ALTER COLUMN column_name
|
||||
{ SET GENERATED { ALWAYS| BY DEFAULT } |
|
||||
SET sequence_option | RESTART [ [ WITH ] restart ] }
|
||||
*/
|
||||
internal async Task<bool> RestartSerial(long newSerial)
|
||||
{
|
||||
using (var command = ct.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
command.CommandText = $"alter table awidget alter column serial restart with {newSerial}";
|
||||
|
||||
@@ -61,7 +61,6 @@ namespace AyaNova.Biz
|
||||
return null;
|
||||
else
|
||||
{
|
||||
newObject.Serial = ServerBootConfig.WORKORDER_SERIAL.GetNext();
|
||||
newObject.Tags = TagUtil.NormalizeTags(newObject.Tags);
|
||||
newObject.CustomFields = JsonUtil.CompactJson(newObject.CustomFields);
|
||||
await ct.WorkOrder.AddAsync(newObject);
|
||||
@@ -85,8 +84,7 @@ namespace AyaNova.Biz
|
||||
return null;
|
||||
}
|
||||
WorkOrder newObject = new WorkOrder();
|
||||
CopyObject.Copy(dbObject, newObject, "Wiki");
|
||||
newObject.Serial = ServerBootConfig.WORKORDER_SERIAL.GetNext();
|
||||
CopyObject.Copy(dbObject, newObject, "Wiki,Serial");
|
||||
newObject.Id = 0;
|
||||
newObject.Concurrency = 0;
|
||||
await ct.WorkOrder.AddAsync(newObject);
|
||||
@@ -151,7 +149,7 @@ namespace AyaNova.Biz
|
||||
CopyObject.Copy(dbObject, SnapshotOfOriginalDBObj);
|
||||
|
||||
//Replace the db object with the PUT object
|
||||
CopyObject.Copy(dtPutObject, dbObject, "Id,Serial");
|
||||
CopyObject.Copy(dtPutObject, dbObject, "Id");
|
||||
|
||||
//if user has rights then change it, otherwise just ignore it and do the rest
|
||||
if (dtPutObject.Serial != 0 && SnapshotOfOriginalDBObj.Serial != dtPutObject.Serial && Authorized.HasAnyRole(CurrentUserRoles, RolesAllowedToChangeSerial))
|
||||
@@ -235,6 +233,26 @@ namespace AyaNova.Biz
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
//RESTART SERIAL
|
||||
//
|
||||
internal async Task<bool> RestartSerial(long newSerial)
|
||||
{
|
||||
|
||||
using (var command = ct.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
command.CommandText = $"alter table aworkorder alter column serial restart with {newSerial}";
|
||||
await ct.Database.OpenConnectionAsync();
|
||||
await command.ExecuteNonQueryAsync();
|
||||
await ct.Database.CloseConnectionAsync();
|
||||
}
|
||||
await EventLogProcessor.LogEventToDatabaseAsync(new Event(UserId, 0, BizType, AyaEvent.ResetSerial, newSerial.ToString()), ct);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private async Task WorkOrderSearchIndexAsync(WorkOrder obj, bool isNew)
|
||||
{
|
||||
//SEARCH INDEXING
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
namespace AyaNova.Util
|
||||
{
|
||||
public class AutoId
|
||||
{
|
||||
private readonly object valueLock = new object();
|
||||
private uint currentValue; //postgre bigint
|
||||
|
||||
public AutoId(uint initialValue)
|
||||
{
|
||||
currentValue = initialValue;
|
||||
}
|
||||
|
||||
public uint GetNext()
|
||||
{
|
||||
lock (valueLock)
|
||||
{
|
||||
currentValue += 1;
|
||||
if (currentValue == 0)
|
||||
currentValue += 1;
|
||||
return currentValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,10 +60,6 @@ namespace AyaNova.Util
|
||||
ApiServerState.ServerState wasServerState = apiServerState.GetState();
|
||||
string wasReason = apiServerState.Reason;
|
||||
|
||||
//START SERIAL NUMBER GENERATORS
|
||||
if (ServerBootConfig.WIDGET_SERIAL == null)
|
||||
ServerBootConfig.WIDGET_SERIAL = new AutoId(0);
|
||||
|
||||
try
|
||||
{
|
||||
await LogStatusAsync(JobId, LogJob, log, $"SEEDER: Seeding data level is {slevel.ToString()}, time zone offset is {timeZoneOffset.ToString()}");
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Linq;
|
||||
|
||||
|
||||
namespace AyaNova.Util
|
||||
{
|
||||
@@ -13,14 +12,6 @@ namespace AyaNova.Util
|
||||
internal static class ServerBootConfig
|
||||
{
|
||||
|
||||
//AUTOID's
|
||||
|
||||
//Get the most recent id number issued previously and start the auto-id at that level
|
||||
internal static AutoId WIDGET_SERIAL { get; set; }
|
||||
internal static AutoId WORKORDER_SERIAL { get; set; }
|
||||
internal static AutoId QUOTE_SERIAL { get; set; }
|
||||
internal static AutoId PM_SERIAL { get; set; }
|
||||
|
||||
//Diagnostic static values used during development, may not be related to config at all, this is just a convenient class to put them in
|
||||
#if (DEBUG)
|
||||
internal static List<string> TranslationKeysRequested { get; set; }
|
||||
@@ -219,50 +210,6 @@ namespace AyaNova.Util
|
||||
}
|
||||
|
||||
|
||||
//Get the auto-id most recent values at boot time
|
||||
internal static void SetMostRecentAutoIdValuesFromDatabase(AyaNova.Models.AyContext ct)
|
||||
{
|
||||
//TODO: CHANGE TO A DEDICATED SPOT IN GLOBAL RATHER THAN INFERRING AS IT IS NOT RIGHT PRACTICALLY for reasons
|
||||
//WIDGET SERIALS
|
||||
if (WIDGET_SERIAL == null)
|
||||
{
|
||||
//query for most recently used serial number
|
||||
//(note, can't query for highest serial as it can and likely will reset or be changed manually from time to time
|
||||
// so the algorithm is to keep the most recent sequence going on startup of the server)
|
||||
// var serializedObject = ct.Widget.AsNoTracking().OrderByDescending(x => x.Id).FirstOrDefault();
|
||||
// WIDGET_SERIAL = new AutoId(serializedObject == null ? 0 : serializedObject.Serial);
|
||||
|
||||
// var MostRecentWidget = ct.Widget.AsNoTracking().OrderByDescending(x => x.Id).FirstOrDefault();
|
||||
// uint MaxWidgetId = 0;
|
||||
// if (MostRecentWidget != null)
|
||||
// MaxWidgetId = MostRecentWidget.Serial;
|
||||
// WIDGET_SERIAL = new AutoId(MaxWidgetId);
|
||||
|
||||
}
|
||||
|
||||
//Workorder
|
||||
if (WORKORDER_SERIAL == null)
|
||||
{
|
||||
var serializedObject = ct.WorkOrder.AsNoTracking().OrderByDescending(x => x.Id).FirstOrDefault();
|
||||
WORKORDER_SERIAL = new AutoId(serializedObject == null ? 0 : serializedObject.Serial);
|
||||
}
|
||||
|
||||
//quote
|
||||
if (QUOTE_SERIAL == null)
|
||||
{
|
||||
var serializedObject = ct.Quote.AsNoTracking().OrderByDescending(x => x.Id).FirstOrDefault();
|
||||
QUOTE_SERIAL = new AutoId(serializedObject == null ? 0 : serializedObject.Serial);
|
||||
}
|
||||
|
||||
//PM
|
||||
if (PM_SERIAL == null)
|
||||
{
|
||||
var serializedObject = ct.PM.AsNoTracking().OrderByDescending(x => x.Id).FirstOrDefault();
|
||||
PM_SERIAL = new AutoId(serializedObject == null ? 0 : serializedObject.Serial);
|
||||
}
|
||||
|
||||
//OTHER SERIALS HERE IN FUTURE...
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user