Foxchat.NET/Foxchat.Identity/Migrations/20240512225835_Init.cs
2024-05-14 03:51:31 +02:00

181 lines
7.4 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace Foxchat.Identity.Migrations
{
/// <inheritdoc />
public partial class Init : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "accounts",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
username = table.Column<string>(type: "text", nullable: false),
email = table.Column<string>(type: "text", nullable: false),
password = table.Column<string>(type: "text", nullable: false),
role = table.Column<int>(type: "integer", nullable: false),
avatar = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("pk_accounts", x => x.id);
});
migrationBuilder.CreateTable(
name: "chat_instances",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
domain = table.Column<string>(type: "text", nullable: false),
base_url = table.Column<string>(type: "text", nullable: false),
public_key = table.Column<string>(type: "text", nullable: false),
status = table.Column<int>(type: "integer", nullable: false),
reason = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("pk_chat_instances", x => x.id);
});
migrationBuilder.CreateTable(
name: "instance",
columns: table => new
{
id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
public_key = table.Column<string>(type: "text", nullable: false),
private_key = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("pk_instance", x => x.id);
});
migrationBuilder.CreateTable(
name: "tokens",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
account_id = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("pk_tokens", x => x.id);
table.ForeignKey(
name: "fk_tokens_accounts_account_id",
column: x => x.account_id,
principalTable: "accounts",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "account_chat_instance",
columns: table => new
{
accounts_id = table.Column<Guid>(type: "uuid", nullable: false),
chat_instances_id = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("pk_account_chat_instance", x => new { x.accounts_id, x.chat_instances_id });
table.ForeignKey(
name: "fk_account_chat_instance_accounts_accounts_id",
column: x => x.accounts_id,
principalTable: "accounts",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_account_chat_instance_chat_instances_chat_instances_id",
column: x => x.chat_instances_id,
principalTable: "chat_instances",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "guild_accounts",
columns: table => new
{
chat_instance_id = table.Column<Guid>(type: "uuid", nullable: false),
guild_id = table.Column<string>(type: "text", nullable: false),
account_id = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("pk_guild_accounts", x => new { x.chat_instance_id, x.guild_id, x.account_id });
table.ForeignKey(
name: "fk_guild_accounts_accounts_account_id",
column: x => x.account_id,
principalTable: "accounts",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_guild_accounts_chat_instances_chat_instance_id",
column: x => x.chat_instance_id,
principalTable: "chat_instances",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "ix_account_chat_instance_chat_instances_id",
table: "account_chat_instance",
column: "chat_instances_id");
migrationBuilder.CreateIndex(
name: "ix_accounts_email",
table: "accounts",
column: "email",
unique: true);
// EF Core doesn't support creating indexes on arbitrary expressions, so we have to create it manually.
migrationBuilder.Sql("CREATE UNIQUE INDEX ix_accounts_username ON accounts (lower(username))");
migrationBuilder.CreateIndex(
name: "ix_chat_instances_domain",
table: "chat_instances",
column: "domain",
unique: true);
migrationBuilder.CreateIndex(
name: "ix_guild_accounts_account_id",
table: "guild_accounts",
column: "account_id");
migrationBuilder.CreateIndex(
name: "ix_tokens_account_id",
table: "tokens",
column: "account_id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "account_chat_instance");
migrationBuilder.DropTable(
name: "guild_accounts");
migrationBuilder.DropTable(
name: "instance");
migrationBuilder.DropTable(
name: "tokens");
migrationBuilder.DropTable(
name: "chat_instances");
migrationBuilder.DropTable(
name: "accounts");
}
}
}