import { buildUUID } from '@shared/extra-utils' ; import * as Sequelize from 'sequelize' ; async function up ( utils ) { const q = utils . queryInterface ; const dataAuthorUUID = { type : Sequelize . UUID , defaultValue : Sequelize . UUIDV4 , allowNull : true } ; await q . addColumn ( 'Authors' , 'uuid' , dataAuthorUUID ) ; { const authors = await utils . db . Author . findAll ( ) ; for ( const author of authors ) { author . uuid = buildUUID ( ) ; await author . save ( ) ; } } dataAuthorUUID . allowNull = false ; await q . changeColumn ( 'Authors' , 'uuid' , dataAuthorUUID ) ; const users = await utils . db . User . findAll ( ) ; for ( const user of users ) { const author = await utils . db . Author . find ( { where : { userId : user . id } } ) ; if ( ! author ) { await utils . db . Author . create ( { name : user . username , podId : null , userId : user . id } ) ; } } await utils . db . VideoChannel . sync ( ) ; const authors = await utils . db . Author . findAll ( ) ; for ( const author of authors ) { await utils . db . VideoChannel . create ( { name : `Default ${ author . name } channel` , remote : false , authorId : author . id } ) ; } const dataChannelId = { type : Sequelize . INTEGER , defaultValue : null , allowNull : true } ; await q . addColumn ( 'Videos' , 'channelId' , dataChannelId ) ; const query = 'SELECT "id", "authorId" FROM "Videos"' ; const options = { type : Sequelize . QueryTypes . SELECT } ; const rawVideos = await utils . sequelize . query ( query , options ) as any ; for ( const rawVideo of rawVideos ) { const videoChannel = await utils . db . VideoChannel . findOne ( { where : { authorId : rawVideo . authorId } } ) ; const video = await utils . db . Video . findByPk ( rawVideo . id ) ; video . channelId = videoChannel . id ; await video . save ( ) ; } dataChannelId . allowNull = false ; await q . changeColumn ( 'Videos' , 'channelId' , dataChannelId ) ; const constraintName = 'Videos_channelId_fkey' ; const queryForeignKey = 'ALTER TABLE "Videos" ' + ' ADD CONSTRAINT "' + constraintName + '"' + ' FOREIGN KEY ("channelId") REFERENCES "VideoChannels" ON UPDATE CASCADE ON DELETE CASCADE' ; await utils . sequelize . query ( queryForeignKey ) ; await q . removeColumn ( 'Videos' , 'authorId' ) ; } function down ( options ) { throw new Error ( 'Not implemented.' ) ; } export { up , down } ;