CreateMigrationsTable.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\MigrationLog\Migrations; |
| 4 | |
| 5 | use Give\Framework\Database\DB; |
| 6 | use Give\Framework\Database\Exceptions\DatabaseQueryException; |
| 7 | use Give\Framework\Migrations\Contracts\Migration; |
| 8 | use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; |
| 9 | |
| 10 | /** |
| 11 | * Class CreateMigrationsTable |
| 12 | * @package Give\MigrationLog\Migrations |
| 13 | * |
| 14 | * @since 2.10.0 |
| 15 | */ |
| 16 | class CreateMigrationsTable extends Migration |
| 17 | { |
| 18 | /** |
| 19 | * @return string |
| 20 | */ |
| 21 | public static function id() |
| 22 | { |
| 23 | return 'create_migrations_table'; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * @return string |
| 28 | */ |
| 29 | public static function title() |
| 30 | { |
| 31 | return esc_html__('Create new give_migrations table', 'give'); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @return int |
| 36 | */ |
| 37 | public static function timestamp() |
| 38 | { |
| 39 | /** |
| 40 | * For this migration, we have to use the earliest possible date because we will be using |
| 41 | * the table created with this migration to store the status of the migration |
| 42 | */ |
| 43 | return strtotime('1970-01-01 00:00'); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @since 2.21.0 Add Check whether table installed before adding it to database. |
| 48 | * @throws DatabaseMigrationException |
| 49 | */ |
| 50 | public function run() |
| 51 | { |
| 52 | $table = DB::prefix('give_migrations'); |
| 53 | $charset = DB::get_charset_collate(); |
| 54 | |
| 55 | $sql = "CREATE TABLE IF NOT EXISTS {$table} ( |
| 56 | id VARCHAR(180) NOT NULL, |
| 57 | status VARCHAR(16) NOT NULL, |
| 58 | error text NULL, |
| 59 | last_run DATETIME NOT NULL, |
| 60 | PRIMARY KEY (id) |
| 61 | ) {$charset}"; |
| 62 | |
| 63 | try { |
| 64 | DB::delta($sql); |
| 65 | } catch (DatabaseQueryException $exception) { |
| 66 | throw new DatabaseMigrationException("An error occurred while creating the {$table} table", 0, $exception); |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 |