using System; using Microsoft.EntityFrameworkCore.Migrations; #nullable disable namespace Messenger.Api.Data.Migrations { /// public partial class InitialSchema : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "chats", columns: table => new { id = table.Column(type: "uuid", nullable: false), title = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), created_at = table.Column(type: "timestamp with time zone", nullable: false), history_version = table.Column(type: "bigint", nullable: false, defaultValue: 0L) }, constraints: table => { table.PrimaryKey("pk_chats", x => x.id); table.CheckConstraint("ck_chats_history_version_nonneg", "history_version >= 0"); table.CheckConstraint("ck_chats_title_not_blank", "title ~ '\\S'"); }); migrationBuilder.CreateTable( name: "users", columns: table => new { id = table.Column(type: "uuid", nullable: false), username = table.Column(type: "character varying(32)", maxLength: 32, nullable: false), display_name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), password_hash = table.Column(type: "text", nullable: false), created_at = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("pk_users", x => x.id); table.CheckConstraint("ck_users_display_name_not_blank", "display_name ~ '\\S'"); table.CheckConstraint("ck_users_username_format", "username ~ '^[a-z0-9_]{3,32}$'"); }); migrationBuilder.CreateTable( name: "chat_members", columns: table => new { chat_id = table.Column(type: "uuid", nullable: false), user_id = table.Column(type: "uuid", nullable: false) }, constraints: table => { table.PrimaryKey("pk_chat_members", x => new { x.chat_id, x.user_id }); table.ForeignKey( name: "fk_chat_members_chats_chat_id", column: x => x.chat_id, principalTable: "chats", principalColumn: "id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "fk_chat_members_users_user_id", column: x => x.user_id, principalTable: "users", principalColumn: "id", onDelete: ReferentialAction.Restrict); }); migrationBuilder.CreateTable( name: "sessions", columns: table => new { id = table.Column(type: "uuid", nullable: false), token_hash = table.Column(type: "bytea", nullable: false), user_id = table.Column(type: "uuid", nullable: false), created_at = table.Column(type: "timestamp with time zone", nullable: false), expires_at = table.Column(type: "timestamp with time zone", nullable: false), revoked_at = table.Column(type: "timestamp with time zone", nullable: true) }, constraints: table => { table.PrimaryKey("pk_sessions", x => x.id); table.CheckConstraint("ck_sessions_expiry_after_creation", "expires_at > created_at"); table.CheckConstraint("ck_sessions_token_hash_len", "octet_length(token_hash) = 32"); table.ForeignKey( name: "fk_sessions_users_user_id", column: x => x.user_id, principalTable: "users", principalColumn: "id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "messages", columns: table => new { id = table.Column(type: "uuid", nullable: false), chat_id = table.Column(type: "uuid", nullable: false), sender_id = table.Column(type: "uuid", nullable: false), text = table.Column(type: "character varying(4000)", maxLength: 4000, nullable: false), created_at = table.Column(type: "timestamp with time zone", nullable: false) }, constraints: table => { table.PrimaryKey("pk_messages", x => x.id); table.CheckConstraint("ck_messages_text_not_blank", "text ~ '\\S'"); table.ForeignKey( name: "fk_messages_chat_members_chat_id_sender_id", columns: x => new { x.chat_id, x.sender_id }, principalTable: "chat_members", principalColumns: new[] { "chat_id", "user_id" }, onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( name: "ix_chat_members_user_id", table: "chat_members", column: "user_id"); migrationBuilder.CreateIndex( name: "ix_chats_created_at_id", table: "chats", columns: new[] { "created_at", "id" }); migrationBuilder.CreateIndex( name: "ix_messages_chat_history", table: "messages", columns: new[] { "chat_id", "created_at", "id" }); migrationBuilder.CreateIndex( name: "ix_messages_chat_id_sender_id", table: "messages", columns: new[] { "chat_id", "sender_id" }); migrationBuilder.CreateIndex( name: "ix_sessions_expires_at", table: "sessions", column: "expires_at"); migrationBuilder.CreateIndex( name: "ix_sessions_user_id", table: "sessions", column: "user_id"); migrationBuilder.CreateIndex( name: "ux_sessions_token_hash", table: "sessions", column: "token_hash", unique: true); migrationBuilder.CreateIndex( name: "ux_users_username", table: "users", column: "username", unique: true); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "messages"); migrationBuilder.DropTable( name: "sessions"); migrationBuilder.DropTable( name: "chat_members"); migrationBuilder.DropTable( name: "chats"); migrationBuilder.DropTable( name: "users"); } } }