From 02ffa0c14f1c7401344eee4acde46c000f91bcc7 Mon Sep 17 00:00:00 2001 From: hashworks Date: Mon, 10 Jul 2017 20:12:03 +0200 Subject: Use bigint for filesize in database The current type, integer, only stores numerics up to 2147483647. Since filebin stores the size in byte MySQL will only write up to 2GB in there, PostgreSQL failes by default for files >2GB. The new type bigint allows file sizes up to ~9223 petabyte. --- application/config/migration.php | 2 +- .../migrations/019_change_filesize_type.php | 27 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 application/migrations/019_change_filesize_type.php diff --git a/application/config/migration.php b/application/config/migration.php index 659907cb8..45ce7711b 100644 --- a/application/config/migration.php +++ b/application/config/migration.php @@ -21,7 +21,7 @@ $config['migration_enabled'] = true; | be upgraded / downgraded to. | */ -$config['migration_version'] = 18; +$config['migration_version'] = 19; /* diff --git a/application/migrations/019_change_filesize_type.php b/application/migrations/019_change_filesize_type.php new file mode 100644 index 000000000..327d8dcae --- /dev/null +++ b/application/migrations/019_change_filesize_type.php @@ -0,0 +1,27 @@ +db->dbprefix; + + if ($this->db->dbdriver == 'postgre') { + $this->db->query(' + ALTER TABLE "'.$prefix.'file_storage" + ALTER "filesize" TYPE bigint; + '); + } else { + $this->db->query(' + ALTER TABLE `'.$prefix.'file_storage` + MODIFY `filesize` bigint; + '); + } + } + + public function down() + { + throw new \exceptions\ApiException("migration/downgrade-not-supported", "downgrade not supported"); + } +} -- cgit v1.2.3-24-g4f1b From cfcf6afd4f61769db72dd3927d1cea85c2603191 Mon Sep 17 00:00:00 2001 From: hashworks Date: Mon, 10 Jul 2017 21:00:41 +0200 Subject: Update filesizes after changing filesize db type This updates all wrong filesizes after db migration 19 (`filesize` type change to `bigint`). It will only update files with a filesize of 2147483647 byte since the database set the max integer value as the filesize if the file was > 2147483647 byte. --- .../migrations/019_change_filesize_type.php | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/application/migrations/019_change_filesize_type.php b/application/migrations/019_change_filesize_type.php index 327d8dcae..47a998452 100644 --- a/application/migrations/019_change_filesize_type.php +++ b/application/migrations/019_change_filesize_type.php @@ -18,6 +18,29 @@ class Migration_change_filesize_type extends CI_Migration { MODIFY `filesize` bigint; '); } + + $chunk = 500; + + $total = $this->db->count_all("file_storage"); + + for ($limit = 0; $limit < $total; $limit += $chunk) { + $query = $this->db->select('hash, id') + ->from('file_storage') + ->where('filesize', 2147483647) + ->limit($chunk, $limit) + ->get()->result_array(); + + foreach ($query as $key => $item) { + $data_id = $item["hash"].'-'.$item['id']; + $filesize = filesize($this->mfile->file($data_id)); + + $this->db->where('id', $item['id']) + ->set(array( + 'filesize' => $filesize, + )) + ->update('file_storage'); + } + } } public function down() -- cgit v1.2.3-24-g4f1b From 0a1e76f365dbd90eee9c72d81d898e1a0ddf630c Mon Sep 17 00:00:00 2001 From: hashworks Date: Mon, 10 Jul 2017 21:41:59 +0200 Subject: Fix chunk update for filesizes This includes the where limitation of the update query in the total limit of the chunk for loop. --- application/migrations/019_change_filesize_type.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/application/migrations/019_change_filesize_type.php b/application/migrations/019_change_filesize_type.php index 47a998452..33abf89ed 100644 --- a/application/migrations/019_change_filesize_type.php +++ b/application/migrations/019_change_filesize_type.php @@ -21,7 +21,8 @@ class Migration_change_filesize_type extends CI_Migration { $chunk = 500; - $total = $this->db->count_all("file_storage"); + $this->db->where('filesize', 2147483647); + $total = $this->db->count_all_results("file_storage"); for ($limit = 0; $limit < $total; $limit += $chunk) { $query = $this->db->select('hash, id') -- cgit v1.2.3-24-g4f1b