Skip to content

Commit

Permalink
MDL-69166 Replace all upgrade steps with just "create_table" steps
Browse files Browse the repository at this point in the history
  • Loading branch information
rezaies committed Oct 27, 2020
1 parent 61766b3 commit 410973c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 99 deletions.
131 changes: 33 additions & 98 deletions lib/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2862,40 +2862,6 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2021052500.30);
}

if ($oldversion < 2021052500.32) {

// Define table payments to be created.
$table = new xmldb_table('payments');

// Adding fields to table payments.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('component', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null);
$table->add_field('componentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('amount', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
$table->add_field('currency', XMLDB_TYPE_CHAR, '3', null, XMLDB_NOTNULL, null, null);
$table->add_field('gateway', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');

// Adding keys to table payments.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('userid', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);

// Adding indexes to table payments.
$table->add_index('component', XMLDB_INDEX_NOTUNIQUE, ['component']);
$table->add_index('componentid', XMLDB_INDEX_NOTUNIQUE, ['componentid']);
$table->add_index('gateway', XMLDB_INDEX_NOTUNIQUE, ['gateway']);

// Conditionally launch create table for payments.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.32);
}

if ($oldversion < 2021052500.33) {

// Define table payment_accounts to be created.
Expand All @@ -2907,6 +2873,7 @@ function xmldb_main_upgrade($oldversion) {
$table->add_field('idnumber', XMLDB_TYPE_CHAR, '100', null, null, null, null);
$table->add_field('contextid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('enabled', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
$table->add_field('archived', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, null, null, null);
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, null, null, null);

Expand All @@ -2918,6 +2885,12 @@ function xmldb_main_upgrade($oldversion) {
$dbman->create_table($table);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.33);
}

if ($oldversion < 2021052500.34) {

// Define table payment_gateways to be created.
$table = new xmldb_table('payment_gateways');

Expand All @@ -2939,82 +2912,44 @@ function xmldb_main_upgrade($oldversion) {
$dbman->create_table($table);
}

// Define field accountid to be added to payments.
$table = new xmldb_table('payments');
$field = new xmldb_field('accountid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null, 'currency');

// Conditionally launch add field accountid.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define key accountid (foreign) to be added to payments.
$table = new xmldb_table('payments');
$key = new xmldb_key('accountid', XMLDB_KEY_FOREIGN, ['accountid'], 'payment_accounts', ['id']);

// Launch add key accountid.
$dbman->add_key($table, $key);

// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.33);
}

if ($oldversion < 2021052500.26) {

// Define field archived to be added to payment_accounts.
$table = new xmldb_table('payment_accounts');
$field = new xmldb_field('archived', XMLDB_TYPE_INTEGER, '1', null, XMLDB_NOTNULL, null, '0', 'enabled');

// Conditionally launch add field archived.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.26);
upgrade_main_savepoint(true, 2021052500.34);
}

if ($oldversion < 2021052500.27) {

// Define field paymentarea to be added to payments.
$table = new xmldb_table('payments');
$field = new xmldb_field('paymentarea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null, 'component');
if ($oldversion < 2021052500.35) {

// Conditionally launch add field paymentarea.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define index component (not unique) to be dropped form payments.
// Define table payments to be created.
$table = new xmldb_table('payments');
$index = new xmldb_index('component', XMLDB_INDEX_NOTUNIQUE, ['component']);

// Conditionally launch drop index component.
if ($dbman->index_exists($table, $index)) {
$dbman->drop_index($table, $index);
}

// Define index componentid (not unique) to be dropped form payments.
$table = new xmldb_table('payments');
$index = new xmldb_index('componentid', XMLDB_INDEX_NOTUNIQUE, ['componentid']);
// Adding fields to table payments.
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('component', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('paymentarea', XMLDB_TYPE_CHAR, '50', null, XMLDB_NOTNULL, null, null);
$table->add_field('componentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('userid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('amount', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, null);
$table->add_field('currency', XMLDB_TYPE_CHAR, '3', null, XMLDB_NOTNULL, null, null);
$table->add_field('accountid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null);
$table->add_field('gateway', XMLDB_TYPE_CHAR, '100', null, XMLDB_NOTNULL, null, null);
$table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
$table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');

// Conditionally launch drop index componentid.
if ($dbman->index_exists($table, $index)) {
$dbman->drop_index($table, $index);
}
// Adding keys to table payments.
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
$table->add_key('userid', XMLDB_KEY_FOREIGN, ['userid'], 'user', ['id']);
$table->add_key('accountid', XMLDB_KEY_FOREIGN, ['accountid'], 'payment_accounts', ['id']);

// Define index component-paymentarea-componentid (not unique) to be added to payments.
$table = new xmldb_table('payments');
$index = new xmldb_index('component-paymentarea-componentid', XMLDB_INDEX_NOTUNIQUE,
['component', 'paymentarea', 'componentid']);
// Adding indexes to table payments.
$table->add_index('gateway', XMLDB_INDEX_NOTUNIQUE, ['gateway']);
$table->add_index('component-paymentarea-componentid', XMLDB_INDEX_NOTUNIQUE, ['component', 'paymentarea', 'componentid']);

// Conditionally launch add index component-paymentarea-componentid.
if (!$dbman->index_exists($table, $index)) {
$dbman->add_index($table, $index);
// Conditionally launch create table for payments.
if (!$dbman->table_exists($table)) {
$dbman->create_table($table);
}

// Main savepoint reached.
upgrade_main_savepoint(true, 2021052500.27);
upgrade_main_savepoint(true, 2021052500.35);
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2021052500.32; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2021052500.35; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.
$release = '4.0dev (Build: 20201023)'; // Human-friendly version name
Expand Down

0 comments on commit 410973c

Please sign in to comment.