[model] migrations setup

This commit is contained in:
les 2019-10-25 18:43:49 +02:00
parent c6a5895de7
commit 69a50fea4f
26 changed files with 426 additions and 337 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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) {

View file

@ -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)

View file

@ -1,29 +0,0 @@
'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');
*/
}
}

View file

@ -1,26 +0,0 @@
'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');
*/
}
}

View file

@ -1,26 +0,0 @@
'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');
*/
}
}

View file

@ -1,26 +0,0 @@
'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');
*/
}
}

View file

@ -1,27 +0,0 @@
'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');
*/
}
}

View file

@ -1,28 +0,0 @@
'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');
*/
}
}

View file

@ -1,27 +0,0 @@
'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');
*/
}
}

View file

@ -1,27 +0,0 @@
'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');
*/
}
};

View file

@ -1,47 +0,0 @@
'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');
*/
}
};

View file

@ -1,56 +0,0 @@
'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');
*/
}
};

View file

@ -0,0 +1,31 @@
'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')
}
};

View file

@ -0,0 +1,34 @@
'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')
}
}

View file

@ -0,0 +1,29 @@
'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')
}
};

View file

@ -0,0 +1,48 @@
'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');
}
};

View file

@ -0,0 +1,63 @@
'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,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('events')
}
};

View file

@ -0,0 +1,41 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('comments',
{
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
activitypub_id: {
type: Sequelize.STRING(18),
index: true,
unique: true
},
eventId: {
type: Sequelize.INTEGER,
references: {
model: 'events',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'SET NULL',
},
data: Sequelize.JSON,
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('comments')
}
};

View file

@ -1,4 +1,5 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('fed_users', {
@ -17,9 +18,9 @@ module.exports = {
allowNull: false,
type: Sequelize.DATE
}
});
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('fed_users');
return queryInterface.dropTable('fed_users')
}
};
};

View file

@ -0,0 +1,35 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('user_followers', {
userId: {
type: Sequelize.INTEGER,
references: {
model: 'users',
key: 'id'
},
primaryKey: true,
allowNull: false,
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
fedUserApId: {
primaryKey: true,
type: Sequelize.STRING,
references: {
model: 'fed_users',
key: 'ap_id'
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE'
},
createdAt: { type: Sequelize.DATE, allowNull: false },
updatedAt: { type: Sequelize.DATE, allowNull: false }
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('user_followers')
}
};

View file

@ -0,0 +1,36 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('notifications',{
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
filters: Sequelize.JSON,
email: Sequelize.STRING,
remove_code: Sequelize.STRING,
action: {
type: Sequelize.ENUM,
values: ['Create', 'Update', 'Delete']
},
type: {
type: Sequelize.ENUM,
values: ['mail', 'admin_email', 'ap']
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('notifications')
}
};

View file

@ -0,0 +1,15 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.addIndex('notifications', {
unique: true,
fields: ['action', 'type']
}).catch(e => {})
},
down: (queryInterface, Sequelize) => {
return queryInterface.removeIndex('notifications',
['actions', 'type'])
}
};

View file

@ -0,0 +1,40 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('event_tags', {
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
},
eventId: {
type: Sequelize.INTEGER,
primaryKey: true,
references: {
model: 'events',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
tagTag: {
primaryKey: true,
type: Sequelize.STRING,
references: {
model: 'tags',
key: 'tag',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('event_tags')
}
};

View file

@ -0,0 +1,40 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('event_notifications', {
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
},
eventId: {
type: Sequelize.INTEGER,
primaryKey: true,
references: {
model: 'events',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
notificationId: {
primaryKey: true,
type: Sequelize.INTEGER,
references: {
model: 'notifications',
key: 'id',
},
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
}
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('event_notifications')
}
}