parent
cade2df6a5
commit
59b3dc6f68
12 changed files with 331 additions and 182 deletions
@ -0,0 +1,9 @@ |
||||
using LinqToDB.Mapping; |
||||
|
||||
namespace RTMPDash.DataModels.Tables; |
||||
|
||||
[Table(Name = "DbInfo")] |
||||
public class DbInfo { |
||||
[Column(Name = "ID"), PrimaryKey, Identity, NotNull] public int Id { get; set; } |
||||
[Column(Name = "DbVer"), NotNull] public int DbVer { get; set; } |
||||
} |
@ -1,18 +1,20 @@ |
||||
using LinqToDB.Mapping; |
||||
|
||||
namespace RTMPDash.DataModels.Tables { |
||||
[Table(Name = "Users")] |
||||
public class User { |
||||
[Column(Name = "Username"), PrimaryKey] public string Username { get; set; } |
||||
[Column(Name = "Password")] public string Password { get; set; } |
||||
[Column(Name = "IsAdmin")] public bool IsAdmin { get; set; } |
||||
[Column(Name = "AllowRestream")] public bool AllowRestream { get; set; } |
||||
[Column(Name = "StreamKey")] public string StreamKey { get; set; } |
||||
[Column(Name = "PronounSubject")] public string PronounSubject { get; set; } |
||||
[Column(Name = "PronounPossessive")] public string PronounPossessive { get; set; } |
||||
[Column(Name = "ChatUrl")] public string ChatUrl { get; set; } |
||||
[Column(Name = "AnnouncementUrl")] public string AnnouncementUrl { get; set; } |
||||
[Column(Name = "RestreamTargets")] public string RestreamTargets { get; set; } |
||||
[Column(Name = "RestreamUrls")] public string RestreamUrls { get; set; } |
||||
} |
||||
namespace RTMPDash.DataModels.Tables; |
||||
|
||||
[Table(Name = "Users")] |
||||
public class User { |
||||
[Column(Name = "Username"), PrimaryKey] public string Username { get; set; } |
||||
[Column(Name = "Password")] public string Password { get; set; } |
||||
[Column(Name = "IsAdmin")] public bool IsAdmin { get; set; } |
||||
[Column(Name = "AllowRestream")] public bool AllowRestream { get; set; } |
||||
[Column(Name = "StreamKey")] public string StreamKey { get; set; } |
||||
[Column(Name = "PronounSubject")] public string PronounSubject { get; set; } |
||||
[Column(Name = "PronounPossessive")] public string PronounPossessive { get; set; } |
||||
[Column(Name = "ChatUrl")] public string ChatUrl { get; set; } |
||||
[Column(Name = "AnnouncementUrl")] public string AnnouncementUrl { get; set; } |
||||
[Column(Name = "RestreamTargets")] public string RestreamTargets { get; set; } |
||||
[Column(Name = "RestreamUrls")] public string RestreamUrls { get; set; } |
||||
[Column(Name = "IsPrivate")] public bool IsPrivate { get; set; } |
||||
[Column(Name = "PrivateAccessKey")] public string PrivateAccessKey { get; set; } |
||||
} |
@ -0,0 +1,75 @@ |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using LinqToDB; |
||||
using LinqToDB.Data; |
||||
using RTMPDash.DataModels; |
||||
using RTMPDash.DataModels.Tables; |
||||
|
||||
namespace RTMPDash; |
||||
|
||||
/** |
||||
* TODO: Initial migrations should create all tables & an admin user and print a randomly generated password for the admin user |
||||
* * |
||||
*/ |
||||
public static class Migrations { |
||||
public const int DbVer = 1; |
||||
|
||||
private static readonly List<Migration> _migrations = new() { |
||||
new Migration(1, "ALTER TABLE Users ADD IsPrivate INTEGER DEFAULT 0 NOT NULL"), new Migration(1, "ALTER TABLE Users ADD PrivateAccessKey TEXT") |
||||
}; |
||||
|
||||
public static void RunMigrations() { |
||||
using var db = new AppDb.DbConn(); |
||||
|
||||
if (db.DataProvider.GetSchemaProvider().GetSchema(db).Tables.All(t => t.TableName != "DbInfo")) { |
||||
db.CreateTable<DbInfo>(); |
||||
db.InsertWithIdentity(new DbInfo { DbVer = 0 }); |
||||
} |
||||
|
||||
var dbinfo = db.DbInfo.First(); |
||||
var ccolor = Console.ForegroundColor; |
||||
|
||||
Console.ForegroundColor = ConsoleColor.Yellow; |
||||
Console.WriteLine($"Database version: {db.DbInfo.ToList().First().DbVer}"); |
||||
|
||||
var migrationsToRun = _migrations.FindAll(p => p.IntroducedWithDbVer > db.DbInfo.First().DbVer); |
||||
if (migrationsToRun.Count == 0) { |
||||
Console.ForegroundColor = ConsoleColor.Green; |
||||
Console.WriteLine("No migrations to run."); |
||||
} |
||||
else { |
||||
migrationsToRun.ForEach(RunMigration); |
||||
dbinfo.DbVer = DbVer; |
||||
db.Update(dbinfo); |
||||
Console.ForegroundColor = ConsoleColor.DarkGreen; |
||||
Console.WriteLine($"Database version is now: {DbVer}"); |
||||
Console.ForegroundColor = ConsoleColor.Green; |
||||
Console.WriteLine("Finished running migrations."); |
||||
} |
||||
|
||||
Console.ForegroundColor = ccolor; |
||||
} |
||||
|
||||
private static void RunMigration(Migration mig) => mig.Run(); |
||||
|
||||
private class Migration { |
||||
private readonly string _sql; |
||||
public readonly int IntroducedWithDbVer; |
||||
|
||||
public Migration(int introducedWithDbVer, string sql) { |
||||
IntroducedWithDbVer = introducedWithDbVer; |
||||
_sql = sql; |
||||
} |
||||
|
||||
public void Run() { |
||||
using var db = new AppDb.DbConn(); |
||||
|
||||
Console.ForegroundColor = ConsoleColor.DarkCyan; |
||||
Console.Write("Running migration: "); |
||||
Console.ForegroundColor = ConsoleColor.Yellow; |
||||
Console.WriteLine(_sql); |
||||
db.Execute(_sql); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue