aboutsummaryrefslogtreecommitdiffstats
path: root/apps/workflowengine/lib/Migration/Version2019Date20190808074233.php
blob: ffea3c88d015bc59c25399154fe9ccf17c3c6dbd (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

declare(strict_types=1);

namespace OCA\WorkflowEngine\Migration;

use Closure;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use OCP\DB\ISchemaWrapper;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;

class Version2019Date20190808074233 extends SimpleMigrationStep {

	/**
	 * @param IOutput $output
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
	 * @param array $options
	 * @return null|ISchemaWrapper
	 */
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
		/** @var ISchemaWrapper $schema */
		$schema = $schemaClosure();

		if (!$schema->hasTable('flow_checks')) {
			$table = $schema->createTable('flow_checks');
			$table->addColumn('id', Type::INTEGER, [
				'autoincrement' => true,
				'notnull' => true,
				'length' => 4,
			]);
			$table->addColumn('class', Type::STRING, [
				'notnull' => true,
				'length' => 256,
			]);
			$table->addColumn('operator', Type::STRING, [
				'notnull' => true,
				'length' => 16,
			]);
			$table->addColumn('value', Type::TEXT, [
				'notnull' => false,
			]);
			$table->addColumn('hash', Type::STRING, [
				'notnull' => true,
				'length' => 32,
			]);
			$table->setPrimaryKey(['id']);
			$table->addUniqueIndex(['hash'], 'flow_unique_hash');
		}

		if (!$schema->hasTable('flow_operations')) {
			$table = $schema->createTable('flow_operations');
			$table->addColumn('id', Type::INTEGER, [
				'autoincrement' => true,
				'notnull' => true,
				'length' => 4,
			]);
			$table->addColumn('class', Type::STRING, [
				'notnull' => true,
				'length' => 256,
			]);
			$table->addColumn('name', Type::STRING, [
				'notnull' => true,
				'length' => 256,
			]);
			$table->addColumn('checks', Type::TEXT, [
				'notnull' => false,
			]);
			$table->addColumn('operation', Type::TEXT, [
				'notnull' => false,
			]);
			$this->addEntityColumns($table);
			$table->setPrimaryKey(['id']);
		} else {
			$table = $schema->getTable('flow_operations');
			if(!$table->hasColumn('entity')) {
				$this->addEntityColumns($table);
			}
		}

		if (!$schema->hasTable('flow_operations_scope')) {
			$table = $schema->createTable('flow_operations_scope');
			$table->addColumn('id', Type::BIGINT, [
				'autoincrement' => true,
				'notnull' => true,
				'length' => 4,
			]);
			$table->addColumn('operation_id', Type::INTEGER, [
				'notnull' => true,
				'length' => 4,
			]);
			$table->addColumn('type', Type::INTEGER, [
				'notnull' => true,
				'length' => 4,
			]);
			$table->addColumn('value', Type::STRING, [
				'notnull' => false,
				'length' => 64,
			]);
			$table->setPrimaryKey(['id']);
			$table->addUniqueIndex(['operation_id', 'type', 'value'], 'flow_unique_scope');
		}

		return $schema;
	}

	protected function addEntityColumns(Table $table) {
		$table->addColumn('entity', Type::STRING, [
			'notnull' => true,
			'length' => 256,
		]);
		$table->addColumn('events', Type::TEXT, [
			'notnull' => true,
			'default' => '[]',
		]);
	}
}