Skip to content

Commit 4a4877e

Browse files
committed
Add command to migrate local files to the database
1 parent 8227853 commit 4a4877e

3 files changed

Lines changed: 73 additions & 1 deletion

File tree

Command/MigrateCommand.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace Kanboard\Plugin\DatabaseStorage\Command;
4+
5+
use Kanboard\Console\BaseCommand;
6+
use Kanboard\Core\ObjectStorage\ObjectStorageException;
7+
use Kanboard\Plugin\DatabaseStorage\DatabaseObjectStorage;
8+
use RecursiveDirectoryIterator;
9+
use RecursiveIteratorIterator;
10+
use Symfony\Component\Console\Helper\Table;
11+
use Symfony\Component\Console\Input\InputInterface;
12+
use Symfony\Component\Console\Output\OutputInterface;
13+
14+
class MigrateCommand extends BaseCommand
15+
{
16+
protected function configure()
17+
{
18+
$this
19+
->setName('db-storage:migrate')
20+
->setDescription('Migrate local files to the database');
21+
}
22+
23+
protected function execute(InputInterface $input, OutputInterface $output)
24+
{
25+
if (! file_exists(FILES_DIR)) {
26+
$output->writeln('<error>Directory not found: '.FILES_DIR.'</error>');
27+
} else {
28+
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(FILES_DIR), RecursiveIteratorIterator::SELF_FIRST);
29+
$storage = new DatabaseObjectStorage($this->container['db']);;
30+
31+
foreach($files as $file) {
32+
if ($file->getFilename(){0} !== '.' && ! $file->isDir()) {
33+
$key = substr($file->getRealPath(), strlen(FILES_DIR) + 1);
34+
35+
if (! $this->fileExists($storage, $key)) {
36+
$output->writeln('<info>Migrating '.$file->getFilename().'</info>');
37+
$blob = $this->readFile($file->getRealPath());
38+
$storage->put($key, $blob);
39+
}
40+
}
41+
}
42+
}
43+
}
44+
45+
protected function fileExists(DatabaseObjectStorage $storage, $key)
46+
{
47+
try {
48+
$storage->get($key);
49+
return true;
50+
} catch (ObjectStorageException $e) {}
51+
52+
return false;
53+
}
54+
55+
protected function readFile($filename)
56+
{
57+
$handle = fopen($filename, 'rb');
58+
$contents = fread($handle, filesize($filename));
59+
fclose($handle);
60+
return $contents;
61+
}
62+
}

Plugin.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Kanboard\Plugin\DatabaseStorage;
44

55
use Kanboard\Core\Plugin\Base;
6+
use Kanboard\Plugin\DatabaseStorage\Command\MigrateCommand;
67

78
class Plugin extends Base
89
{
@@ -11,6 +12,8 @@ public function initialize()
1112
$this->container['objectStorage'] = function() {
1213
return new DatabaseObjectStorage($this->db);
1314
};
15+
16+
$this->cli->add(new MigrateCommand($this->container));
1417
}
1518

1619
public function getPluginName()
@@ -30,7 +33,7 @@ public function getPluginAuthor()
3033

3134
public function getPluginVersion()
3235
{
33-
return '1.0.0';
36+
return '1.0.1';
3437
}
3538

3639
public function getPluginHomepage()

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ The main benefit of doing this is to simplify backups.
2828
Everything is in a central location and nothing is stored on the frontend servers.
2929
PostgreSQL is preferred because streaming files is supported.
3030

31+
Migrating old files to the database
32+
-----------------------------------
33+
34+
```bash
35+
./cli db-storage:migrate
36+
```
37+
3138
Notes
3239
-----
3340

0 commit comments

Comments
 (0)