-
-
Notifications
You must be signed in to change notification settings - Fork 174
/
Version000014000Date20181002094721.php
183 lines (172 loc) Β· 4.63 KB
/
Version000014000Date20181002094721.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/*
* Copyright (c) 2020-2024. The Nextcloud Bookmarks contributors.
*
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/
namespace OCA\Bookmarks\Migration;
use Closure;
use OCP\DB\ISchemaWrapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\SimpleMigrationStep;
/**
* Auto-generated migration step: Please modify to your needs!
*/
class Version000014000Date20181002094721 extends SimpleMigrationStep {
private $db;
public function __construct(IDBConnection $db) {
$this->db = $db;
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*
* @return void
*/
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*
* @return ISchemaWrapper
*/
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
/** @var ISchemaWrapper $schema */
$schema = $schemaClosure();
if (!$schema->hasTable('bookmarks')) {
$table = $schema->createTable('bookmarks');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 64,
]);
$table->addColumn('url', 'string', [
'notnull' => true,
'length' => 4096,
'default' => '',
]);
$table->addColumn('title', 'string', [
'notnull' => true,
'length' => 4096,
'default' => '',
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 64,
'default' => '',
]);
$table->addColumn('description', 'string', [
'notnull' => true,
'length' => 4096,
'default' => '',
]);
$table->addColumn('public', 'smallint', [
'notnull' => false,
'length' => 1,
'default' => 0,
]);
$table->addColumn('added', 'integer', [
'notnull' => false,
'length' => 4,
'default' => 0,
'unsigned' => true,
]);
$table->addColumn('lastmodified', 'integer', [
'notnull' => false,
'length' => 4,
'default' => 0,
'unsigned' => true,
]);
$table->addColumn('clickcount', 'integer', [
'notnull' => true,
'length' => 4,
'default' => 0,
'unsigned' => true,
]);
$table->setPrimaryKey(['id']);
}
if (!$schema->hasTable('bookmarks_tags')) {
$table = $schema->createTable('bookmarks_tags');
$table->addColumn('bookmark_id', 'bigint', [
'notnull' => false,
'length' => 64,
]);
$table->addColumn('tag', 'string', [
'notnull' => true,
'length' => 255,
'default' => '',
]);
$table->addUniqueIndex(['bookmark_id', 'tag'], 'bookmark_tag');
}
if (!$schema->hasTable('bookmarks_folders')) {
$table = $schema->createTable('bookmarks_folders');
$table->addColumn('id', 'bigint', [
'autoincrement' => true,
'notnull' => true,
'length' => 64,
]);
$table->addColumn('parent_folder', 'bigint', [
'notnull' => false,
'length' => 64,
]);
$table->addColumn('title', 'string', [
'notnull' => true,
'length' => 4096,
'default' => '',
]);
$table->addColumn('user_id', 'string', [
'notnull' => true,
'length' => 64,
'default' => '',
]);
$table->setPrimaryKey(['id']);
}
if (!$schema->hasTable('bookmarks_folders_bookmarks')) {
$table = $schema->createTable('bookmarks_folders_bookmarks');
$table->addColumn('folder_id', 'bigint', [
'notnull' => false,
'length' => 64,
]);
$table->addColumn('bookmark_id', 'bigint', [
'notnull' => false,
'length' => 64,
]);
}
return $schema;
}
/**
* @param IOutput $output
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
* @param array $options
*
* @return void
*/
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
$query = $this->db->getQueryBuilder();
$query->select('id', 'user_id')->from('bookmarks');
$bookmarks = $query->execute()->fetchAll();
foreach ($bookmarks as $i => $bookmark) {
$query = $this->db->getQueryBuilder();
$query->select('bookmark_id')
->from('bookmarks_folders_bookmarks')
->where(
$query->expr()->eq('bookmark_id', $query->createPositionalParameter($bookmark['id']))
);
if (count($query->execute()->fetchAll()) > 0) {
continue;
}
$query = $this->db->getQueryBuilder();
$query
->insert('bookmarks_folders_bookmarks')
->values([
'folder_id' => $query->createNamedParameter(-1),
'bookmark_id' => $query->createNamedParameter($bookmark['id']),
]);
$query->execute();
}
}
}