| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Schema\Blueprint; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Database\Migrations\Migration; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  | class CreatePlanSubscriptionsTable extends Migration | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |      * Run the migrations. | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public function up(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         Schema::create(config('rinvex.subscriptions.tables.plan_subscriptions'), function (Blueprint $table) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             $table->increments('id'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             $table->morphs('subscriber'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             $table->integer('plan_id')->unsigned(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             $table->string('slug'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             $table->{$this->jsonable()}('name'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             $table->{$this->jsonable()}('description')->nullable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             $table->dateTime('trial_ends_at')->nullable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             $table->dateTime('starts_at')->nullable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             $table->dateTime('ends_at')->nullable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             $table->dateTime('cancels_at')->nullable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $table->dateTime('canceled_at')->nullable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             $table->string('timezone')->nullable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $table->timestamps(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $table->softDeletes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             // Indexes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $table->unique('slug'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             $table->foreign('plan_id')->references('id')->on(config('rinvex.subscriptions.tables.plans')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |                   ->onDelete('cascade')->onUpdate('cascade'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         }); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * Reverse the migrations. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     public function down(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         Schema::dropIfExists(config('rinvex.subscriptions.tables.plan_subscriptions')); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * Get jsonable column data type. | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     protected function jsonable(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $driverName = DB::connection()->getPdo()->getAttribute(PDO::ATTR_DRIVER_NAME); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $dbVersion = DB::connection()->getPdo()->getAttribute(PDO::ATTR_SERVER_VERSION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $isOldVersion = version_compare($dbVersion, '5.7.8', 'lt'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $driverName === 'mysql' && $isOldVersion ? 'text' : 'json'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  |