diff --git a/database/init.sql b/database/init.sql new file mode 100644 index 0000000..d4d56ef --- /dev/null +++ b/database/init.sql @@ -0,0 +1,142 @@ +-- 设置字符集和时区 +SET NAMES utf8mb4; +SET TIME_ZONE = '+08:00'; + +-- 创建数据库 +DROP DATABASE IF EXISTS `aiblog`; +CREATE DATABASE IF NOT EXISTS `aiblog` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; +USE `aiblog`; + +-- 创建 users 表 +CREATE TABLE `users` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `account` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `email_verified_at` timestamp NULL DEFAULT NULL, + `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `role` enum('admin','author') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'author', + `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `users_account_unique` (`account`), + UNIQUE KEY `users_email_unique` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 创建 password_reset_tokens 表 +CREATE TABLE `password_reset_tokens` ( + `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `created_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 创建 failed_jobs 表 +CREATE TABLE `failed_jobs` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `uuid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `connection` text COLLATE utf8mb4_unicode_ci NOT NULL, + `queue` text COLLATE utf8mb4_unicode_ci NOT NULL, + `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL, + `exception` longtext COLLATE utf8mb4_unicode_ci NOT NULL, + `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `failed_jobs_uuid_unique` (`uuid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 创建 personal_access_tokens 表 +CREATE TABLE `personal_access_tokens` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `tokenable_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `tokenable_id` bigint unsigned NOT NULL, + `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `token` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `abilities` text COLLATE utf8mb4_unicode_ci, + `last_used_at` timestamp NULL DEFAULT NULL, + `expires_at` timestamp NULL DEFAULT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `personal_access_tokens_token_unique` (`token`), + KEY `personal_access_tokens_tokenable_type_tokenable_id_index` (`tokenable_type`,`tokenable_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 创建 categories 表 +CREATE TABLE `categories` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `description` text COLLATE utf8mb4_unicode_ci, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `categories_name_unique` (`name`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 创建 posts 表 +CREATE TABLE `posts` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `summary` varchar(500) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `content` text COLLATE utf8mb4_unicode_ci NOT NULL, + `author_id` bigint unsigned NOT NULL, + `category_id` bigint unsigned DEFAULT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `posts_author_id_foreign` (`author_id`), + KEY `posts_category_id_foreign` (`category_id`), + CONSTRAINT `posts_author_id_foreign` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE CASCADE, + CONSTRAINT `posts_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `categories` (`id`) ON DELETE SET NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 创建 comments 表 +CREATE TABLE `comments` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `post_id` bigint unsigned NOT NULL, + `content` text COLLATE utf8mb4_unicode_ci NOT NULL, + `author_id` bigint unsigned NOT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `comments_post_id_foreign` (`post_id`), + KEY `comments_author_id_foreign` (`author_id`), + CONSTRAINT `comments_author_id_foreign` FOREIGN KEY (`author_id`) REFERENCES `users` (`id`) ON DELETE CASCADE, + CONSTRAINT `comments_post_id_foreign` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 创建 activity_logs 表 +CREATE TABLE `activity_logs` ( + `id` bigint unsigned NOT NULL AUTO_INCREMENT, + `user_id` bigint unsigned NOT NULL, + `action` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `model_type` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `model_id` bigint unsigned NOT NULL, + `description` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `properties` json DEFAULT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `activity_logs_user_id_foreign` (`user_id`), + KEY `activity_logs_model_type_model_id_index` (`model_type`,`model_id`), + CONSTRAINT `activity_logs_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 插入默认管理员账号 +INSERT INTO `users` ( + `account`, + `name`, + `email`, + `password`, + `role`, + `created_at`, + `updated_at` +) VALUES ( + 'admin', + 'Super Admin', + 'admin@example.com', + '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', -- password + 'admin', + NOW(), + NOW() +); \ No newline at end of file diff --git a/database/migrations/2024_01_01_000000_add_role_to_users_table.php b/database/migrations/2024_01_01_000000_add_role_to_users_table.php deleted file mode 100644 index 9a9c213..0000000 --- a/database/migrations/2024_01_01_000000_add_role_to_users_table.php +++ /dev/null @@ -1,24 +0,0 @@ -enum('role', ['admin', 'author'])->default('author')->after('password'); - }); - } - - public function down(): void - { - Schema::table('users', function (Blueprint $table) { - $table->dropColumn('role'); - }); - } -}; \ No newline at end of file diff --git a/database/migrations/2024_01_01_000001_add_views_and_soft_deletes_to_posts.php b/database/migrations/2024_01_01_000001_add_views_and_soft_deletes_to_posts.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/2024_01_01_000001_add_views_and_soft_deletes_to_posts.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/2024_01_01_000001_create_categories_table.php b/database/migrations/2024_01_01_000001_create_categories_table.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/2024_01_01_000001_create_categories_table.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/2024_01_01_000002_add_soft_deletes_to_comments.php b/database/migrations/2024_01_01_000002_add_soft_deletes_to_comments.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/2024_01_01_000002_add_soft_deletes_to_comments.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/2024_01_01_000002_create_posts_table.php b/database/migrations/2024_01_01_000002_create_posts_table.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/2024_01_01_000002_create_posts_table.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/2024_01_01_000003_add_account_to_users.php b/database/migrations/2024_01_01_000003_add_account_to_users.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/2024_01_01_000003_add_account_to_users.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/2024_01_01_000003_create_comments_table.php b/database/migrations/2024_01_01_000003_create_comments_table.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/2024_01_01_000003_create_comments_table.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/2024_01_02_000000_add_soft_deletes_to_comments_table.php b/database/migrations/2024_01_02_000000_add_soft_deletes_to_comments_table.php deleted file mode 100644 index b36b76a..0000000 --- a/database/migrations/2024_01_02_000000_add_soft_deletes_to_comments_table.php +++ /dev/null @@ -1,24 +0,0 @@ -softDeletes(); - }); - } - - public function down(): void - { - Schema::table('comments', function (Blueprint $table) { - $table->dropSoftDeletes(); - }); - } -}; \ No newline at end of file diff --git a/database/migrations/2024_01_02_000000_add_soft_deletes_to_posts_table.php b/database/migrations/2024_01_02_000000_add_soft_deletes_to_posts_table.php deleted file mode 100644 index f36d661..0000000 --- a/database/migrations/2024_01_02_000000_add_soft_deletes_to_posts_table.php +++ /dev/null @@ -1,26 +0,0 @@ -use Illuminate\Database\Migrations\Migration; -use Illuminate\Database\Schema\Blueprint; -use Illuminate\Support\Facades\Schema; - -return new class extends Migration -{ - /** - * Run the migrations. - */ - public function up(): void - { - Schema::table('posts', function (Blueprint $table) { - $table->softDeletes(); - }); - } - - /** - * Reverse the migrations. - */ - public function down(): void - { - Schema::table('posts', function (Blueprint $table) { - $table->dropSoftDeletes(); - }); - } -}; \ No newline at end of file diff --git a/database/migrations/2024_01_03_000000_add_views_to_posts_table.php b/database/migrations/2024_01_03_000000_add_views_to_posts_table.php deleted file mode 100644 index 4a9b4d9..0000000 --- a/database/migrations/2024_01_03_000000_add_views_to_posts_table.php +++ /dev/null @@ -1,24 +0,0 @@ -unsignedInteger('views')->default(0)->after('category_id'); - }); - } - - public function down(): void - { - Schema::table('posts', function (Blueprint $table) { - $table->dropColumn('views'); - }); - } -}; \ No newline at end of file diff --git a/database/migrations/2024_01_04_000000_add_account_to_users_table.php b/database/migrations/2024_11_17_035301_add_account_and_role_to_users.php similarity index 75% rename from database/migrations/2024_01_04_000000_add_account_to_users_table.php rename to database/migrations/2024_11_17_035301_add_account_and_role_to_users.php index dfc3e75..5773e07 100644 --- a/database/migrations/2024_01_04_000000_add_account_to_users_table.php +++ b/database/migrations/2024_11_17_035301_add_account_and_role_to_users.php @@ -1,24 +1,28 @@ string('account')->unique()->after('id'); + // }); } + /** + * Reverse the migrations. + */ public function down(): void { Schema::table('users', function (Blueprint $table) { - $table->dropColumn('account'); + // }); } -}; \ No newline at end of file +}; diff --git a/database/migrations/[timestamp]_add_account_and_role_to_users.php b/database/migrations/[timestamp]_add_account_and_role_to_users.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/[timestamp]_add_account_and_role_to_users.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/[timestamp]_add_soft_deletes_to_comments.php b/database/migrations/[timestamp]_add_soft_deletes_to_comments.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/[timestamp]_add_soft_deletes_to_comments.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/[timestamp]_add_soft_deletes_to_posts.php b/database/migrations/[timestamp]_add_soft_deletes_to_posts.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/[timestamp]_add_soft_deletes_to_posts.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/[timestamp]_add_views_and_soft_deletes_to_posts.php b/database/migrations/[timestamp]_add_views_and_soft_deletes_to_posts.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/[timestamp]_add_views_and_soft_deletes_to_posts.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/[timestamp]_create_categories_table.php b/database/migrations/[timestamp]_create_categories_table.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/[timestamp]_create_categories_table.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/[timestamp]_create_comments_table.php b/database/migrations/[timestamp]_create_comments_table.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/[timestamp]_create_comments_table.php @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/database/migrations/[timestamp]_create_posts_table.php b/database/migrations/[timestamp]_create_posts_table.php new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/database/migrations/[timestamp]_create_posts_table.php @@ -0,0 +1 @@ + \ No newline at end of file