@@ -59,10 +59,10 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | protected $migration_log; |
| 61 | 61 | |
| 62 | - public function __construct( Builder $builder, wpdb $wpdb, ?string $migration_log_key = null ) { |
|
| 62 | + public function __construct(Builder $builder, wpdb $wpdb, ?string $migration_log_key = null) { |
|
| 63 | 63 | $this->builder = $builder; |
| 64 | 64 | $this->wpdb = $wpdb; |
| 65 | - $this->migration_log = new Migration_Log_Manager( $migration_log_key ); |
|
| 65 | + $this->migration_log = new Migration_Log_Manager($migration_log_key); |
|
| 66 | 66 | |
| 67 | 67 | } |
| 68 | 68 | |
@@ -94,7 +94,7 @@ discard block |
||
| 94 | 94 | * @param \PinkCrab\DB_Migration\Database_Migration $migration |
| 95 | 95 | * @return self |
| 96 | 96 | */ |
| 97 | - public function add_migration( Database_Migration $migration ): self { |
|
| 97 | + public function add_migration(Database_Migration $migration): self { |
|
| 98 | 98 | $this->migrations[ $migration->get_table_name() ] = $migration; |
| 99 | 99 | return $this; |
| 100 | 100 | } |
@@ -114,22 +114,22 @@ discard block |
||
| 114 | 114 | * @param string ...$excluded_table Table names to exclude. |
| 115 | 115 | * @return self |
| 116 | 116 | */ |
| 117 | - public function create_tables( string ...$excluded_table ): self { |
|
| 117 | + public function create_tables(string ...$excluded_table): self { |
|
| 118 | 118 | |
| 119 | 119 | // Remove excluded tables. |
| 120 | 120 | $to_create = array_filter( |
| 121 | 121 | $this->migrations, |
| 122 | - function( Database_Migration $migration ) use ( $excluded_table ): bool { |
|
| 123 | - return ! in_array( $migration->get_table_name(), $excluded_table, true ) |
|
| 124 | - && $this->migration_log->can_migrate( $migration->get_schema() ); |
|
| 122 | + function(Database_Migration $migration) use ($excluded_table): bool { |
|
| 123 | + return ! in_array($migration->get_table_name(), $excluded_table, true) |
|
| 124 | + && $this->migration_log->can_migrate($migration->get_schema()); |
|
| 125 | 125 | } |
| 126 | 126 | ); |
| 127 | 127 | |
| 128 | 128 | // Upsert Tables. |
| 129 | - foreach ( $to_create as $migration ) { |
|
| 130 | - $result = $this->builder->create_table( $migration->get_schema() ); |
|
| 131 | - if ( $result === true ) { |
|
| 132 | - $this->migration_log->upsert_migration( $migration->get_schema() ); |
|
| 129 | + foreach ($to_create as $migration) { |
|
| 130 | + $result = $this->builder->create_table($migration->get_schema()); |
|
| 131 | + if ($result === true) { |
|
| 132 | + $this->migration_log->upsert_migration($migration->get_schema()); |
|
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | 135 | |
@@ -143,23 +143,23 @@ discard block |
||
| 143 | 143 | * @param string ...$excluded_table Table names to exclude. |
| 144 | 144 | * @return self |
| 145 | 145 | */ |
| 146 | - public function seed_tables( string ...$excluded_table ): self { |
|
| 146 | + public function seed_tables(string ...$excluded_table): self { |
|
| 147 | 147 | |
| 148 | 148 | // Remove excluded tables. |
| 149 | 149 | $to_seed = array_filter( |
| 150 | 150 | $this->migrations, |
| 151 | - function( Database_Migration $migration ) use ( $excluded_table ): bool { |
|
| 152 | - return ! in_array( $migration->get_table_name(), $excluded_table, true ) |
|
| 153 | - && ! $this->migration_log->is_seeded( $migration->get_schema() ); |
|
| 151 | + function(Database_Migration $migration) use ($excluded_table): bool { |
|
| 152 | + return ! in_array($migration->get_table_name(), $excluded_table, true) |
|
| 153 | + && ! $this->migration_log->is_seeded($migration->get_schema()); |
|
| 154 | 154 | } |
| 155 | 155 | ); |
| 156 | 156 | |
| 157 | - $seeder = new Migration_Seeder( $this->wpdb ); |
|
| 157 | + $seeder = new Migration_Seeder($this->wpdb); |
|
| 158 | 158 | |
| 159 | - foreach ( $to_seed as $migration ) { |
|
| 160 | - $row = $seeder->seed( $migration->get_schema(), $migration->get_seeds() ); |
|
| 161 | - if ( count( $row ) !== 0 ) { |
|
| 162 | - $this->migration_log->mark_table_seeded( $migration->get_schema() ); |
|
| 159 | + foreach ($to_seed as $migration) { |
|
| 160 | + $row = $seeder->seed($migration->get_schema(), $migration->get_seeds()); |
|
| 161 | + if (count($row) !== 0) { |
|
| 162 | + $this->migration_log->mark_table_seeded($migration->get_schema()); |
|
| 163 | 163 | } |
| 164 | 164 | } |
| 165 | 165 | |
@@ -172,26 +172,26 @@ discard block |
||
| 172 | 172 | * @param string ...$excluded_table Table names to exclude. |
| 173 | 173 | * @return self |
| 174 | 174 | */ |
| 175 | - public function drop_tables( string ...$excluded_table ): self { |
|
| 175 | + public function drop_tables(string ...$excluded_table): self { |
|
| 176 | 176 | // Remove excluded tables. |
| 177 | 177 | $to_seed = array_filter( |
| 178 | 178 | $this->migrations, |
| 179 | - function( Database_Migration $migration ) use ( $excluded_table ): bool { |
|
| 180 | - return ! in_array( $migration->get_table_name(), $excluded_table, true ); |
|
| 179 | + function(Database_Migration $migration) use ($excluded_table): bool { |
|
| 180 | + return ! in_array($migration->get_table_name(), $excluded_table, true); |
|
| 181 | 181 | } |
| 182 | 182 | ); |
| 183 | 183 | |
| 184 | - foreach ( $to_seed as $migration ) { |
|
| 184 | + foreach ($to_seed as $migration) { |
|
| 185 | 185 | |
| 186 | - $result = $this->builder->drop_table( $migration->get_schema() ); |
|
| 186 | + $result = $this->builder->drop_table($migration->get_schema()); |
|
| 187 | 187 | |
| 188 | 188 | // Throw exception if fails. |
| 189 | - if ( $result === false ) { |
|
| 190 | - throw Migration_Exception::failed_to_drop_table( $migration->get_table_name() ); |
|
| 189 | + if ($result === false) { |
|
| 190 | + throw Migration_Exception::failed_to_drop_table($migration->get_table_name()); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | // Remove migration from log. |
| 194 | - $this->migration_log->remove_migration( $migration->get_schema() ); |
|
| 194 | + $this->migration_log->remove_migration($migration->get_schema()); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | return $this; |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | */ |
| 46 | 46 | protected $migration_details = array(); |
| 47 | 47 | |
| 48 | - public function __construct( string $option_key = null ) { |
|
| 48 | + public function __construct(string $option_key = null) { |
|
| 49 | 49 | $this->option_key = $option_key ?? 'pinkcrab_migration_log'; |
| 50 | 50 | $this->set_migration_details(); |
| 51 | 51 | } |
@@ -57,19 +57,19 @@ discard block |
||
| 57 | 57 | * @throws InvalidArgumentException If can not unserialize |
| 58 | 58 | */ |
| 59 | 59 | protected function set_migration_details(): void { |
| 60 | - $migrations = get_option( $this->option_key, null ); |
|
| 60 | + $migrations = get_option($this->option_key, null); |
|
| 61 | 61 | |
| 62 | - if ( $migrations === null ) { |
|
| 62 | + if ($migrations === null) { |
|
| 63 | 63 | return; |
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Handle errors as exceptions. |
| 67 | 67 | set_error_handler( // phpcs:ignore |
| 68 | - function ( int $errno, string $errstr ) { // phpcs:ignore |
|
| 69 | - throw new InvalidArgumentException( 'Migration details as unserialize from options, failed to be decoded.' ); |
|
| 68 | + function(int $errno, string $errstr) { // phpcs:ignore |
|
| 69 | + throw new InvalidArgumentException('Migration details as unserialize from options, failed to be decoded.'); |
|
| 70 | 70 | } |
| 71 | 71 | ); |
| 72 | - $migrations = \unserialize( $migrations ); // phpcs:ignore |
|
| 72 | + $migrations = \unserialize($migrations); // phpcs:ignore |
|
| 73 | 73 | restore_error_handler(); |
| 74 | 74 | |
| 75 | 75 | $this->migration_details = $migrations; |
@@ -81,8 +81,8 @@ discard block |
||
| 81 | 81 | * @param \PinkCrab\Table_Builder\Schema $schema |
| 82 | 82 | * @return boolean |
| 83 | 83 | */ |
| 84 | - public function has_migration( Schema $schema ): bool { |
|
| 85 | - return array_key_exists( $schema->get_table_name(), $this->migration_details ); |
|
| 84 | + public function has_migration(Schema $schema): bool { |
|
| 85 | + return array_key_exists($schema->get_table_name(), $this->migration_details); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | /** |
@@ -90,8 +90,8 @@ discard block |
||
| 90 | 90 | * |
| 91 | 91 | * @return \PinkCrab\DB_Migration\Log\Migration_Log|null |
| 92 | 92 | */ |
| 93 | - public function get_migration( Schema $schema ): ?Migration_Log { |
|
| 94 | - return $this->has_migration( $schema ) |
|
| 93 | + public function get_migration(Schema $schema): ?Migration_Log { |
|
| 94 | + return $this->has_migration($schema) |
|
| 95 | 95 | ? $this->migration_details[ $schema->get_table_name() ] |
| 96 | 96 | : null; |
| 97 | 97 | } |
@@ -102,8 +102,8 @@ discard block |
||
| 102 | 102 | * @param \PinkCrab\Table_Builder\Schema $schema |
| 103 | 103 | * @return bool |
| 104 | 104 | */ |
| 105 | - public function can_migrate( Schema $schema ): bool { |
|
| 106 | - return ! $this->check_hash( $schema ); |
|
| 105 | + public function can_migrate(Schema $schema): bool { |
|
| 106 | + return ! $this->check_hash($schema); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
@@ -112,8 +112,8 @@ discard block |
||
| 112 | 112 | * @param \PinkCrab\Table_Builder\Schema $schema |
| 113 | 113 | * @return bool |
| 114 | 114 | */ |
| 115 | - public function is_seeded( Schema $schema ): bool { |
|
| 116 | - return $this->has_migration( $schema ) |
|
| 115 | + public function is_seeded(Schema $schema): bool { |
|
| 116 | + return $this->has_migration($schema) |
|
| 117 | 117 | && $this->migration_details[ $schema->get_table_name() ]->is_seeded(); |
| 118 | 118 | } |
| 119 | 119 | |
@@ -123,15 +123,15 @@ discard block |
||
| 123 | 123 | * @param \PinkCrab\Table_Builder\Schema $schema |
| 124 | 124 | * @return bool |
| 125 | 125 | */ |
| 126 | - private function check_hash( Schema $schema ): bool { |
|
| 126 | + private function check_hash(Schema $schema): bool { |
|
| 127 | 127 | // If table doesnt exist, return false; |
| 128 | - if ( ! $this->has_migration( $schema ) ) { |
|
| 128 | + if ( ! $this->has_migration($schema)) { |
|
| 129 | 129 | return false; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - $schema_hash = Migration_Log::compose_column_hash( $schema ); |
|
| 132 | + $schema_hash = Migration_Log::compose_column_hash($schema); |
|
| 133 | 133 | $migration = $this->migration_details[ $schema->get_table_name() ]; |
| 134 | - return strcmp( $schema_hash, $migration->schema_hash() ) === 0; |
|
| 134 | + return strcmp($schema_hash, $migration->schema_hash()) === 0; |
|
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | /** |
@@ -145,20 +145,20 @@ discard block |
||
| 145 | 145 | * @param \PinkCrab\Table_Builder\Schema $schema |
| 146 | 146 | * @return self |
| 147 | 147 | */ |
| 148 | - public function upsert_migration( Schema $schema ): self { |
|
| 148 | + public function upsert_migration(Schema $schema): self { |
|
| 149 | 149 | // Update if table exists and we have a new schema defined. |
| 150 | - if ( $this->has_migration( $schema ) |
|
| 151 | - && ! $this->check_hash( $schema ) ) { |
|
| 150 | + if ($this->has_migration($schema) |
|
| 151 | + && ! $this->check_hash($schema)) { |
|
| 152 | 152 | $this->migration_details[ $schema->get_table_name() ] = |
| 153 | - $this->migration_details[ $schema->get_table_name() ]->as_updated( $schema ); |
|
| 153 | + $this->migration_details[ $schema->get_table_name() ]->as_updated($schema); |
|
| 154 | 154 | |
| 155 | 155 | $this->save(); |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | // If a new hash. |
| 159 | - if ( $this->has_migration( $schema ) === false ) { |
|
| 159 | + if ($this->has_migration($schema) === false) { |
|
| 160 | 160 | $this->migration_details[ $schema->get_table_name() ] = |
| 161 | - Migration_Log::new_from_schema( $schema ); |
|
| 161 | + Migration_Log::new_from_schema($schema); |
|
| 162 | 162 | |
| 163 | 163 | $this->save(); |
| 164 | 164 | } |
@@ -172,9 +172,9 @@ discard block |
||
| 172 | 172 | * @param \PinkCrab\Table_Builder\Schema $schema |
| 173 | 173 | * @return self |
| 174 | 174 | */ |
| 175 | - public function remove_migration( Schema $schema ): self { |
|
| 176 | - if ( $this->has_migration( $schema ) ) { |
|
| 177 | - unset( $this->migration_details[ $schema->get_table_name() ] ); |
|
| 175 | + public function remove_migration(Schema $schema): self { |
|
| 176 | + if ($this->has_migration($schema)) { |
|
| 177 | + unset($this->migration_details[ $schema->get_table_name() ]); |
|
| 178 | 178 | |
| 179 | 179 | $this->save(); |
| 180 | 180 | } |
@@ -187,8 +187,8 @@ discard block |
||
| 187 | 187 | * @param \PinkCrab\Table_Builder\Schema $schema |
| 188 | 188 | * @return self |
| 189 | 189 | */ |
| 190 | - public function mark_table_seeded( Schema $schema ): self { |
|
| 191 | - if ( ! $this->is_seeded( $schema ) ) { |
|
| 190 | + public function mark_table_seeded(Schema $schema): self { |
|
| 191 | + if ( ! $this->is_seeded($schema)) { |
|
| 192 | 192 | $this->migration_details[ $schema->get_table_name() ] = |
| 193 | 193 | $this->migration_details[ $schema->get_table_name() ]->as_seeded(); |
| 194 | 194 | |
@@ -204,7 +204,7 @@ discard block |
||
| 204 | 204 | * @return void |
| 205 | 205 | */ |
| 206 | 206 | protected function save(): void { |
| 207 | - \update_option( $this->option_key, serialize( $this->migration_details ), false ); // phpcs:ignore |
|
| 207 | + \update_option($this->option_key, serialize($this->migration_details), false); // phpcs:ignore |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | /** |
@@ -222,6 +222,6 @@ discard block |
||
| 222 | 222 | * @return void |
| 223 | 223 | */ |
| 224 | 224 | public function clear_log(): void { |
| 225 | - \delete_option( $this->option_key ); |
|
| 225 | + \delete_option($this->option_key); |
|
| 226 | 226 | } |
| 227 | 227 | } |