-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.php
175 lines (159 loc) · 7.57 KB
/
database.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
<?php
// To allow this to be called directly or from admin/upgrade.php
if ( !isset($PDOX) ) {
require_once "../config.php";
$CURRENT_FILE = __FILE__;
require $CFG->dirroot."/admin/migrate-setup.php";
}
// The SQL to uninstall this tool
$DATABASE_UNINSTALL = array(
"drop table if exists {$CFG->dbprefix}migration",
"drop table if exists {$CFG->dbprefix}migration_site",
"drop table if exists {$CFG->dbprefix}migration_site_property"
);
// The SQL to create the tables if they don't exist
$DATABASE_INSTALL = array(
array(
"{$CFG->dbprefix}migration",
"CREATE TABLE `{$CFG->dbprefix}migration` (
`link_id` int NOT NULL DEFAULT '0',
`user_id` int NOT NULL DEFAULT '0',
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` int NOT NULL DEFAULT '0',
`is_admin` tinyint(1) NOT NULL DEFAULT '0',
UNIQUE KEY `link_id` (`link_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3"
),
array( "{$CFG->dbprefix}migration_site",
"CREATE TABLE `{$CFG->dbprefix}migration_site` (
`link_id` int(11) NOT NULL,
`site_id` VARCHAR(99) NOT NULL,
`transfer_site_id` varchar(255) DEFAULT NULL,
`imported_site_id` int(11) NOT NULL DEFAULT '0',
`started_at` datetime DEFAULT NULL,
`started_by` int(11) NOT NULL DEFAULT '0',
`uploaded_at` datetime DEFAULT NULL,
`modified_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`modified_by` int(11) NOT NULL DEFAULT '0',
`provider` mediumtext,
`term` int(11) DEFAULT NULL,
`dept` VARCHAR(25) DEFAULT NULL,
`active` tinyint(1) DEFAULT '0',
`is_paused` tinyint(1) DEFAULT '0',
`state` enum('init','starting','exporting','running','queued','uploading','importing','updating','completed','error','paused','admin') DEFAULT NULL,
`title` VARCHAR(99) DEFAULT NULL,
`workflow` mediumtext,
`notification` mediumtext,
`report_url` VARCHAR(255) DEFAULT NULL,
`files` mediumtext,
`test_conversion` tinyint(1) NOT NULL DEFAULT '0',
`target_title` VARCHAR(255) DEFAULT NULL,
`target_course` VARCHAR(255) DEFAULT NULL,
`target_term` int(11) DEFAULT NULL,
`target_dept` VARCHAR(25) DEFAULT NULL,
`target_site_id` int(11) NOT NULL DEFAULT '0',
`target_site_created` tinyint(1) DEFAULT '0',
`create_course_offering` tinyint(1) DEFAULT '0',
`site_url` VARCHAR(255) DEFAULT NULL,
`archive_log` mediumtext,
`site_owners` mediumtext,
`zip_size` bigint(20) unsigned DEFAULT NULL,
`failure_type` enum('exception','expired','import-error') DEFAULT NULL,
`failure_detail` VARCHAR(255) DEFAULT NULL,
`enrol_users` TINYINT(1) NOT NULL DEFAULT 0,
PRIMARY KEY (`site_id`,`link_id`),
KEY `idx_started_by` (`started_by`),
KEY `idx_modified_by` (`modified_by`),
KEY `migration_link_ibfk` (`link_id`),
CONSTRAINT `{$CFG->dbprefix}migration_link_ibfk`
FOREIGN KEY (`link_id`)
REFERENCES `{$CFG->dbprefix}migration` (`link_id`)
ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;"
),
array( "{$CFG->dbprefix}migration_site_property",
"CREATE TABLE `{$CFG->dbprefix}migration_site_property` (
`site_id` varchar(99) NOT NULL,
`key` varchar(45) NOT NULL,
`found` tinyint(1) DEFAULT '0',
`detail` mediumtext,
PRIMARY KEY (`site_id`,`key`),
CONSTRAINT `migration_property_link_fk` FOREIGN KEY (`site_id`) REFERENCES `migration_site` (`site_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3"
)
);
// Database upgrade
$DATABASE_UPGRADE = function($oldversion) {
global $CFG, $PDOX;
// This is a place to make sure added fields are present
// if you add a field to a table, put it in here and it will be auto-added
$add_some_fields = array(
array('migration', 'is_admin', 'TINYINT(1) NOT NULL DEFAULT 0'),
array('migration_site', 'link_id', 'int(11) NOT NULL'),
array('migration_site', 'site_id', 'VARCHAR(99) NOT NULL'),
array('migration_site', 'title', 'VARCHAR(99) DEFAULT NULL'),
array('migration_site', 'state', "enum('init','starting','exporting','running','queued','uploading','importing','updating','completed','error','paused','admin') DEFAULT NULL"),
array('migration_site', 'transfer_site_id', 'varchar(255) DEFAULT NULL'),
array('migration_site', 'imported_site_id', 'int(11) NOT NULL DEFAULT 0'),
array('migration_site', 'started_at', 'datetime DEFAULT NULL'),
array('migration_site', 'started_by', 'int(11) NOT NULL DEFAULT 0'),
array('migration_site', 'modified_at', 'datetime NOT NULL DEFAULT CURRENT_TIMESTAMP'),
array('migration_site', 'modified_by', 'int(11) NOT NULL DEFAULT 0'),
array('migration_site', 'files', 'mediumtext'),
array('migration_site', 'term', 'int(11) DEFAULT NULL'),
array('migration_site', 'dept', 'VARCHAR(25) DEFAULT NULL'),
array('migration_site', 'active', 'tinyint(1) DEFAULT 0'),
array('migration_site', 'workflow', 'mediumtext'),
array('migration_site', 'notification', 'mediumtext'),
array('migration_site', 'test_conversion', 'tinyint(1) NOT NULL DEFAULT 0'),
array('migration_site', 'uploaded_at', 'datetime DEFAULT NULL'),
array('migration_site', 'report_url', 'varchar(255) DEFAULT NULL'),
array('migration_site', 'is_paused', 'tinyint(1) DEFAULT 0'),
array('migration_site', 'target_title', 'VARCHAR(255) DEFAULT NULL'),
array('migration_site', 'target_course', 'VARCHAR(255) DEFAULT NULL'),
array('migration_site', 'target_term', 'int(11) DEFAULT NULL'),
array('migration_site', 'target_dept', 'VARCHAR(25) DEFAULT NULL'),
array('migration_site', 'create_course_offering', 'tinyint(1) DEFAULT 0'),
array('migration_site', 'target_site_id', 'int(11) NOT NULL DEFAULT 0'),
array('migration_site', 'target_site_created', 'tinyint(1) DEFAULT 0'),
array('migration_site', 'site_url', 'VARCHAR(255) DEFAULT NULL'),
array('migration_site', 'archive_log', 'mediumtext'),
array('migration_site', 'site_owners', 'mediumtext'),
array('migration_site', 'zip_size', 'bigint(20) unsigned DEFAULT NULL'),
array('migration_site', 'failure_type', "enum('exception','expired','import-error') DEFAULT NULL"),
array('migration_site', 'failure_detail', 'VARCHAR(255) DEFAULT NULL'),
array('migration_site', 'enrol_users', 'TINYINT(1) NOT NULL DEFAULT 0'),
// drop report
array('migration_site', 'report', 'DROP')
);
foreach ( $add_some_fields as $add_field ) {
if (count($add_field) != 3 ) {
echo("Badly formatted add_field");
var_dump($add_field);
continue;
}
$table = $add_field[0];
$column = $add_field[1];
$type = $add_field[2];
$sql = false;
if ( $PDOX->columnExists($column, $CFG->dbprefix.$table ) ) {
if ( $type == 'DROP' ) {
$sql= "ALTER TABLE {$CFG->dbprefix}$table DROP COLUMN $column";
} else {
// continue;
$sql= "ALTER TABLE {$CFG->dbprefix}$table MODIFY $column $type";
}
} else {
if ( $type == 'DROP' ) continue;
$sql= "ALTER TABLE {$CFG->dbprefix}$table ADD $column $type";
}
echo("Upgrading: ".$sql."<br/>\n");
error_log("Upgrading: ".$sql);
$q = $PDOX->queryReturnError($sql);
}
return 202210211000;
}; // Don't forget the semicolon on anonymous functions :)
// Do the actual migration if we are not in admin/upgrade.php
if ( isset($CURRENT_FILE) ) {
include $CFG->dirroot."/admin/migrate-run.php";
}