PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.19
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.19
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / database / Migrations / Migrator.php

Migrator.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.19, at database/Migrations/Migrator.php

198 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Database\Migrations;
4
5 use FluentCart\Framework\Database\Schema;
6
7 abstract class Migrator
8 {
9 public static string $tableName = '';
10
11 /**
12 * Migrate the table.
13 *
14 * @return void
15 */
16
17 public static function migrate()
18 {
19 Schema::createTableIfNotExist(
20 static::getTableName(),
21 static::getSqlSchema()
22 );
23
24 static::migrated();
25 }
26
27 /**
28 * Called once migration is done.
29 *
30 * Override this method in child migrators to perform
31 * post-migration tasks like seeding data or adding indexes.
32 *
33 * @return void
34 */
35 public static function migrated()
36 {
37 // Override in child classes as needed.
38 }
39
40 /**
41 * Add a column if it does not exist.
42 *
43 * @param string $column Column name
44 * @param string $definition Column definition (e.g. "VARCHAR(100) NULL")
45 * @param string|null $after Place after this column (ignored on SQLite)
46 * @return void
47 */
48 protected static function addColumnIfNotExists($column, $definition, $after = null)
49 {
50 if (Schema::hasColumn($column, static::$tableName)) {
51 return;
52 }
53
54 $sql = "ADD COLUMN `{$column}` {$definition}";
55
56 if ($after && !Schema::isSqlite()) {
57 $sql .= " AFTER `{$after}`";
58 }
59
60 Schema::alterTable(static::$tableName, $sql);
61 }
62
63 /**
64 * Rename a column if the old name exists.
65 *
66 * @param string $oldColumn Current column name
67 * @param string $newColumn New column name
68 * @param string $definition New column definition
69 * @return void
70 */
71 protected static function renameColumnIfExists($oldColumn, $newColumn, $definition)
72 {
73 if (!Schema::hasColumn($oldColumn, static::$tableName)) {
74 return;
75 }
76
77 Schema::alterTable(
78 static::$tableName,
79 "CHANGE `{$oldColumn}` `{$newColumn}` {$definition}"
80 );
81 }
82
83 /**
84 * Modify a column type/definition if it exists.
85 *
86 * @param string $column Column name
87 * @param string $definition New column definition
88 * @return void
89 */
90 protected static function modifyColumnIfExists($column, $definition)
91 {
92 if (!Schema::hasColumn($column, static::$tableName)) {
93 return;
94 }
95
96 if (Schema::isSqlite()) {
97 // SQLite has no MODIFY; use CHANGE with same name to trigger rebuild
98 Schema::alterTable(
99 static::$tableName,
100 "CHANGE COLUMN `{$column}` `{$column}` {$definition}"
101 );
102 } else {
103 Schema::alterTable(
104 static::$tableName,
105 "MODIFY COLUMN `{$column}` {$definition}"
106 );
107 }
108 }
109
110 /**
111 * Check if an index exists on the table.
112 *
113 * @param string $indexName Index name
114 * @return bool
115 */
116 protected static function hasIndex($indexName)
117 {
118 $wpdb = Schema::db();
119 $tableName = static::getTableName();
120
121 // SHOW INDEX is translated by the WP SQLite integration plugin
122 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
123 $results = $wpdb->get_results($wpdb->prepare(
124 "SHOW INDEX FROM %i WHERE Key_name = %s",
125 $tableName,
126 $indexName
127 ));
128
129 return !empty($results);
130 }
131
132 /**
133 * Add an index if it does not exist.
134 *
135 * @param string $indexName Index name
136 * @param string|array $columns Column name(s)
137 * @param bool $unique Whether the index is unique
138 * @return void
139 */
140 protected static function addIndexIfNotExists($indexName, $columns, $unique = false)
141 {
142 if (static::hasIndex($indexName)) {
143 return;
144 }
145
146 $type = $unique ? 'UNIQUE INDEX' : 'INDEX';
147
148 if (is_array($columns)) {
149 $colsSql = '`' . implode('`, `', $columns) . '`';
150 } else {
151 $colsSql = "`{$columns}`";
152 }
153
154 Schema::alterTable(
155 static::$tableName,
156 "ADD {$type} `{$indexName}` ({$colsSql})"
157 );
158 }
159
160 /**
161 * Drop an index if it exists.
162 *
163 * @param string $indexName Index name
164 * @return void
165 */
166 protected static function dropIndexIfExists($indexName)
167 {
168 if (!static::hasIndex($indexName)) {
169 return;
170 }
171
172 Schema::dropIndex(static::$tableName, $indexName);
173 }
174
175 public static function getTableName(bool $withPrefix = true): string
176 {
177 return ($withPrefix ? static::getDbPrefix() : '') . static::$tableName;
178 }
179
180 public static function getDbPrefix(): string
181 {
182 global $wpdb;
183 return $wpdb->prefix;
184 }
185
186 public static function getCharsetCollate(): string
187 {
188 global $wpdb;
189 return $wpdb->get_charset_collate();
190 }
191
192 public static function dropTable()
193 {
194 Schema::dropTableIfExists(static::getTableName(false));
195 }
196
197 abstract public static function getSqlSchema(): string;
198 }