LibraryManagement / DbConnect /Migrations /20260506030549_InitialPostgres.cs
Yuyuqt
fix: changed to postgresql
ed77e1a
Raw
History Blame Contribute Delete
16.1 kB
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable
namespace DbConnect.Migrations
{
/// <inheritdoc />
public partial class InitialPostgres : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Books",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Title = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
ISBN = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false),
Author = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Available"),
IsActive = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true),
CreatedAt = table.Column<DateTime>(type: "timestamp", nullable: false, defaultValueSql: "now()"),
Description = table.Column<string>(type: "text", nullable: true),
CoverUrl = table.Column<string>(type: "text", nullable: true),
TotalCopies = table.Column<int>(type: "integer", nullable: false, defaultValue: 1),
AvailableCopies = table.Column<int>(type: "integer", nullable: false, defaultValue: 1),
UpdatedAt = table.Column<DateTime>(type: "timestamp", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK__Books__3214EC07AE1974C8", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Categories",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(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<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
MaxBooks = table.Column<int>(type: "integer", nullable: false),
BorrowingDays = table.Column<int>(type: "integer", nullable: false),
Price = table.Column<decimal>(type: "numeric(10,2)", nullable: false),
DurationMonths = table.Column<int>(type: "integer", nullable: false),
RewardId = table.Column<string>(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<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
FullName = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
Email = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: false),
PasswordHash = table.Column<string>(type: "text", nullable: false),
PhoneNumber = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true),
Role = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "User"),
IsActive = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true),
CreatedAt = table.Column<DateTime>(type: "timestamp", nullable: false, defaultValueSql: "now()"),
UpdatedAt = table.Column<DateTime>(type: "timestamp", nullable: true),
StudentId = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: true),
BanStatus = table.Column<bool>(type: "boolean", nullable: true),
SuspensionEndDate = table.Column<DateTime>(type: "timestamp", nullable: true),
Address = table.Column<string>(type: "character varying(255)", maxLength: 255, nullable: true),
FcmToken = table.Column<string>(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<int>(type: "integer", nullable: false),
CategoryId = table.Column<int>(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<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
BookId = table.Column<int>(type: "integer", nullable: false),
BorrowDate = table.Column<DateTime>(type: "timestamp", nullable: false, defaultValueSql: "now()"),
DueDate = table.Column<DateTime>(type: "timestamp", nullable: false),
ReturnDate = table.Column<DateTime>(type: "timestamp", nullable: true),
Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Active"),
FineAmount = table.Column<decimal>(type: "numeric(10,2)", nullable: false),
IsFinePaid = table.Column<bool>(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<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
Title = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
Message = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: false),
Type = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false, defaultValue: "Info"),
ActionLink = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: true),
ActionText = table.Column<string>(type: "character varying(100)", maxLength: 100, nullable: true),
IsRead = table.Column<bool>(type: "boolean", nullable: false),
CreatedAt = table.Column<DateTime>(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<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
MembershipId = table.Column<int>(type: "integer", nullable: false),
StartDate = table.Column<DateTime>(type: "timestamp", nullable: false),
ExpiryDate = table.Column<DateTime>(type: "timestamp", nullable: false),
IsActive = table.Column<bool>(type: "boolean", nullable: false, defaultValue: true),
Status = table.Column<string>(type: "character varying(20)", maxLength: 20, nullable: false, defaultValue: "Active"),
ExternalRedemptionId = table.Column<string>(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<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
UserId = table.Column<Guid>(type: "uuid", nullable: false),
BookId = table.Column<int>(type: "integer", nullable: false),
AddedAt = table.Column<DateTime>(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");
}
/// <inheritdoc />
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");
}
}
}