| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Database\Migrations; |
| 4 |
|
| 5 |
|
| 6 |
use FluentCart\Framework\Database\Schema; |
| 7 |
|
| 8 |
class TaxClassesMigrator extends Migrator |
| 9 |
{ |
| 10 |
|
| 11 |
public static string $tableName = 'fct_tax_classes'; |
| 12 |
|
| 13 |
public static function getSqlSchema(): string |
| 14 |
{ |
| 15 |
$indexPrefix = static::getDbPrefix() . 'fct_tcl_'; |
| 16 |
return "`id` BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, |
| 17 |
`title` VARCHAR(192) NULL, |
| 18 |
`slug` VARCHAR(100) NULL, |
| 19 |
`description` longtext NULL, |
| 20 |
`meta` json DEFAULT NULL, |
| 21 |
`created_at` DATETIME NULL , |
| 22 |
`updated_at` DATETIME NULL"; |
| 23 |
} |
| 24 |
|
| 25 |
public static function migrated() |
| 26 |
{ |
| 27 |
static::renameCategoriesToMeta(); |
| 28 |
static::addMetaColumn(); |
| 29 |
static::addSlugColumn(); |
| 30 |
static::addDescriptionColumn(); |
| 31 |
} |
| 32 |
|
| 33 |
public static function renameCategoriesToMeta() |
| 34 |
{ |
| 35 |
// "ALTER TABLE %i CHANGE `categories` `meta` JSON" |
| 36 |
// Only rename if categories exists and meta doesn't (avoid collision) |
| 37 |
if (Schema::hasColumn('categories', static::$tableName) && !Schema::hasColumn('meta', static::$tableName)) { |
| 38 |
Schema::alterTable( |
| 39 |
static::$tableName, |
| 40 |
"CHANGE `categories` `meta` JSON" |
| 41 |
); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public static function addMetaColumn() |
| 46 |
{ |
| 47 |
// "ALTER TABLE %i ADD COLUMN `meta` JSON" |
| 48 |
static::addColumnIfNotExists('meta', 'JSON'); |
| 49 |
} |
| 50 |
|
| 51 |
public static function addSlugColumn() |
| 52 |
{ |
| 53 |
// "ALTER TABLE %i ADD COLUMN `slug` VARCHAR(100) NULL AFTER `title`" |
| 54 |
static::addColumnIfNotExists('slug', 'VARCHAR(100) NULL', 'title'); |
| 55 |
} |
| 56 |
|
| 57 |
public static function addDescriptionColumn() |
| 58 |
{ |
| 59 |
// "ALTER TABLE %i ADD COLUMN `description` LONGTEXT NULL AFTER `slug`" |
| 60 |
static::addColumnIfNotExists('description', 'LONGTEXT NULL', 'slug'); |
| 61 |
} |
| 62 |
} |
| 63 |
|