import * as Knex from "knex"; export async function up(knex: Knex): Promise { // tracks table. await knex.schema.createTable( 'tracks', (table: any) => { table.increments('id'); table.string('name'); table.string('storeLinks') table.integer('user') .unsigned() .notNullable() .defaultTo(1) .references('users.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.integer('album') .unsigned() .defaultTo(null) .references('albums.id') .onDelete('SET NULL') .onUpdate('CASCADE') } ) // Artists table. await knex.schema.createTable( 'artists', (table: any) => { table.increments('id'); table.string('name'); table.string('storeLinks'); table.integer('user') .unsigned() .notNullable() .defaultTo(1) .references('users.id') .onDelete('CASCADE') .onUpdate('CASCADE') } ) // Albums table. await knex.schema.createTable( 'albums', (table: any) => { table.increments('id'); table.string('name'); table.string('storeLinks'); table.integer('user') .unsigned() .notNullable() .defaultTo(1) .references('users.id') .onDelete('CASCADE') .onUpdate('CASCADE') } ) // Tags table. await knex.schema.createTable( 'tags', (table: any) => { table.increments('id'); table.string('name'); table.integer('parentId') .references('tags.id') .onDelete('SET NULL') .onUpdate('CASCADE') table.integer('user') .unsigned() .notNullable() .defaultTo(1) .references('users.id') .onDelete('CASCADE') .onUpdate('CASCADE') } ) // Users table. await knex.schema.createTable( 'users', (table: any) => { table.increments('id'); table.string('email'); table.string('passwordHash') } ) // Integrations table. await knex.schema.createTable( 'integrations', (table: any) => { table.increments('id'); table.integer('user') .unsigned() .notNullable() .defaultTo(1) .references('users.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.string('name') .notNullable(); // Uniquely identifies this integration configuration for the user. table.string('type') .notNullable(); // Enumerates different supported integration types (e.g. Spotify) table.string('details'); // Stores anything that might be needed for the integration to work. table.string('secretDetails'); // Stores anything that might be needed for the integration to work and which // should never leave the server. } ) // tracks <-> Artists await knex.schema.createTable( 'tracks_artists', (table: any) => { table.increments('id'); table.integer('trackId') .references('tracks.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.integer('artistId') .references('artists.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.unique(['trackId', 'artistId']) } ) // tracks <-> Tags await knex.schema.createTable( 'tracks_tags', (table: any) => { table.increments('id'); table.integer('trackId') .references('tracks.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.integer('tagId') .references('tags.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.unique(['trackId', 'tagId']) } ) // Artists <-> Tags await knex.schema.createTable( 'artists_tags', (table: any) => { table.increments('id'); table.integer('artistId') .references('artists.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.integer('tagId') .references('tags.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.unique(['artistId', 'tagId']) } ) // Albums <-> Tags await knex.schema.createTable( 'albums_tags', (table: any) => { table.increments('id'); table.integer('tagId') .references('tags.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.integer('albumId') .references('albums.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.unique(['albumId', 'tagId']) } ) // Artists <-> Albums await knex.schema.createTable( 'artists_albums', (table: any) => { table.increments('id'); table.integer('artistId') .references('artists.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.integer('albumId') .references('albums.id') .onDelete('CASCADE') .onUpdate('CASCADE') table.unique(['artistId', 'albumId']) } ) } export async function down(knex: Knex): Promise { await knex.schema.dropTable('tracks'); await knex.schema.dropTable('artists'); await knex.schema.dropTable('albums'); await knex.schema.dropTable('tags'); await knex.schema.dropTable('tracks_artists'); await knex.schema.dropTable('tracks_albums'); await knex.schema.dropTable('tracks_tags'); await knex.schema.dropTable('artists_tags'); await knex.schema.dropTable('albums_tags'); await knex.schema.dropTable('artists_albums'); await knex.schema.dropTable('users'); await knex.schema.dropTable('integrations'); }