summaryrefslogtreecommitdiffstats
path: root/application/migrations/014_deduplicate_file_storage.php
blob: 8f8f40430571f6f6ebcb7d409c0ce8565e0f9785 (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
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Migration_deduplicate_file_storage extends CI_Migration {

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

		// FIXME: use prefix

		if ($this->db->dbdriver == 'postgre') {
			throw new \exceptions\ApiException("migration/postgres/not-implemented", "migration 14 not implemented yet for postgres");
		} else {
			$this->db->query('
				CREATE TABLE `file_storage` (
					`id` int(11) NOT NULL AUTO_INCREMENT,
					`filesize` int(11) NOT NULL,
					`mimetype` varchar(255) NOT NULL,
					`hash` char(32) NOT NULL,
					`hash_collision_counter` int(11) NOT NULL,
					PRIMARY KEY (`id`),
					UNIQUE KEY `data_id` (`hash`, `hash_collision_counter`)
				) ENGINE=InnoDB DEFAULT CHARSET=utf8;
			');
			$this->db->query('
				ALTER TABLE `files`
				ADD `file_storage_id` INT NOT NULL,
				ADD INDEX (`file_storage_id`),
				ADD FOREIGN KEY (`file_storage_id`) REFERENCES `filebin_test`.`file_storage`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
			');

			$this->db->query('
				INSERT INTO file_storage (storage-id, filesize, mimetype)
				SELECT hash, filesize, mimetype FROM files;
			');

			$this->db->query('
				UPDATE files f
				JOIN file_storage fs ON fs.data_id = f.hash
				SET f.file_storage_id = fs.id
			');

			$this->dbforge->drop_column("files", array("hash", "mimetype", "filesize"));
		}
	}

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