using System; using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; #nullable disable namespace DbConnect.Migrations { /// public partial class InitialPostgres : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( name: "Books", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Title = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), ISBN = table.Column(type: "character varying(20)", maxLength: 20, nullable: false), Author = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), Status = table.Column(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Available"), IsActive = table.Column(type: "boolean", nullable: false, defaultValue: true), CreatedAt = table.Column(type: "timestamp", nullable: false, defaultValueSql: "now()"), Description = table.Column(type: "text", nullable: true), CoverUrl = table.Column(type: "text", nullable: true), TotalCopies = table.Column(type: "integer", nullable: false, defaultValue: 1), AvailableCopies = table.Column(type: "integer", nullable: false, defaultValue: 1), UpdatedAt = table.Column(type: "timestamp", nullable: true) }, constraints: table => { table.PrimaryKey("PK__Books__3214EC07AE1974C8", x => x.Id); }); migrationBuilder.CreateTable( name: "Categories", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false) }, constraints: table => { table.PrimaryKey("PK__Categori__3214EC0751B5797D", x => x.Id); }); migrationBuilder.CreateTable( name: "Memberships", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), Type = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), MaxBooks = table.Column(type: "integer", nullable: false), BorrowingDays = table.Column(type: "integer", nullable: false), Price = table.Column(type: "numeric(10,2)", nullable: false), DurationMonths = table.Column(type: "integer", nullable: false), RewardId = table.Column(type: "character varying(100)", maxLength: 100, nullable: true) }, constraints: table => { table.PrimaryKey("PK__Membersh__3214EC07A6F5A55F", x => x.Id); }); migrationBuilder.CreateTable( name: "Users", columns: table => new { Id = table.Column(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"), FullName = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), Email = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), PasswordHash = table.Column(type: "text", nullable: false), PhoneNumber = table.Column(type: "character varying(20)", maxLength: 20, nullable: true), Role = table.Column(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "User"), IsActive = table.Column(type: "boolean", nullable: false, defaultValue: true), CreatedAt = table.Column(type: "timestamp", nullable: false, defaultValueSql: "now()"), UpdatedAt = table.Column(type: "timestamp", nullable: true), StudentId = table.Column(type: "character varying(20)", maxLength: 20, nullable: true), BanStatus = table.Column(type: "boolean", nullable: true), SuspensionEndDate = table.Column(type: "timestamp", nullable: true), Address = table.Column(type: "character varying(255)", maxLength: 255, nullable: true), FcmToken = table.Column(type: "character varying(500)", maxLength: 500, nullable: true) }, constraints: table => { table.PrimaryKey("PK_Users", x => x.Id); }); migrationBuilder.CreateTable( name: "BookCategories", columns: table => new { BookId = table.Column(type: "integer", nullable: false), CategoryId = table.Column(type: "integer", nullable: false) }, constraints: table => { table.PrimaryKey("PK__BookCate__9C7051A74E7D221D", x => new { x.BookId, x.CategoryId }); table.ForeignKey( name: "FK_BookCategories_Books", column: x => x.BookId, principalTable: "Books", principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_BookCategories_Categories", column: x => x.CategoryId, principalTable: "Categories", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "Borrowings", columns: table => new { Id = table.Column(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"), UserId = table.Column(type: "uuid", nullable: false), BookId = table.Column(type: "integer", nullable: false), BorrowDate = table.Column(type: "timestamp", nullable: false, defaultValueSql: "now()"), DueDate = table.Column(type: "timestamp", nullable: false), ReturnDate = table.Column(type: "timestamp", nullable: true), Status = table.Column(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Active"), FineAmount = table.Column(type: "numeric(10,2)", nullable: false), IsFinePaid = table.Column(type: "boolean", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Borrowings", x => x.Id); table.ForeignKey( name: "FK_Borrowings_Books", column: x => x.BookId, principalTable: "Books", principalColumn: "Id"); table.ForeignKey( name: "FK_Borrowings_Users", column: x => x.UserId, principalTable: "Users", principalColumn: "Id"); }); migrationBuilder.CreateTable( name: "Notifications", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), UserId = table.Column(type: "uuid", nullable: false), Title = table.Column(type: "character varying(200)", maxLength: 200, nullable: false), Message = table.Column(type: "character varying(1000)", maxLength: 1000, nullable: false), Type = table.Column(type: "character varying(50)", maxLength: 50, nullable: false, defaultValue: "Info"), ActionLink = table.Column(type: "character varying(500)", maxLength: 500, nullable: true), ActionText = table.Column(type: "character varying(100)", maxLength: 100, nullable: true), IsRead = table.Column(type: "boolean", nullable: false), CreatedAt = table.Column(type: "timestamp", nullable: false, defaultValueSql: "now()") }, constraints: table => { table.PrimaryKey("PK_Notifications", x => x.Id); table.ForeignKey( name: "FK_Notifications_Users", column: x => x.UserId, principalTable: "Users", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateTable( name: "UserSubscriptions", columns: table => new { Id = table.Column(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"), UserId = table.Column(type: "uuid", nullable: false), MembershipId = table.Column(type: "integer", nullable: false), StartDate = table.Column(type: "timestamp", nullable: false), ExpiryDate = table.Column(type: "timestamp", nullable: false), IsActive = table.Column(type: "boolean", nullable: false, defaultValue: true), Status = table.Column(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Active"), ExternalRedemptionId = table.Column(type: "character varying(100)", maxLength: 100, nullable: true) }, constraints: table => { table.PrimaryKey("PK_UserSubscriptions", x => x.Id); table.ForeignKey( name: "FK_UserSubscriptions_Memberships", column: x => x.MembershipId, principalTable: "Memberships", principalColumn: "Id"); table.ForeignKey( name: "FK_UserSubscriptions_Users", column: x => x.UserId, principalTable: "Users", principalColumn: "Id"); }); migrationBuilder.CreateTable( name: "WishlistItems", columns: table => new { Id = table.Column(type: "integer", nullable: false) .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), UserId = table.Column(type: "uuid", nullable: false), BookId = table.Column(type: "integer", nullable: false), AddedAt = table.Column(type: "timestamp", nullable: false, defaultValueSql: "now()") }, constraints: table => { table.PrimaryKey("PK_WishlistItems", x => x.Id); table.ForeignKey( name: "FK_WishlistItems_Books", column: x => x.BookId, principalTable: "Books", principalColumn: "Id", onDelete: ReferentialAction.Cascade); table.ForeignKey( name: "FK_WishlistItems_Users", column: x => x.UserId, principalTable: "Users", principalColumn: "Id", onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( name: "IX_BookCategories_CategoryId", table: "BookCategories", column: "CategoryId"); migrationBuilder.CreateIndex( name: "UQ__Books__447D36EA79CF66CE", table: "Books", column: "ISBN", unique: true); migrationBuilder.CreateIndex( name: "IX_Borrowings_BookId", table: "Borrowings", column: "BookId"); migrationBuilder.CreateIndex( name: "IX_Borrowings_UserId", table: "Borrowings", column: "UserId"); migrationBuilder.CreateIndex( name: "UQ__Categori__737584F622B16D3A", table: "Categories", column: "Name", unique: true); migrationBuilder.CreateIndex( name: "IX_Memberships_Type", table: "Memberships", column: "Type", unique: true); migrationBuilder.CreateIndex( name: "IX_Notifications_UserId", table: "Notifications", column: "UserId"); migrationBuilder.CreateIndex( name: "UQ_Users_Email", table: "Users", column: "Email", unique: true); migrationBuilder.CreateIndex( name: "IX_UserSubscriptions_MembershipId", table: "UserSubscriptions", column: "MembershipId"); migrationBuilder.CreateIndex( name: "IX_UserSubscriptions_UserId", table: "UserSubscriptions", column: "UserId"); migrationBuilder.CreateIndex( name: "IX_WishlistItems_BookId", table: "WishlistItems", column: "BookId"); migrationBuilder.CreateIndex( name: "IX_WishlistItems_UserId", table: "WishlistItems", column: "UserId"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( name: "BookCategories"); migrationBuilder.DropTable( name: "Borrowings"); migrationBuilder.DropTable( name: "Notifications"); migrationBuilder.DropTable( name: "UserSubscriptions"); migrationBuilder.DropTable( name: "WishlistItems"); migrationBuilder.DropTable( name: "Categories"); migrationBuilder.DropTable( name: "Memberships"); migrationBuilder.DropTable( name: "Books"); migrationBuilder.DropTable( name: "Users"); } } }