Skip to content

Commit 70163a6

Browse files
author
Julian Reinhardt
committed
Fixes migration
1 parent 6e82161 commit 70163a6

File tree

1 file changed

+40
-22
lines changed

1 file changed

+40
-22
lines changed

backend/migrations/20211010141200_ssl_passthrough_host.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ const logger = require('../logger').migrate;
1010
* @param {Promise} Promise
1111
* @returns {Promise}
1212
*/
13-
exports.up = function (knex/*, Promise*/) {
13+
exports.up = async function (knex/*, Promise*/) {
1414
logger.info('[' + migrate_name + '] Migrating Up...');
1515

16-
return knex.schema.createTable('ssl_passthrough_host', (table) => {
16+
await knex.schema.createTable('ssl_passthrough_host', (table) => {
1717
table.increments().primary();
1818
table.dateTime('created_on').notNull();
1919
table.dateTime('modified_on').notNull();
@@ -24,26 +24,44 @@ exports.up = function (knex/*, Promise*/) {
2424
table.integer('forwarding_port').notNull().unsigned();
2525
table.integer('enabled').notNull().unsigned().defaultTo(1);
2626
table.json('meta').notNull();
27-
}).then(() => {
28-
logger.info('[' + migrate_name + '] Table created');
29-
})
30-
.then(() => {
31-
return knex.schema.table('user_permission', (table) => {
32-
table.string('ssl_passthrough_hosts').notNull();
33-
})
34-
.then(() => {
35-
return knex('user_permission').update('ssl_passthrough_hosts', knex.ref('streams'));
36-
})
37-
.then(() => {
38-
return knex.schema.alterTable('user_permission', (table) => {
39-
table.string('ssl_passthrough_hosts').notNullable().alter();
40-
});
41-
})
42-
.then(() => {
43-
logger.info('[' + migrate_name + '] permissions updated');
44-
});
45-
})
46-
;
27+
});
28+
29+
logger.info('[' + migrate_name + '] Table created');
30+
31+
// Remove unique constraint so name can be used for new table
32+
await knex.schema.alterTable('user_permission', (table) => {
33+
table.dropUnique('user_id');
34+
});
35+
36+
await knex.schema.renameTable('user_permission', 'user_permission_old');
37+
38+
// We need to recreate the table since sqlite does not support altering columns
39+
await knex.schema.createTable('user_permission', (table) => {
40+
table.increments().primary();
41+
table.dateTime('created_on').notNull();
42+
table.dateTime('modified_on').notNull();
43+
table.integer('user_id').notNull().unsigned();
44+
table.string('visibility').notNull();
45+
table.string('proxy_hosts').notNull();
46+
table.string('redirection_hosts').notNull();
47+
table.string('dead_hosts').notNull();
48+
table.string('streams').notNull();
49+
table.string('ssl_passthrough_hosts').notNull();
50+
table.string('access_lists').notNull();
51+
table.string('certificates').notNull();
52+
table.unique('user_id');
53+
});
54+
55+
await knex('user_permission_old').select('*', 'streams as ssl_passthrough_hosts').then((data) => {
56+
if (data.length) {
57+
return knex('user_permission').insert(data);
58+
}
59+
return Promise.resolve();
60+
});
61+
62+
await knex.schema.dropTableIfExists('user_permission_old');
63+
64+
logger.info('[' + migrate_name + '] permissions updated');
4765
};
4866

4967
/**

0 commit comments

Comments
 (0)