added LastSync to TwitterUser (wip)

This commit is contained in:
Nicolas Constant 2021-01-22 20:18:42 -05:00
parent 8708a529d6
commit 8146b6ea5a
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
3 changed files with 33 additions and 16 deletions

View file

@ -23,7 +23,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
public class DbInitializerPostgresDal : PostgresBase, IDbInitializerDal
{
private readonly PostgresTools _tools;
private readonly Version _currentVersion = new Version(1,0);
private readonly Version _currentVersion = new Version(2, 0);
private const string DbVersionType = "db-version";
#region Ctor
@ -32,7 +32,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
_tools = tools;
}
#endregion
public async Task<Version> GetCurrentDbVersionAsync()
{
var query = $"SELECT * FROM {_settings.DbVersionTableName} WHERE type = @type";
@ -65,17 +65,7 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
return _currentVersion;
}
public Tuple<Version, Version>[] GetMigrationPatterns()
{
return new Tuple<Version, Version>[0];
}
public Task MigrateDbAsync(Version from, Version to)
{
throw new NotImplementedException();
}
public async Task InitDbAsync()
public async Task<Version> InitDbAsync()
{
// Create version table
var createVersion = $@"CREATE TABLE {_settings.DbVersionTableName}
@ -124,14 +114,37 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
await _tools.ExecuteRequestAsync(createCachedTweets);
// Insert version to db
var firstVersion = new Version(1, 0);
using (var dbConnection = Connection)
{
dbConnection.Open();
await dbConnection.ExecuteAsync(
$"INSERT INTO {_settings.DbVersionTableName} (type,major,minor) VALUES(@type,@major,@minor)",
new { type = DbVersionType, major = _currentVersion.Major, minor = _currentVersion.Minor });
new { type = DbVersionType, major = firstVersion.Major, minor = firstVersion.Minor });
}
return firstVersion;
}
public Tuple<Version, Version>[] GetMigrationPatterns()
{
return new[]
{
new Tuple<Version, Version>(new Version(1,0), new Version(2,0))
};
}
public async Task<Version> MigrateDbAsync(Version from, Version to)
{
if (from == new Version(1, 0) && to == new Version(2, 0))
{
var addLastSync = $@"ALTER TABLE {_settings.TwitterUserTableName} ADD lastSync TIMESTAMP (2) WITHOUT TIME ZONE";
await _tools.ExecuteRequestAsync(addLastSync);
return new Version(2, 0);
}
throw new NotImplementedException();
}
public async Task DeleteAllAsync()

View file

@ -1,4 +1,6 @@
namespace BirdsiteLive.DAL.Models
using System;
namespace BirdsiteLive.DAL.Models
{
public class SyncTwitterUser
{
@ -7,5 +9,7 @@
public long LastTweetPostedId { get; set; }
public long LastTweetSynchronizedForAllFollowersId { get; set; }
public DateTime LastSync { get; set; }
}
}

View file

@ -35,11 +35,11 @@ namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
[TestMethod]
public async Task InitDbAsync()
{
var mandatory = new Version(1, 0);
var dal = new DbInitializerPostgresDal(_settings, _tools);
await dal.InitDbAsync();
var current = await dal.GetCurrentDbVersionAsync();
var mandatory = dal.GetMandatoryDbVersion();
Assert.IsNotNull(current);
Assert.AreEqual(mandatory.Minor, current.Minor);
Assert.AreEqual(mandatory.Major, current.Major);