Commit 638e4a0b authored by les's avatar les
Browse files

support db migration at startup

parent 04943a36
......@@ -164,6 +164,7 @@ async function start (options) {
This is your first run? Run 'gancio setup'`)
process.exit(-1)
}
await upgrade(options)
require('./index')
}
......@@ -177,8 +178,25 @@ async function setup (options) {
}
async function upgrade (options) {
consola.warn('Not implemented yet but should be an easy task! PR welcome!')
process.exit(-1)
const Umzug = require('umzug')
const Sequelize = require('sequelize')
const config = require('config')
const db = new Sequelize(config.db)
const umzug = new Umzug({
storage: 'sequelize',
storageOptions: { sequelize: db },
migrations: {
wrap: fun => {
return () => fun(db.queryInterface, Sequelize)
},
path: path.resolve(__dirname, 'migrations')
}
})
const migrations = await umzug.up()
if (migrations.length) {
consola.info('Migrations executed: ', migrations.map(m => m.file))
}
db.close()
}
consola.info(`${package.name} - v${package.version} - ${package.description}`)
......@@ -188,7 +206,7 @@ require('yargs')
.option('config', {
alias: 'c',
describe: 'Configuration file',
default: '/gancio/config.json',
default: '/opt/gancio/config.json',
})
.coerce('config', config_path => {
const absolute_config_path = path.resolve(cwd, config_path)
......
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
email: {
type: Sequelize.STRING,
unique: true,
index: true,
allowNull: false
},
description: {
type: Sequelize.TEXT
},
password: {
type: Sequelize.STRING
},
recover_code: {
type: Sequelize.STRING
},
is_admin: {
type: Sequelize.BOOLEAN
},
is_active: {
type: Sequelize.BOOLEAN
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('users');
}
};
\ No newline at end of file
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('places', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
address: {
type: Sequelize.STRING
},
weigth: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('places');
}
};
\ No newline at end of file
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('events', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
title: {
type: Sequelize.STRING
},
slug: {
type: Sequelize.STRING,
index: true
},
description: {
type: Sequelize.TEXT
},
multidate: {
type: Sequelize.BOOLEAN
},
start_datetime: {
type: Sequelize.INTEGER,
index: true
},
end_datetime: {
type: Sequelize.INTEGER,
index: true
},
image_path: {
type: Sequelize.STRING
},
is_visible: {
type: Sequelize.BOOLEAN
},
activitypub_id: {
type: Sequelize.BIGINT
},
userId: {
type: Sequelize.INTEGER,
references: {
model: 'users',
key: 'id'
}
},
placeId: {
type: Sequelize.INTEGER,
references: {
model: 'places',
key: 'id'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('events')
}
}
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('notifications', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
filters: {
type: Sequelize.JSON
},
email: {
type: Sequelize.STRING
},
remove_code: {
type: Sequelize.STRING
},
type: {
type: Sequelize.ENUM,
values: ['mail', 'admin_email', 'mastodon']
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('notifications')
}
}
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('tags', {
tag: {
type: Sequelize.STRING,
allowNull: false,
primaryKey: true
},
weigth: {
type: Sequelize.INTEGER
},
color: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('tags');
}
};
\ No newline at end of file
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('event_notification', {
eventId: {
type: Sequelize.INTEGER,
references: {
model: 'events',
key: 'id'
},
onDelete: 'cascade',
onUpdate: 'cascade'
},
notificationId: {
type: Sequelize.INTEGER,
references: {
model: 'notifications',
key: 'id'
},
onDelete: 'cascade',
onUpdate: 'cascade'
},
status: {
type: Sequelize.ENUM,
values: ['new', 'sent', 'error'],
defaultValue: 'new',
index: true
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('event_notification')
}
}
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('comments', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
eventId: {
type: Sequelize.INTEGER,
references: {
model: 'events',
key: 'id'
}
},
activitypub_id: {
type: Sequelize.BIGINT,
index: true,
},
data: {
type: Sequelize.JSON
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('comments');
}
};
\ No newline at end of file
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('settings', {
key: {
type: Sequelize.STRING,
primaryKey: true,
allowNull: false,
index: true
},
value: {
type: Sequelize.JSON
},
is_secret: Sequelize.BOOLEAN,
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('settings')
}
}
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('event_tags', {
eventId: {
type: Sequelize.INTEGER,
references: {
model: 'events',
key: 'id'
},
onDelete: 'cascade',
onUpdate: 'cascade'
},
tagTag: {
type: Sequelize.STRING,
references: {
model: 'tags',
key: 'tag'
},
onDelete: 'cascade',
onUpdate: 'cascade'
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('event_tags')
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment