Skip to content

Commit 260f058

Browse files
author
Gabriel Simmer
committed
remove delete when trimming databases and use select into instead.
1 parent 27e1c3b commit 260f058

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/DatabaseJanitor.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -139,20 +139,21 @@ public function trim() {
139139
// Rename table and copy is over.
140140
$this->connection->exec("ALTER TABLE " . $table . " RENAME TO original_" . $table);
141141
$ignore[] = 'original_' . $table;
142-
$this->connection->exec("CREATE TABLE " . $table . " SELECT * FROM original_" . $table);
143142
// This makes assumptions about the primary key, should be configurable.
144143
$primary_key = $this->connection->query("SHOW KEYS FROM original_" . $table . " WHERE Key_name = 'PRIMARY'")
145144
->fetch()['Column_name'];
146145
if ($primary_key) {
146+
$keep = array();
147147
$all = $this->connection->query("SELECT " . $primary_key . " FROM " . $table)
148148
->fetchAll();
149149
foreach ($all as $key => $row) {
150150
// Delete every other row.
151151
if ($key % 4 == 0) {
152-
continue;
152+
$keep[] = $row[$primary_key];
153153
}
154-
$this->connection->exec("DELETE FROM " . $table . " WHERE " . $primary_key . "=" . $row[$primary_key]);
155154
}
155+
$keep = implode(',', $keep);
156+
$this->connection->exec("CREATE TABLE " . $table . " SELECT * FROM original_" . $table . 'WHERE ' . $key . ' IN (' . $keep . ')');
156157
}
157158
}
158159
}

0 commit comments

Comments
 (0)