Foxchat.NET/Foxchat.Chat/Migrations/20240521132416_Init.cs

228 lines
9.2 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
using NodaTime;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace Foxchat.Chat.Migrations
{
/// <inheritdoc />
public partial class Init : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "identity_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_identity_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: "users",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
instance_id = table.Column<Guid>(type: "uuid", nullable: false),
remote_user_id = table.Column<string>(type: "text", nullable: false),
username = table.Column<string>(type: "text", nullable: false),
avatar = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("pk_users", x => x.id);
table.ForeignKey(
name: "fk_users_identity_instances_instance_id",
column: x => x.instance_id,
principalTable: "identity_instances",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "guilds",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
name = table.Column<string>(type: "text", nullable: false),
owner_id = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("pk_guilds", x => x.id);
table.ForeignKey(
name: "fk_guilds_users_owner_id",
column: x => x.owner_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "channels",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
guild_id = table.Column<Guid>(type: "uuid", nullable: false),
name = table.Column<string>(type: "text", nullable: false),
topic = table.Column<string>(type: "text", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("pk_channels", x => x.id);
table.ForeignKey(
name: "fk_channels_guilds_guild_id",
column: x => x.guild_id,
principalTable: "guilds",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "guild_user",
columns: table => new
{
guilds_id = table.Column<Guid>(type: "uuid", nullable: false),
users_id = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("pk_guild_user", x => new { x.guilds_id, x.users_id });
table.ForeignKey(
name: "fk_guild_user_guilds_guilds_id",
column: x => x.guilds_id,
principalTable: "guilds",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_guild_user_users_users_id",
column: x => x.users_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "messages",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
channel_id = table.Column<Guid>(type: "uuid", nullable: false),
author_id = table.Column<Guid>(type: "uuid", nullable: false),
content = table.Column<string>(type: "text", nullable: true),
updated_at = table.Column<Instant>(type: "timestamp with time zone", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("pk_messages", x => x.id);
table.ForeignKey(
name: "fk_messages_channels_channel_id",
column: x => x.channel_id,
principalTable: "channels",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_messages_users_author_id",
column: x => x.author_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "ix_channels_guild_id",
table: "channels",
column: "guild_id");
migrationBuilder.CreateIndex(
name: "ix_guild_user_users_id",
table: "guild_user",
column: "users_id");
migrationBuilder.CreateIndex(
name: "ix_guilds_owner_id",
table: "guilds",
column: "owner_id");
migrationBuilder.CreateIndex(
name: "ix_identity_instances_domain",
table: "identity_instances",
column: "domain",
unique: true);
migrationBuilder.CreateIndex(
name: "ix_messages_author_id",
table: "messages",
column: "author_id");
migrationBuilder.CreateIndex(
name: "ix_messages_channel_id",
table: "messages",
column: "channel_id");
migrationBuilder.CreateIndex(
name: "ix_users_instance_id",
table: "users",
column: "instance_id");
migrationBuilder.CreateIndex(
name: "ix_users_remote_user_id_instance_id",
table: "users",
columns: new[] { "remote_user_id", "instance_id" },
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_users_username_instance_id ON users (lower(username), instance_id)");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "guild_user");
migrationBuilder.DropTable(
name: "instance");
migrationBuilder.DropTable(
name: "messages");
migrationBuilder.DropTable(
name: "channels");
migrationBuilder.DropTable(
name: "guilds");
migrationBuilder.DropTable(
name: "users");
migrationBuilder.DropTable(
name: "identity_instances");
}
}
}