• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / 5066322038

pending completion
5066322038

push

github

GitHub
Merge pull request #20316 from Yoast/JRF/ghactions-run-more-selectively

2550 of 29012 relevant lines covered (8.79%)

0.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/initializers/migration-runner.php
1
<?php
2

3
namespace Yoast\WP\SEO\Initializers;
4

5
use Exception;
6
use Yoast\WP\Lib\Migrations\Adapter;
7
use Yoast\WP\Lib\Migrations\Migration;
8
use Yoast\WP\SEO\Conditionals\No_Conditionals;
9
use Yoast\WP\SEO\Config\Migration_Status;
10
use Yoast\WP\SEO\Loader;
11

12
/**
13
 * Triggers database migrations and handles results.
14
 */
15
class Migration_Runner implements Initializer_Interface {
16

17
        use No_Conditionals;
18

19
        /**
20
         * The migrations adapter.
21
         *
22
         * @var Adapter
23
         */
24
        protected $adapter;
25

26
        /**
27
         * The loader.
28
         *
29
         * @var Loader
30
         */
31
        protected $loader;
32

33
        /**
34
         * The migration status.
35
         *
36
         * @var Migration_Status
37
         */
38
        protected $migration_status;
39

40
        /**
41
         * Retrieves the conditionals for the migrations.
42
         *
43
         * @return array The conditionals.
44
         */
45
        public static function get_conditionals() {
46
                return [];
×
47
        }
48

49
        /**
50
         * Migrations constructor.
51
         *
52
         * @param Migration_Status $migration_status The migration status.
53
         * @param Loader           $loader           The loader.
54
         * @param Adapter          $adapter          The migrations adapter.
55
         */
56
        public function __construct(
57
                Migration_Status $migration_status,
58
                Loader $loader,
59
                Adapter $adapter
60
        ) {
61
                $this->migration_status = $migration_status;
×
62
                $this->loader           = $loader;
×
63
                $this->adapter          = $adapter;
×
64
        }
65

66
        /**
67
         * Runs this initializer.
68
         *
69
         * @return void
70
         *
71
         * @throws Exception When a migration errored.
72
         */
73
        public function initialize() {
74
                $this->run_free_migrations();
×
75
                // The below actions is used for when queries fail, this may happen in a multisite environment when switch_to_blog is used.
76
                \add_action( '_yoast_run_migrations', [ $this, 'run_free_migrations' ] );
×
77
        }
78

79
        /**
80
         * Runs the free migrations.
81
         *
82
         * @return void
83
         *
84
         * @throws Exception When a migration errored.
85
         */
86
        public function run_free_migrations() {
87
                $this->run_migrations( 'free' );
×
88
        }
89

90
        /**
91
         * Initializes the migrations.
92
         *
93
         * @param string $name    The name of the migration.
94
         * @param string $version The current version.
95
         *
96
         * @return bool True on success, false on failure.
97
         *
98
         * @throws Exception If the migration fails and YOAST_ENVIRONMENT is not production.
99
         */
100
        public function run_migrations( $name, $version = \WPSEO_VERSION ) {
101
                if ( ! $this->migration_status->should_run_migration( $name, $version ) ) {
×
102
                        return true;
×
103
                }
104

105
                if ( ! $this->migration_status->lock_migration( $name ) ) {
×
106
                        return false;
×
107
                }
108

109
                $migrations = $this->loader->get_migrations( $name );
×
110

111
                if ( $migrations === false ) {
×
112
                        $this->migration_status->set_error( $name, "Could not perform $name migrations. No migrations found.", $version );
×
113
                        return false;
×
114
                }
115

116
                try {
117
                        $this->adapter->create_schema_version_table();
×
118
                        $all_versions      = \array_keys( $migrations );
×
119
                        $migrated_versions = $this->adapter->get_migrated_versions();
×
120
                        $to_do_versions    = \array_diff( $all_versions, $migrated_versions );
×
121

122
                        \sort( $to_do_versions, \SORT_STRING );
×
123

124
                        foreach ( $to_do_versions as $to_do_version ) {
×
125
                                $class = $migrations[ $to_do_version ];
×
126
                                $this->run_migration( $to_do_version, $class );
×
127
                        }
128
                } catch ( Exception $exception ) {
×
129
                        // Something went wrong...
130
                        $this->migration_status->set_error( $name, $exception->getMessage(), $version );
×
131

132
                        if ( \defined( 'YOAST_ENVIRONMENT' ) && \YOAST_ENVIRONMENT !== 'production' ) {
×
133
                                throw $exception;
×
134
                        }
135

136
                        return false;
×
137
                }
138

139
                $this->migration_status->set_success( $name, $version );
×
140

141
                return true;
×
142
        }
143

144
        /**
145
         * Runs a single migration.
146
         *
147
         * @param string $version         The version.
148
         * @param string $migration_class The migration class.
149
         *
150
         * @return void
151
         *
152
         * @throws Exception If the migration failed. Caught by the run_migrations function.
153
         */
154
        protected function run_migration( $version, $migration_class ) {
155
                /**
156
                 * The migration to run.
157
                 *
158
                 * @var Migration
159
                 */
160
                $migration = new $migration_class( $this->adapter );
×
161
                try {
162
                        $this->adapter->start_transaction();
×
163
                        $migration->up();
×
164
                        $this->adapter->add_version( $version );
×
165
                        $this->adapter->commit_transaction();
×
166
                } catch ( Exception $e ) {
×
167
                        $this->adapter->rollback_transaction();
×
168
                        throw new Exception( \sprintf( '%s - %s', $migration_class, $e->getMessage() ), 0, $e );
×
169
                }
170
        }
171
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc