Commit 69a50fea authored by les's avatar les
Browse files

[model] migrations setup

parent c6a5895d
......@@ -4,6 +4,7 @@ const moment = require('moment')
module.exports = (sequelize, DataTypes) => {
const event = sequelize.define('event', {
id: {
allowNull: false,
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
......
......@@ -7,7 +7,7 @@ module.exports = (sequelize, DataTypes) => {
index: true,
primaryKey: true
},
weigth: { type: DataTypes.INTEGER, defaultValue: 0, allowNull: false }
weigth: { type: DataTypes.INTEGER, defaultValue: 0, allowNull: false },
}, {})
tag.associate = function (models) {
......
......@@ -185,11 +185,10 @@ async function setupQuestionnaire(is_docker, db) {
return answers
}
async function upgrade (options) {
async function run_migrations (db_conf) {
const Umzug = require('umzug')
const Sequelize = require('sequelize')
const config = require('config')
const db = new Sequelize(config.db)
const db = new Sequelize(db_conf)
const umzug = new Umzug({
storage: 'sequelize',
storageOptions: { sequelize: db },
......@@ -217,13 +216,15 @@ async function start (options) {
If this is your first run use 'gancio setup --config <CONFIG_FILE.json>' `)
process.exit(-1)
}
await upgrade(options)
const config = require('config')
await run_migrations(config.db)
require('./index')
}
async function setup (options) {
consola.info(`You're going to setup gancio on this machine.`)
const config = await setupQuestionnaire(options.docker, options.db)
await run_migrations(config.db)
const ret = await firstrun.setup(config, options.config)
if (!ret) process.exit(-1)
if (options.docker) {
......
......@@ -27,18 +27,11 @@ module.exports = {
// sync db
const db = require('./api/models')
try {
await db.user.findAll()
const users = await db.user.findAll()
if (users.length) {
consola.warn(` ⚠ Non empty db! Please move your current db elsewhere than retry.`)
return false
} catch (e) { }
consola.info(`Create tables schema`)
await db.sequelize.sync().catch(e => {
consola.error(' ⚠ ️ Error creating tables', e)
return false
})
}
// create admin user
consola.info('Create admin user', admin)
......
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('users', 'username', {
type: Sequelize.STRING,
index: true,
allowNull: false,
defaultValue: ''
})
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
}
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('users', 'display_name', {
type: Sequelize.STRING
})
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
}
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('users', 'rsa', {
type: Sequelize.JSON
})
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
}
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('users', 'followers', {
type: Sequelize.JSON
})
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
}
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('events', 'likes', {
type: Sequelize.JSON,
defaultValue: []
})
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
}
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('events', 'boost', {
type: Sequelize.JSON,
defaultValue: []
})
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
}
'use strict'
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('users', 'settings', {
type: Sequelize.JSON,
defaultValue: {}
})
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
}
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addColumn('events', 'recurrent', {
type: Sequelize.JSON
})
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
},
down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
};
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.removeColumn('users', 'followers'),
queryInterface.createTable('user_followers', {
userId: {
type: Sequelize.INTEGER,
references: {
model: 'users',
key: 'id'
},
primaryKey: true,
allowNull: false
},
fedUserApId: {
primaryKey: true,
type: Sequelize.STRING,
references: {
model: 'fed_users',
key: 'ap_id'
}
},
createdAt: { type: Sequelize.DATE, allowNull: false },
updatedAt: { type: Sequelize.DATE, allowNull: false }
})
])},
down: (queryInterface, Sequelize) => {
return Promise.all([
queryInterface.addColumn(
'users', 'followers', {
type: JSON,
defaultValue: []
}),
queryInterface.dropTable('user_followers')
])
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
};
'use strict';
const { notification: Notification } = require('../api/models')
module.exports = {
up: async (queryInterface, Sequelize) => {
// remove all notifications
await Notification.destroy({where: []})
// add `action` field to notification
try {
await queryInterface.addColumn('notifications', 'action', {
type: Sequelize.ENUM,
values: ['Create', 'Update', 'Delete']
})
} catch {}
// modify values of `type` field
try {
await queryInterface.removeColumn('notifications', 'type').catch()
} catch {}
try {
await queryInterface.addColumn('notifications', 'type', {
type: Sequelize.ENUM,
values: ['mail', 'admin_email', 'ap']
})
} catch {}
await queryInterface.addIndex('notifications', {
unique: true,
fields: ['action', 'type' ]
})
// add AP notifications
await Notification.create({ action: 'Create', type: 'ap', filters: { is_visible: true } })
await Notification.create({ action: 'Update', type: 'ap', filters: { is_visible: true } })
await Notification.create({ action: 'Delete', type: 'ap', filters: { is_visible: true } })
// send anon events via email to admin
await Notification.create({ action: 'Create', type: 'admin_email', filters: { is_visible: false } })
return true
},
down: (queryInterface, Sequelize) => {
return Promise.resolve()
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.dropTable('users');
*/
}
};
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('tags',
{
tag: {
type: Sequelize.STRING,
allowNull: false,
index: true,
primaryKey: true
},
weigth: {
type: Sequelize.INTEGER,
defaultValue: 0, allowNull: false
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('tags')
}
};
'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,
unique: true,
index: true,
allowNull: false
},
address: Sequelize.STRING,
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('places')
}
}
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('settings',
{
key: {
type: Sequelize.STRING,
primaryKey: true,
allowNull: false,
index: true
},
value: 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('users',
{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
username: {
type: Sequelize.STRING,
unique: { msg: 'error.nick_taken' },
index: true,
allowNull: false
},
display_name: Sequelize.STRING,
settings: Sequelize.JSON,
email: {
type: Sequelize.STRING,
unique: { msg: 'error.email_taken' },
index: true,
allowNull: false
},
description: Sequelize.TEXT,
password: Sequelize.STRING,
recover_code: Sequelize.STRING,
is_admin: Sequelize.BOOLEAN,
is_active: Sequelize.BOOLEAN,
rsa: Sequelize.JSON,
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}
)
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('users');
}
};
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('events',
{
id: {
allowNull: false,
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
title: Sequelize.STRING,
slug: Sequelize.STRING,
description: Sequelize.TEXT,
multidate: Sequelize.BOOLEAN,
start_datetime: {
type: Sequelize.INTEGER,
index: true
},
end_datetime: {
type: Sequelize.INTEGER,
index: true
},
image_path: Sequelize.STRING,
is_visible: Sequelize.BOOLEAN,
recurrent: Sequelize.JSON,
// parent: Sequelize.INTEGER
likes: { type: Sequelize.JSON, defaultValue: [] },
boost: { type: Sequelize.JSON, defaultValue: [] },
placeId: {
type: Sequelize.INTEGER,
references: {
model: 'places',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
},
userId: {
type: Sequelize.INTEGER,
references: {
model: 'users',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
},
createdAt: {
allowNull: false,