-
-
Save syntafin/dc028e3d9a6863dcef5d26a0bbc9ea96 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands\DotSexy; | |
use App\Models\Category; | |
use App\Models\File; | |
use App\Models\Folder; | |
use App\Models\Image; | |
use App\Models\ImageComment; | |
use App\Models\Post; | |
use App\Models\PostComment; | |
use App\Models\User; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Artisan; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Password; | |
use Illuminate\Support\Facades\Storage; | |
use function Laravel\Prompts\alert; | |
use function Laravel\Prompts\confirm; | |
use function Laravel\Prompts\intro; | |
use function Laravel\Prompts\note; | |
use function Laravel\Prompts\outro; | |
use function Laravel\Prompts\spin; | |
use function Laravel\Prompts\text; | |
class Migration extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'dotsexy:migration'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Convert the old files and database to be used within the new system'; | |
/** | |
* Execute the console command. | |
*/ | |
public function handle() | |
{ | |
intro('Before continuing with the migration, please ensure the current user is able to reach the old files and has access to the database. Ensure you have set the correct paths and connection infos in ENV to the old files and database'); | |
$proceed = confirm( | |
label: 'Continue migration? (This will wipe the current database)', | |
default: false, | |
yes: 'Proceed', | |
no: 'Abort', | |
hint: 'If you abort, nothing will get changed.' | |
); | |
if (! $proceed) { | |
alert('Migration aborted'); | |
exit(); | |
} | |
spin(function () { | |
sleep(4); | |
}, | |
'Migration is running' | |
); | |
Artisan::call('down'); | |
if (app()->isLocal()) { | |
alert('Local environment detected, proceeding with default folders'); | |
$oldpath = '/home/syntafin/projects/DotSexyV4/storage/app/files'; | |
$userpath = '/home/syntafin/projects/DotSexyV4/storage/app/public'; | |
$oldb = 'local'; | |
} else { | |
note('Please provide directory paths'); | |
/*$oldpath = text( | |
label: 'Enter the absolute path to the old file storage directory', | |
placeholder: '/home/forge/example.tld/storage/app/storage/app/files', | |
required: true | |
); | |
$userpath = text( | |
label: 'Enter the absolute path to the old public storage direcotory', | |
placeholder: '/home/forge/example.tld/storage/app/public', | |
required: true | |
);*/ | |
$oldpath = $this->ask('Enter the absolute path to the old file storage directory'); | |
$userpath = $this->ask('Enter the absolute path to the old public storage direcotory'); | |
$oldb = 'old'; | |
} | |
note('Migrate Database'); | |
Artisan::call('migrate:fresh', [ | |
'--force' => true, | |
]); | |
note('Database Wipe complete'); | |
Artisan::call('db:seed'); | |
note('Seeding Database'); | |
$oldusers = DB::connection($oldb) | |
->table('users') | |
->orderBy('id', 'asc') | |
->get(); | |
$oldfolders = DB::connection($oldb) | |
->table('folders') | |
->orderBy('id', 'asc') | |
->get(); | |
$oldimages = DB::connection($oldb) | |
->table('images') | |
->orderBy('id', 'asc') | |
->get(); | |
$oldfiles = DB::connection($oldb) | |
->table('files') | |
->orderBy('id', 'asc') | |
->get(); | |
$oldcomments = DB::connection($oldb) | |
->table('comments') | |
->orderBy('id', 'asc') | |
->get(); | |
$oldposts = DB::connection($oldb) | |
->table('posts') | |
->orderBy('id', 'asc') | |
->get(); | |
$oldcats = DB::connection($oldb) | |
->table('categories') | |
->orderBy('id', 'asc') | |
->get(); | |
$postcoms = DB::connection($oldb) | |
->table('post_comments') | |
->orderBy('id', 'asc') | |
->get(); | |
User::unguard(); | |
foreach ($oldusers as $user) { | |
$u = User::create([ | |
'name' => $user->name, | |
'email' => $user->email, | |
'mastodon_domain' => $user->mastodon_domain, | |
'mastodon_token' => $user->mastodon_token, | |
'pixelfed_domain' => $user->pixelfed_domain, | |
'pixelfed_token' => $user->pixelfed_token, | |
'profile_photo_path' => $user->profile_photo_path, | |
'profile_banner_path' => $user->profile_banner_path, | |
'password' => null, | |
]); | |
if (app()->environment('production')) { | |
Password::sendResetLink([ | |
'email' => $u->email, | |
]); | |
} | |
} | |
User::reguard(); | |
note('All Users migrated'); | |
note('Start migrating Folders, Files and Images'); | |
Folder::unguard(); | |
foreach ($oldfolders as $folder) { | |
Folder::create([ | |
'id' => $folder->id, | |
'name' => $folder->name, | |
'slug' => $folder->slug, | |
'description' => $folder->description, | |
'user_id' => $folder->user_id, | |
'webhook_url' => $folder->webhook_url, | |
'private' => $folder->private, | |
'public' => $folder->public, | |
'created_at' => $folder->created_at, | |
'deleted_at' => $folder->deleted_at, | |
]); | |
} | |
Folder::reguard(); | |
note('Folders migrated'); | |
Image::unguard(); | |
foreach ($oldimages as $image) { | |
Image::create([ | |
'id' => $image->id, | |
'name' => $image->name, | |
'slug' => $image->slug, | |
'shortkey' => $image->shortkey, | |
'description' => $image->description, | |
'status' => $image->status, | |
'watermark' => $image->watermark, | |
'user_id' => $image->user_id, | |
'folder_id' => $image->folder_id, | |
'created_at' => $image->created_at, | |
'deleted_at' => $image->deleted_at, | |
]); | |
} | |
Image::reguard(); | |
note('Images migrated'); | |
/* Create temporary disk instance */ | |
$disk = Storage::build([ | |
'driver' => 'local', | |
'root' => $oldpath, | |
]); | |
File::unguard(); | |
foreach ($oldfiles as $file) { | |
$filename = $file->filename.'.'.$file->extension; | |
$of = $disk->path($filename); | |
Storage::putFileAs('files', $of, $filename); | |
File::create([ | |
'id' => $file->id, | |
'filename' => $filename, | |
'extension' => $file->extension, | |
'mime' => $file->mime, | |
'original_filename' => $file->original_filename, | |
'filesize' => $file->filesize, | |
'width' => $file->width, | |
'height' => $file->height, | |
'user_id' => $file->user_id, | |
'image_id' => $file->image_id, | |
'created_at' => $file->created_at, | |
'deleted_at' => $file->deleted_at, | |
]); | |
} | |
File::reguard(); | |
note('Files migrated'); | |
note('Moving User files to new place'); | |
$userdisk = Storage::build([ | |
'driver' => 'local', | |
'root' => $userpath, | |
]); | |
foreach ($userdisk->files('profile-photos') as $file) { | |
Storage::putFileAs('public/profile-photos', $userpath.'/'.$file, basename($file)); | |
} | |
note('Profile Photos migrated'); | |
foreach ($userdisk->files('profile-banner') as $file) { | |
Storage::putFileAs('public/profile-banner', $userpath.'/'.$file, basename($file)); | |
} | |
note('Profile Banner migrated'); | |
ImageComment::unguard(); | |
foreach ($oldcomments as $comment) { | |
ImageComment::create([ | |
'id' => $comment->id, | |
'user_id' => $comment->user_id, | |
'image_id' => $comment->image_id, | |
'is_approved' => true, | |
'text' => $comment->content, | |
'created_at' => $comment->created_at, | |
'deleted_at' => $comment->deleted_at, | |
]); | |
} | |
ImageComment::reguard(); | |
Category::unguard(); | |
foreach ($oldcats as $cat) { | |
Category::create([ | |
'id' => $cat->id, | |
'name' => $cat->name, | |
'slug' => $cat->slug, | |
'user_id' => 1, | |
'created_at' => $cat->created_at, | |
'deleted_at' => $cat->deleted_at, | |
]); | |
} | |
Category::reguard(); | |
Post::unguard(); | |
foreach ($oldposts as $post) { | |
$preview = DB::connection($oldb) | |
->table('previews') | |
->where('post_id', '=', $post->id) | |
->pluck('filename') | |
->get('filename'); | |
Post::create([ | |
'id' => $post->id, | |
'title' => $post->title, | |
'slug' => $post->slug, | |
'user_id' => $post->user_id, | |
'category_id' => $post->category_id, | |
'is_approved' => $post->is_approved, | |
'is_published' => $post->is_published, | |
'text' => $post->body, | |
'preview' => $preview, | |
'created_at' => $post->created_at, | |
'deleted_at' => $post->deleted_at, | |
]); | |
} | |
Post::reguard(); | |
PostComment::unguard(); | |
foreach ($postcoms as $com) { | |
PostComment::create([ | |
'id' => $com->id, | |
'user_id' => $com->user_id, | |
'post_id' => $com->post_id, | |
'text' => $com->body, | |
'is_approved' => $com->is_approved, | |
'created_at' => $com->created_at, | |
'deleted_at' => $com->deleted_at, | |
]); | |
} | |
PostComment::reguard(); | |
outro('Migration complete!'); | |
Artisan::call('up'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment