save last sync

This commit is contained in:
Nicolas Constant 2021-01-22 21:05:01 -05:00
parent 8146b6ea5a
commit b4c01ad326
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
8 changed files with 25 additions and 18 deletions

View file

@ -24,7 +24,7 @@ namespace BirdsiteLive.Services
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
await _databaseInitializer.DbInitAsync();
await _databaseInitializer.InitAndMigrateDbAsync();
await _statusPublicationPipeline.ExecuteAsync(stoppingToken);
}
}

View file

@ -75,19 +75,20 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
}
}
public async Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId)
public async Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId, DateTime lastSync)
{
if(id == default) throw new ArgumentException("id");
if(lastTweetPostedId == default) throw new ArgumentException("lastTweetPostedId");
if(lastTweetSynchronizedForAllFollowersId == default) throw new ArgumentException("lastTweetSynchronizedForAllFollowersId");
var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = @lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = @lastTweetSynchronizedForAllFollowersId WHERE id = @id";
if(lastSync == default) throw new ArgumentException("lastSync");
var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = @lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = @lastTweetSynchronizedForAllFollowersId, lastSync = @lastSync WHERE id = @id";
using (var dbConnection = Connection)
{
dbConnection.Open();
await dbConnection.QueryAsync(query, new { id, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId });
await dbConnection.QueryAsync(query, new { id, lastTweetPostedId, lastTweetSynchronizedForAllFollowersId, lastSync = lastSync.ToUniversalTime() });
}
}

View file

@ -1,4 +1,5 @@
using System.Threading.Tasks;
using System;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Models;
namespace BirdsiteLive.DAL.Contracts
@ -8,7 +9,7 @@ namespace BirdsiteLive.DAL.Contracts
Task CreateTwitterUserAsync(string acct, long lastTweetPostedId);
Task<SyncTwitterUser> GetTwitterUserAsync(string acct);
Task<SyncTwitterUser[]> GetAllTwitterUsersAsync();
Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId);
Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId, DateTime lastSync);
Task DeleteTwitterUserAsync(string acct);
Task<int> GetTwitterUsersCountAsync();
}

View file

@ -7,7 +7,7 @@ namespace BirdsiteLive.DAL
{
public interface IDatabaseInitializer
{
Task DbInitAsync();
Task InitAndMigrateDbAsync();
}
public class DatabaseInitializer : IDatabaseInitializer
@ -21,7 +21,7 @@ namespace BirdsiteLive.DAL
}
#endregion
public async Task DbInitAsync()
public async Task InitAndMigrateDbAsync()
{
var currentVersion = await _dbInitializerDal.GetCurrentDbVersionAsync();
var mandatoryVersion = _dbInitializerDal.GetMandatoryDbVersion();

View file

@ -14,7 +14,8 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
public async Task TestInit()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
await dal.InitDbAsync();
var init = new DatabaseInitializer(dal);
await init.InitAndMigrateDbAsync();
}
[TestCleanup]

View file

@ -16,7 +16,8 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
public async Task TestInit()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
await dal.InitDbAsync();
var init = new DatabaseInitializer(dal);
await init.InitAndMigrateDbAsync();
}
[TestCleanup]

View file

@ -14,7 +14,8 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
public async Task TestInit()
{
var dal = new DbInitializerPostgresDal(_settings, _tools);
await dal.InitDbAsync();
var init = new DatabaseInitializer(dal);
await init.InitAndMigrateDbAsync();
}
[TestCleanup]
@ -70,13 +71,15 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
var updatedLastTweetId = 1600L;
var updatedLastSyncId = 1550L;
await dal.UpdateTwitterUserAsync(result.Id, updatedLastTweetId, updatedLastSyncId);
var now = DateTime.Now;
await dal.UpdateTwitterUserAsync(result.Id, updatedLastTweetId, updatedLastSyncId, now);
result = await dal.GetTwitterUserAsync(acct);
Assert.AreEqual(acct, result.Acct);
Assert.AreEqual(updatedLastTweetId, result.LastTweetPostedId);
Assert.AreEqual(updatedLastSyncId, result.LastTweetSynchronizedForAllFollowersId);
Assert.IsTrue(Math.Abs((now.ToUniversalTime() - result.LastSync).Milliseconds) < 100);
}
[TestMethod]

View file

@ -31,7 +31,7 @@ namespace BirdsiteLive.DAL.Tests
#endregion
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
await dbInitializer.DbInitAsync();
await dbInitializer.InitAndMigrateDbAsync();
#region Validations
dbInitializerDal.VerifyAll();
@ -69,7 +69,7 @@ namespace BirdsiteLive.DAL.Tests
#endregion
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
await dbInitializer.DbInitAsync();
await dbInitializer.InitAndMigrateDbAsync();
#region Validations
dbInitializerDal.VerifyAll();
@ -122,7 +122,7 @@ namespace BirdsiteLive.DAL.Tests
#endregion
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
await dbInitializer.DbInitAsync();
await dbInitializer.InitAndMigrateDbAsync();
#region Validations
dbInitializerDal.VerifyAll();
@ -171,7 +171,7 @@ namespace BirdsiteLive.DAL.Tests
#endregion
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
await dbInitializer.DbInitAsync();
await dbInitializer.InitAndMigrateDbAsync();
#region Validations
dbInitializerDal.VerifyAll();
@ -227,7 +227,7 @@ namespace BirdsiteLive.DAL.Tests
var dbInitializer = new DatabaseInitializer(dbInitializerDal.Object);
try
{
await dbInitializer.DbInitAsync();
await dbInitializer.InitAndMigrateDbAsync();
}
finally
{