@@ -60,10 +60,10 @@ discard block |
||
60 | 60 | */ |
61 | 61 | protected $migration_log; |
62 | 62 | |
63 | - public function __construct( Builder $builder, wpdb $wpdb, ?string $migration_log_key = null ) { |
|
63 | + public function __construct(Builder $builder, wpdb $wpdb, ?string $migration_log_key = null) { |
|
64 | 64 | $this->builder = $builder; |
65 | 65 | $this->wpdb = $wpdb; |
66 | - $this->migration_log = new Migration_Log_Manager( $migration_log_key ); |
|
66 | + $this->migration_log = new Migration_Log_Manager($migration_log_key); |
|
67 | 67 | |
68 | 68 | } |
69 | 69 | |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | * @param \PinkCrab\DB_Migration\Database_Migration $migration |
97 | 97 | * @return self |
98 | 98 | */ |
99 | - public function add_migration( Database_Migration $migration ): self { |
|
99 | + public function add_migration(Database_Migration $migration): self { |
|
100 | 100 | $this->migrations[ $migration->get_table_name() ] = $migration; |
101 | 101 | return $this; |
102 | 102 | } |
@@ -116,22 +116,22 @@ discard block |
||
116 | 116 | * @param string ...$excluded_table Table names to exclude. |
117 | 117 | * @return self |
118 | 118 | */ |
119 | - public function create_tables( string ...$excluded_table ): self { |
|
119 | + public function create_tables(string ...$excluded_table): self { |
|
120 | 120 | |
121 | 121 | // Remove excluded tables. |
122 | 122 | $to_create = array_filter( |
123 | 123 | $this->migrations, |
124 | - function( Database_Migration $migration ) use ( $excluded_table ): bool { |
|
125 | - return ! in_array( $migration->get_table_name(), $excluded_table, true ) |
|
126 | - && $this->migration_log->can_migrate( $migration->get_schema() ); |
|
124 | + function(Database_Migration $migration) use ($excluded_table): bool { |
|
125 | + return ! in_array($migration->get_table_name(), $excluded_table, true) |
|
126 | + && $this->migration_log->can_migrate($migration->get_schema()); |
|
127 | 127 | } |
128 | 128 | ); |
129 | 129 | |
130 | 130 | // Upsert Tables. |
131 | - foreach ( $to_create as $migration ) { |
|
132 | - $result = $this->builder->create_table( $migration->get_schema() ); |
|
133 | - if ( $result === true ) { |
|
134 | - $this->migration_log->upsert_migration( $migration->get_schema() ); |
|
131 | + foreach ($to_create as $migration) { |
|
132 | + $result = $this->builder->create_table($migration->get_schema()); |
|
133 | + if ($result === true) { |
|
134 | + $this->migration_log->upsert_migration($migration->get_schema()); |
|
135 | 135 | } |
136 | 136 | } |
137 | 137 | |
@@ -145,23 +145,23 @@ discard block |
||
145 | 145 | * @param string ...$excluded_table Table names to exclude. |
146 | 146 | * @return self |
147 | 147 | */ |
148 | - public function seed_tables( string ...$excluded_table ): self { |
|
148 | + public function seed_tables(string ...$excluded_table): self { |
|
149 | 149 | |
150 | 150 | // Remove excluded tables. |
151 | 151 | $to_seed = array_filter( |
152 | 152 | $this->migrations, |
153 | - function( Database_Migration $migration ) use ( $excluded_table ): bool { |
|
154 | - return ! in_array( $migration->get_table_name(), $excluded_table, true ) |
|
155 | - && ! $this->migration_log->is_seeded( $migration->get_schema() ); |
|
153 | + function(Database_Migration $migration) use ($excluded_table): bool { |
|
154 | + return ! in_array($migration->get_table_name(), $excluded_table, true) |
|
155 | + && ! $this->migration_log->is_seeded($migration->get_schema()); |
|
156 | 156 | } |
157 | 157 | ); |
158 | 158 | |
159 | - $seeder = new Migration_Seeder( $this->wpdb ); |
|
159 | + $seeder = new Migration_Seeder($this->wpdb); |
|
160 | 160 | |
161 | - foreach ( $to_seed as $migration ) { |
|
162 | - $row = $seeder->seed( $migration->get_schema(), $migration->get_seeds() ); |
|
163 | - if ( count( $row ) !== 0 ) { |
|
164 | - $this->migration_log->mark_table_seeded( $migration->get_schema() ); |
|
161 | + foreach ($to_seed as $migration) { |
|
162 | + $row = $seeder->seed($migration->get_schema(), $migration->get_seeds()); |
|
163 | + if (count($row) !== 0) { |
|
164 | + $this->migration_log->mark_table_seeded($migration->get_schema()); |
|
165 | 165 | } |
166 | 166 | } |
167 | 167 | |
@@ -174,30 +174,30 @@ discard block |
||
174 | 174 | * @param string ...$excluded_table Table names to exclude. |
175 | 175 | * @return self |
176 | 176 | */ |
177 | - public function drop_tables( string ...$excluded_table ): self { |
|
177 | + public function drop_tables(string ...$excluded_table): self { |
|
178 | 178 | // Remove excluded tables. |
179 | 179 | $to_seed = array_filter( |
180 | 180 | $this->migrations, |
181 | - function( Database_Migration $migration ) use ( $excluded_table ): bool { |
|
182 | - return ! in_array( $migration->get_table_name(), $excluded_table, true ); |
|
181 | + function(Database_Migration $migration) use ($excluded_table): bool { |
|
182 | + return ! in_array($migration->get_table_name(), $excluded_table, true); |
|
183 | 183 | } |
184 | 184 | ); |
185 | 185 | |
186 | - foreach ( $to_seed as $migration ) { |
|
186 | + foreach ($to_seed as $migration) { |
|
187 | 187 | |
188 | 188 | try { |
189 | - $result = $this->builder->drop_table( $migration->get_schema() ); |
|
190 | - } catch ( Engine_Exception $th ) { |
|
191 | - throw Migration_Exception::failed_to_drop_table( $migration->get_schema(), $th->getMessage() ); |
|
189 | + $result = $this->builder->drop_table($migration->get_schema()); |
|
190 | + } catch (Engine_Exception $th) { |
|
191 | + throw Migration_Exception::failed_to_drop_table($migration->get_schema(), $th->getMessage()); |
|
192 | 192 | } |
193 | 193 | |
194 | 194 | // Throw exception if fails. |
195 | - if ( $result === false ) { |
|
196 | - throw Migration_Exception::failed_to_drop_table( $migration->get_schema(), '' ); |
|
195 | + if ($result === false) { |
|
196 | + throw Migration_Exception::failed_to_drop_table($migration->get_schema(), ''); |
|
197 | 197 | } |
198 | 198 | |
199 | 199 | // Remove migration from log. |
200 | - $this->migration_log->remove_migration( $migration->get_schema() ); |
|
200 | + $this->migration_log->remove_migration($migration->get_schema()); |
|
201 | 201 | } |
202 | 202 | |
203 | 203 | return $this; |
@@ -36,7 +36,7 @@ discard block |
||
36 | 36 | */ |
37 | 37 | protected $wpdb; |
38 | 38 | |
39 | - public function __construct( wpdb $wpdb ) { |
|
39 | + public function __construct(wpdb $wpdb) { |
|
40 | 40 | $this->wpdb = $wpdb; |
41 | 41 | } |
42 | 42 | |
@@ -47,11 +47,11 @@ discard block |
||
47 | 47 | * @param array<array<string, mixed>> $seed_data |
48 | 48 | * @return array<string, int> |
49 | 49 | */ |
50 | - public function seed( Schema $schema, array $seed_data ): array { |
|
50 | + public function seed(Schema $schema, array $seed_data): array { |
|
51 | 51 | $results = array(); |
52 | 52 | |
53 | - foreach ( $seed_data as $key => $seed ) { |
|
54 | - $results[ $key ] = $this->insert_seed( $schema, $seed ); |
|
53 | + foreach ($seed_data as $key => $seed) { |
|
54 | + $results[ $key ] = $this->insert_seed($schema, $seed); |
|
55 | 55 | } |
56 | 56 | |
57 | 57 | return $results; |
@@ -65,13 +65,13 @@ discard block |
||
65 | 65 | * @return int The new row ID. |
66 | 66 | * @throws Migration_Exception |
67 | 67 | */ |
68 | - protected function insert_seed( Schema $schema, array $seed ): int { |
|
68 | + protected function insert_seed(Schema $schema, array $seed): int { |
|
69 | 69 | // Get format for each column based on the type. |
70 | 70 | $format = array_map( |
71 | - function( string $column_name ) use ( $schema ): string { |
|
72 | - return $this->column_type( $schema, $column_name ); |
|
71 | + function(string $column_name) use ($schema): string { |
|
72 | + return $this->column_type($schema, $column_name); |
|
73 | 73 | }, |
74 | - array_keys( $seed ) |
|
74 | + array_keys($seed) |
|
75 | 75 | ); |
76 | 76 | |
77 | 77 | $this->wpdb->insert( |
@@ -81,8 +81,8 @@ discard block |
||
81 | 81 | ); |
82 | 82 | |
83 | 83 | // Check any errors inserting. |
84 | - if ( $this->wpdb->last_error !== '' ) { |
|
85 | - throw Migration_Exception::failed_to_insert_seed( $schema, $this->wpdb->last_error ); |
|
84 | + if ($this->wpdb->last_error !== '') { |
|
85 | + throw Migration_Exception::failed_to_insert_seed($schema, $this->wpdb->last_error); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | return $this->wpdb->insert_id; |
@@ -96,15 +96,15 @@ discard block |
||
96 | 96 | * @return string |
97 | 97 | * @throws Migration_Exception If a column cant be found. |
98 | 98 | */ |
99 | - protected function column_type( Schema $schema, string $column ): string { |
|
99 | + protected function column_type(Schema $schema, string $column): string { |
|
100 | 100 | $schema_columns = $schema->get_columns(); |
101 | 101 | |
102 | 102 | // If colum doesnt exist, thorw exception. |
103 | - if ( ! array_key_exists( $column, $schema_columns ) ) { |
|
104 | - throw Migration_Exception::seed_column_doesnt_exist( $schema, $column ); |
|
103 | + if ( ! array_key_exists($column, $schema_columns)) { |
|
104 | + throw Migration_Exception::seed_column_doesnt_exist($schema, $column); |
|
105 | 105 | } |
106 | 106 | |
107 | - return $this->column_type_format( $schema_columns[ $column ]->get_type() ?? '' ); |
|
107 | + return $this->column_type_format($schema_columns[ $column ]->get_type() ?? ''); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | /** |
@@ -113,8 +113,8 @@ discard block |
||
113 | 113 | * @param string $type |
114 | 114 | * @return string |
115 | 115 | */ |
116 | - protected function column_type_format( string $type ): string { |
|
117 | - switch ( \strtoupper( $type ) ) { |
|
116 | + protected function column_type_format(string $type): string { |
|
117 | + switch (\strtoupper($type)) { |
|
118 | 118 | case 'CHAR': |
119 | 119 | case 'VARCHAR': |
120 | 120 | case 'BINARY': |
@@ -53,8 +53,8 @@ discard block |
||
53 | 53 | * @param int $code |
54 | 54 | * @param Throwable$previous |
55 | 55 | */ |
56 | - public function __construct( Schema $schema, string $wpdb_error = '', $message = '', $code = 0, Throwable $previous = null ) { |
|
57 | - parent::__construct( $message, $code, $previous ); |
|
56 | + public function __construct(Schema $schema, string $wpdb_error = '', $message = '', $code = 0, Throwable $previous = null) { |
|
57 | + parent::__construct($message, $code, $previous); |
|
58 | 58 | $this->schema = $schema; |
59 | 59 | $this->wpdb_error = $wpdb_error; |
60 | 60 | } |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | * @return Migration_Exception |
69 | 69 | * @code 1 |
70 | 70 | */ |
71 | - public static function seed_column_doesnt_exist( Schema $schema, string $column ): Migration_Exception { |
|
71 | + public static function seed_column_doesnt_exist(Schema $schema, string $column): Migration_Exception { |
|
72 | 72 | return new Migration_Exception( |
73 | 73 | $schema, |
74 | 74 | '', |
@@ -89,11 +89,11 @@ discard block |
||
89 | 89 | * @return Migration_Exception |
90 | 90 | * @code 2 |
91 | 91 | */ |
92 | - public static function failed_to_insert_seed( Schema $schema, string $wpdb_error ): Migration_Exception { |
|
92 | + public static function failed_to_insert_seed(Schema $schema, string $wpdb_error): Migration_Exception { |
|
93 | 93 | return new Migration_Exception( |
94 | 94 | $schema, |
95 | 95 | $wpdb_error, |
96 | - \sprintf( 'Could not insert seed into %s, failed with error: %s', $schema->get_table_name(), $wpdb_error ), |
|
96 | + \sprintf('Could not insert seed into %s, failed with error: %s', $schema->get_table_name(), $wpdb_error), |
|
97 | 97 | 2 |
98 | 98 | ); |
99 | 99 | } |
@@ -106,11 +106,11 @@ discard block |
||
106 | 106 | * @return Migration_Exception |
107 | 107 | * @code 3 |
108 | 108 | */ |
109 | - public static function failed_to_drop_table( Schema $schema, string $wpdb_error ): Migration_Exception { |
|
109 | + public static function failed_to_drop_table(Schema $schema, string $wpdb_error): Migration_Exception { |
|
110 | 110 | return new Migration_Exception( |
111 | 111 | $schema, |
112 | 112 | $wpdb_error, |
113 | - \sprintf( 'Failed to drop %s', $schema->get_table_name() ), |
|
113 | + \sprintf('Failed to drop %s', $schema->get_table_name()), |
|
114 | 114 | 3 |
115 | 115 | ); |
116 | 116 | } |