-
-
Notifications
You must be signed in to change notification settings - Fork 1
feat: show id in content #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 2.x
Are you sure you want to change the base?
Changes from all commits
8002c4b
8c15ccd
6ffd18e
f4d2a42
eb81c71
4ae0308
9f1d89b
7f03414
de39612
4890a4d
b0b2b08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| /** | ||
| * Run the migrations. | ||
| */ | ||
| public function up(): void | ||
| { | ||
| if (Schema::hasColumn('content', 'id')) { | ||
| return; | ||
| } | ||
|
|
||
| Schema::table('content', function (Blueprint $table) { | ||
| $table->unsignedBigInteger('id')->after('ulid')->nullable(); | ||
| }); | ||
|
|
||
| // Fill the id column for existing content based on creation order (database-agnostic) | ||
| $contents = DB::table('content')->orderBy('created_at', 'asc')->get(['ulid']); | ||
| $id = 1; | ||
| foreach ($contents as $content) { | ||
| DB::table('content')->where('ulid', $content->ulid)->update(['id' => $id++]); | ||
| } | ||
|
|
||
| // Add unique index and make NOT NULL | ||
| Schema::table('content', function (Blueprint $table) { | ||
| $table->unsignedBigInteger('id')->nullable(false)->unique()->change(); | ||
| }); | ||
|
|
||
| // Set auto-increment using database-specific syntax | ||
| $driver = DB::getDriverName(); | ||
|
|
||
| match ($driver) { | ||
| 'mysql' => DB::statement('ALTER TABLE content MODIFY id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT'), | ||
| 'pgsql' => DB::statement('CREATE SEQUENCE IF NOT EXISTS content_id_seq OWNED BY content.id; | ||
| SELECT setval(\'content_id_seq\', COALESCE((SELECT MAX(id) FROM content), 0) + 1, false); | ||
| ALTER TABLE content ALTER COLUMN id SET DEFAULT nextval(\'content_id_seq\')'), | ||
| 'sqlite' => null, // SQLite doesn't support adding autoincrement to existing columns | ||
| 'sqlsrv' => DB::statement('ALTER TABLE content ALTER COLUMN id BIGINT NOT NULL'), | ||
| default => null, | ||
| }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Migration breaks inserts for SQLite and SQL ServerHigh Severity The migration adds a NOT NULL |
||
| } | ||
|
|
||
| /** | ||
| * Reverse the migrations. | ||
| */ | ||
| public function down(): void | ||
| { | ||
| if (! Schema::hasColumn('content', 'id')) { | ||
| return; | ||
| } | ||
|
|
||
| Schema::table('content', function (Blueprint $table) { | ||
| $table->dropColumn('id'); | ||
| }); | ||
cursor[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.