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