NNTmux /
newznab-tmux
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Console\Commands; |
||
| 4 | |||
| 5 | use App\Models\UsenetGroup; |
||
| 6 | use Blacklight\ManticoreSearch; |
||
| 7 | use Illuminate\Console\Command; |
||
| 8 | use Illuminate\Support\Facades\DB; |
||
| 9 | use Illuminate\Support\Facades\File; |
||
| 10 | |||
| 11 | class NntmuxResetDb extends Command |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The name and signature of the console command. |
||
| 15 | * |
||
| 16 | * @var string |
||
| 17 | */ |
||
| 18 | protected $signature = 'nntmux:resetdb'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * The console command description. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $description = 'This command will reset your database to blank state (will retain settings)'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Create a new command instance. |
||
| 29 | * |
||
| 30 | * @return void |
||
| 31 | */ |
||
| 32 | public function __construct() |
||
| 33 | { |
||
| 34 | parent::__construct(); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @throws \Exception |
||
| 39 | */ |
||
| 40 | public function handle(): void |
||
| 41 | { |
||
| 42 | // Allow database reset only if app environment is local |
||
| 43 | if (app()->environment() !== 'local') { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 44 | $this->error('This command can only be run in local environment'); |
||
| 45 | |||
| 46 | return; |
||
| 47 | } |
||
| 48 | if ($this->confirm('This command removes all releases, nzb files, samples, previews , nfos, truncates all article tables and resets all groups. Are you sure you want reset the DB?')) { |
||
| 49 | $timestart = now(); |
||
| 50 | |||
| 51 | DB::statement('SET FOREIGN_KEY_CHECKS = 0;'); |
||
| 52 | |||
| 53 | UsenetGroup::query()->update([ |
||
| 54 | 'first_record' => 0, |
||
| 55 | 'first_record_postdate' => null, |
||
| 56 | 'last_record' => 0, |
||
| 57 | 'last_record_postdate' => null, |
||
| 58 | 'last_updated' => null, |
||
| 59 | ]); |
||
| 60 | $this->info('Reseting all groups completed.'); |
||
| 61 | |||
| 62 | $arr = [ |
||
| 63 | 'binaries', |
||
| 64 | 'collections', |
||
| 65 | 'parts', |
||
| 66 | 'missed_parts', |
||
| 67 | 'videos', |
||
| 68 | 'tv_episodes', |
||
| 69 | 'tv_info', |
||
| 70 | 'release_nfos', |
||
| 71 | 'release_comments', |
||
| 72 | 'users_releases', |
||
| 73 | 'user_movies', |
||
| 74 | 'user_series', |
||
| 75 | 'movieinfo', |
||
| 76 | 'musicinfo', |
||
| 77 | 'release_files', |
||
| 78 | 'audio_data', |
||
| 79 | 'release_subtitles', |
||
| 80 | 'video_data', |
||
| 81 | 'media_infos', |
||
| 82 | 'releases', |
||
| 83 | 'anidb_titles', |
||
| 84 | 'anidb_info', |
||
| 85 | 'releases_groups', |
||
| 86 | ]; |
||
| 87 | foreach ($arr as &$value) { |
||
| 88 | DB::statement("TRUNCATE TABLE $value"); |
||
| 89 | $this->info('Truncating '.$value.' completed.'); |
||
| 90 | } |
||
| 91 | unset($value); |
||
| 92 | |||
| 93 | if (config('nntmux.elasticsearch_enabled') === true) { |
||
| 94 | if (\Elasticsearch::indices()->exists(['index' => 'releases'])) { |
||
| 95 | \Elasticsearch::indices()->delete(['index' => 'releases']); |
||
| 96 | } |
||
| 97 | $releases_index = [ |
||
| 98 | 'index' => 'releases', |
||
| 99 | 'body' => [ |
||
| 100 | 'settings' => [ |
||
| 101 | 'number_of_shards' => 2, |
||
| 102 | 'number_of_replicas' => 0, |
||
| 103 | ], |
||
| 104 | 'mappings' => [ |
||
| 105 | 'properties' => [ |
||
| 106 | 'id' => [ |
||
| 107 | 'type' => 'long', |
||
| 108 | 'index' => false, |
||
| 109 | ], |
||
| 110 | 'name' => ['type' => 'text'], |
||
| 111 | 'searchname' => [ |
||
| 112 | 'type' => 'text', |
||
| 113 | 'fields' => [ |
||
| 114 | 'sort' => [ |
||
| 115 | 'type' => 'keyword', |
||
| 116 | ], |
||
| 117 | ], |
||
| 118 | ], |
||
| 119 | 'plainsearchname' => [ |
||
| 120 | 'type' => 'text', |
||
| 121 | 'fields' => [ |
||
| 122 | 'sort' => [ |
||
| 123 | 'type' => 'keyword', |
||
| 124 | ], |
||
| 125 | ], |
||
| 126 | ], |
||
| 127 | 'fromname' => ['type' => 'text'], |
||
| 128 | 'filename' => ['type' => 'text'], |
||
| 129 | 'add_date' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss'], |
||
| 130 | 'post_date' => ['type' => 'date', 'format' => 'yyyy-MM-dd HH:mm:ss'], |
||
| 131 | ], |
||
| 132 | ], |
||
| 133 | ], |
||
| 134 | ]; |
||
| 135 | |||
| 136 | \Elasticsearch::indices()->create($releases_index); |
||
| 137 | |||
| 138 | if (\Elasticsearch::indices()->exists(['index' => 'predb'])) { |
||
| 139 | \Elasticsearch::indices()->delete(['index' => 'predb']); |
||
| 140 | } |
||
| 141 | $predb_index = [ |
||
| 142 | 'index' => 'predb', |
||
| 143 | 'body' => [ |
||
| 144 | 'settings' => [ |
||
| 145 | 'number_of_shards' => 2, |
||
| 146 | 'number_of_replicas' => 0, |
||
| 147 | ], |
||
| 148 | 'mappings' => [ |
||
| 149 | 'properties' => [ |
||
| 150 | 'id' => [ |
||
| 151 | 'type' => 'long', |
||
| 152 | 'index' => false, |
||
| 153 | ], |
||
| 154 | 'title' => [ |
||
| 155 | 'type' => 'text', |
||
| 156 | 'fields' => [ |
||
| 157 | 'sort' => [ |
||
| 158 | 'type' => 'keyword', |
||
| 159 | ], |
||
| 160 | ], |
||
| 161 | ], |
||
| 162 | 'filename' => ['type' => 'text'], |
||
| 163 | 'source' => ['type' => 'text'], |
||
| 164 | ], |
||
| 165 | ], |
||
| 166 | ], |
||
| 167 | |||
| 168 | ]; |
||
| 169 | |||
| 170 | \Elasticsearch::indices()->create($predb_index); |
||
| 171 | |||
| 172 | $this->info('All done! ElasticSearch indexes are deleted and recreated.'); |
||
| 173 | } else { |
||
| 174 | (new ManticoreSearch)->truncateRTIndex(['releases_rt', 'predb_rt']); |
||
| 175 | } |
||
| 176 | |||
| 177 | $this->info('Deleting nzbfiles subfolders.'); |
||
| 178 | $files = File::allFiles(config('nntmux_settings.path_to_nzbs')); |
||
| 179 | File::delete($files); |
||
| 180 | |||
| 181 | $this->info('Deleting all images, previews and samples that still remain.'); |
||
| 182 | |||
| 183 | $files = File::allFiles(storage_path('covers/')); |
||
| 184 | foreach ($files as $file) { |
||
| 185 | if (basename($file) !== '.gitignore' && basename($file) !== 'no-cover.jpg' && basename($file) !== 'no-backdrop.jpg') { |
||
| 186 | File::delete($file); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | $this->call('cache:clear'); |
||
| 191 | |||
| 192 | $this->info('Deleted all releases, images, previews and samples. This script finished '.now()->diffForHumans($timestart).' start'); |
||
| 193 | DB::statement('SET FOREIGN_KEY_CHECKS = 1;'); |
||
| 194 | } else { |
||
| 195 | $this->info('Script execution stopped'); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 |