rtmpdash/Migrations.cs

117 lines
3.8 KiB
C#
Raw Normal View History

2022-02-04 04:48:04 +01:00
using System;
using System.Collections.Generic;
using System.Linq;
using LinqToDB;
using LinqToDB.Data;
using RTMPDash.DataModels;
using RTMPDash.DataModels.Tables;
using RTMPDash.Pages;
2022-02-04 04:48:04 +01:00
namespace RTMPDash;
public static class Migrations {
2022-02-09 18:32:16 +01:00
private const int DbVer = 2;
2022-02-04 04:48:04 +01:00
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"),
new Migration(2, "ALTER TABLE Users ADD PronounPlural INTEGER DEFAULT 0 NOT NULL"),
new Migration(2, "UPDATE Users SET PronounPlural = 1 WHERE PronounSubject = 'they'")
2022-02-04 04:48:04 +01:00
};
public static void RunMigrations() {
using var db = new AppDb.DbConn();
var ccolor = Console.ForegroundColor;
2022-02-04 04:48:04 +01:00
if (!db.DataProvider.GetSchemaProvider().GetSchema(db).Tables.Any()) {
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Write("Running migration: ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Initialize Database");
db.CreateTable<User>();
db.CreateTable<Invite>();
db.CreateTable<DbInfo>();
db.InsertWithIdentity(new DbInfo { DbVer = DbVer });
var password = Convert.ToBase64String(Guid.NewGuid().ToByteArray())[..12];
db.InsertWithIdentity(new User {
Username = "admin",
Password = password.Sha256(),
StreamKey = Guid.NewGuid().ToString(),
PronounSubject = "they",
PronounPossessive = "their",
PronounPlural = true,
2022-03-03 01:14:55 +01:00
AllowRestream = true,
IsAdmin = true
});
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("The user ");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.Write("admin ");
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("has been created with the password ");
Console.ForegroundColor = ConsoleColor.DarkMagenta;
Console.WriteLine(password);
}
else if (db.DataProvider.GetSchemaProvider().GetSchema(db).Tables.All(t => t.TableName != "DbInfo")) {
2022-02-04 04:48:04 +01:00
db.CreateTable<DbInfo>();
db.InsertWithIdentity(new DbInfo { DbVer = 0 });
}
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 {
2022-02-09 18:26:27 +01:00
new Migration(0, "BEGIN TRANSACTION").Run(db);
try {
migrationsToRun.ForEach(p => p.Run(db));
}
catch {
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine($"Migrating to database version {DbVer} failed.");
new Migration(0, "ROLLBACK TRANSACTION").Run(db);
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine("Rolled back migrations.");
Environment.Exit(1);
}
new Migration(0, "COMMIT TRANSACTION").Run(db);
2022-02-04 06:00:17 +01:00
var newdb = new AppDb.DbConn();
var dbinfo = newdb.DbInfo.First();
2022-02-04 04:48:04 +01:00
dbinfo.DbVer = DbVer;
2022-02-04 06:00:17 +01:00
newdb.Update(dbinfo);
2022-02-04 04:48:04 +01:00
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine($"Database version is now: {DbVer}");
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Finished running migrations.");
}
Console.ForegroundColor = ccolor;
}
private class Migration {
private readonly string _sql;
public readonly int IntroducedWithDbVer;
public Migration(int introducedWithDbVer, string sql) {
IntroducedWithDbVer = introducedWithDbVer;
_sql = sql;
}
2022-02-09 18:26:27 +01:00
public void Run(DataConnection db) {
2022-02-04 04:48:04 +01:00
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.Write("Running migration: ");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(_sql);
db.Execute(_sql);
}
}
2022-03-03 01:14:55 +01:00
}