summaryrefslogtreecommitdiffstats
path: root/application/migrations/015_actually_deduplicate_file_storage.php
blob: 8f2a51c74d6780a7b1ba2627e4a8a5ba74010f43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_actually_deduplicate_file_storage extends CI_Migration {

	public function up()
	{
		$prefix = $this->db->dbprefix;

		if ($this->db->dbdriver == 'postgre') {
			// likely no need for this migration since 14 won't be buggy
			throw new \exceptions\ApiException("migration/postgres/not-implemented", "migration 15 not yet implemented for postgres");
		} else {
			// XXX: This query also exists in migration 14
			$this->db->query('
				DELETE file_storage
				FROM file_storage
				LEFT OUTER JOIN files ON files.file_storage_id = file_storage.id
				WHERE file_storage.id NOT IN (
					SELECT min(x.id)
					FROM (
						SELECT fs.id, fs.hash
						FROM file_storage fs
					) x
					GROUP BY x.hash
				)
				AND files.id IS NULL
			');

			$chunk = 500;
			$total = $this->db->count_all("file_storage");
			$consistent = true;

			for ($limit = 0; $limit < $total; $limit += $chunk) {
				$query = $this->db->select('hash, id')
					->from('file_storage')
					->limit($chunk, $limit)
					->get()->result_array();

				foreach ($query as $key => $item) {
					$data_id = $item["hash"].'-'.$item["id"];
					$file = $this->mfile->file($data_id);
					if (!file_exists($file)) {
						echo "Warning: no file found for $data_id\n";
						$consistent = false;
					}
				}
			}

			if (!$consistent) {
				echo "Your database is not consistent with your file system.\n";
				echo "Please report this as it is most likely a bug.\n";
			}
		}
	}

	public function down()
	{
		throw new \exceptions\ApiException("migration/downgrade-not-supported", "downgrade not supported");
	}
}