mirror of
https://github.com/apricote/Listory.git
synced 2026-01-13 13:11:02 +00:00
feat(api): setup database migrations
This commit is contained in:
parent
d58cb46f3e
commit
a7c5c68540
6 changed files with 329 additions and 5 deletions
|
|
@ -17,9 +17,15 @@ export const DatabaseModule = TypeOrmModule.forRootAsync({
|
||||||
entities: [join(__dirname, "..", "**/*.entity.{ts,js}")],
|
entities: [join(__dirname, "..", "**/*.entity.{ts,js}")],
|
||||||
|
|
||||||
// Migrations
|
// Migrations
|
||||||
// migrationsRun: true,
|
migrationsRun: true,
|
||||||
// migrations: [join(__dirname, "migrations", "*.{ts,js}")],
|
migrations: [join(__dirname, "migrations", "*.{ts,js}")],
|
||||||
synchronize: true,
|
|
||||||
|
// Debug/Development Options
|
||||||
|
//
|
||||||
|
// logging: true,
|
||||||
|
//
|
||||||
|
// synchronize: true,
|
||||||
|
// migrationsRun: false,
|
||||||
}),
|
}),
|
||||||
inject: [ConfigService],
|
inject: [ConfigService],
|
||||||
});
|
});
|
||||||
|
|
|
||||||
49
src/database/migrations/01-CreateUsersTable.ts
Normal file
49
src/database/migrations/01-CreateUsersTable.ts
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import { MigrationInterface, QueryRunner, Table, TableIndex } from "typeorm";
|
||||||
|
|
||||||
|
export class CreateUsersTable0000000000001 implements MigrationInterface {
|
||||||
|
async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: "user",
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
name: "id",
|
||||||
|
type: "uuid",
|
||||||
|
isPrimary: true,
|
||||||
|
isGenerated: true,
|
||||||
|
generationStrategy: "uuid",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "displayName",
|
||||||
|
type: "varchar",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "photo",
|
||||||
|
type: "varchar",
|
||||||
|
isNullable: true,
|
||||||
|
},
|
||||||
|
{ name: "spotifyId", type: "varchar", isNullable: true },
|
||||||
|
{ name: "spotifyAccesstoken", type: "varchar", isNullable: true },
|
||||||
|
{ name: "spotifyRefreshtoken", type: "varchar", isNullable: true },
|
||||||
|
{
|
||||||
|
name: "spotifyLastrefreshtime",
|
||||||
|
type: "timestamp",
|
||||||
|
isNullable: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
indices: [
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_USER_SPOTIFY_ID",
|
||||||
|
columnNames: ["spotifyId"],
|
||||||
|
isUnique: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.dropTable("user");
|
||||||
|
}
|
||||||
|
}
|
||||||
194
src/database/migrations/02-CreateLibraryTables.ts
Normal file
194
src/database/migrations/02-CreateLibraryTables.ts
Normal file
|
|
@ -0,0 +1,194 @@
|
||||||
|
import {
|
||||||
|
MigrationInterface,
|
||||||
|
QueryRunner,
|
||||||
|
Table,
|
||||||
|
TableIndex,
|
||||||
|
TableForeignKey,
|
||||||
|
} from "typeorm";
|
||||||
|
import { TableColumnOptions } from "typeorm/schema-builder/options/TableColumnOptions";
|
||||||
|
|
||||||
|
const spotifyLibraryDetailsColumns: TableColumnOptions[] = [
|
||||||
|
{ name: "spotifyId", type: "varchar", isNullable: true },
|
||||||
|
{ name: "spotifyUri", type: "varchar", isNullable: true },
|
||||||
|
{ name: "spotifyType", type: "varchar", isNullable: true },
|
||||||
|
{ name: "spotifyHref", type: "varchar", isNullable: true },
|
||||||
|
];
|
||||||
|
|
||||||
|
const primaryUUIDColumn: TableColumnOptions = {
|
||||||
|
name: "id",
|
||||||
|
type: "uuid",
|
||||||
|
isPrimary: true,
|
||||||
|
isGenerated: true,
|
||||||
|
generationStrategy: "uuid",
|
||||||
|
};
|
||||||
|
|
||||||
|
export class CreateLibraryTables0000000000002 implements MigrationInterface {
|
||||||
|
async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: "artist",
|
||||||
|
columns: [
|
||||||
|
primaryUUIDColumn,
|
||||||
|
{
|
||||||
|
name: "name",
|
||||||
|
type: "varchar",
|
||||||
|
},
|
||||||
|
...spotifyLibraryDetailsColumns,
|
||||||
|
],
|
||||||
|
indices: [
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_ARTIST_SPOTIFY_ID",
|
||||||
|
columnNames: ["spotifyId"],
|
||||||
|
isUnique: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: "album",
|
||||||
|
columns: [
|
||||||
|
primaryUUIDColumn,
|
||||||
|
{
|
||||||
|
name: "name",
|
||||||
|
type: "varchar",
|
||||||
|
},
|
||||||
|
...spotifyLibraryDetailsColumns,
|
||||||
|
],
|
||||||
|
indices: [
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_ALBUM_SPOTIFY_ID",
|
||||||
|
columnNames: ["spotifyId"],
|
||||||
|
isUnique: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: "track",
|
||||||
|
columns: [
|
||||||
|
primaryUUIDColumn,
|
||||||
|
{
|
||||||
|
name: "name",
|
||||||
|
type: "varchar",
|
||||||
|
},
|
||||||
|
{ name: "albumId", type: "uuid" },
|
||||||
|
...spotifyLibraryDetailsColumns,
|
||||||
|
],
|
||||||
|
indices: [
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_TRACK_SPOTIFY_ID",
|
||||||
|
columnNames: ["spotifyId"],
|
||||||
|
isUnique: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
foreignKeys: [
|
||||||
|
new TableForeignKey({
|
||||||
|
name: "FK_TRACK_ALBUM_ID",
|
||||||
|
columnNames: ["albumId"],
|
||||||
|
referencedColumnNames: ["id"],
|
||||||
|
referencedTableName: "album",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: "album_artists",
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
name: "albumId",
|
||||||
|
type: "uuid",
|
||||||
|
isPrimary: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "artistId",
|
||||||
|
type: "uuid",
|
||||||
|
isPrimary: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
indices: [
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_ALBUM_ARTISTS_ALBUM_ID",
|
||||||
|
columnNames: ["albumId"],
|
||||||
|
}),
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_ALBUM_ARTISTS_ARTIST_ID",
|
||||||
|
columnNames: ["artistId"],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
foreignKeys: [
|
||||||
|
new TableForeignKey({
|
||||||
|
name: "FK_ALBUM_ID",
|
||||||
|
columnNames: ["albumId"],
|
||||||
|
referencedColumnNames: ["id"],
|
||||||
|
referencedTableName: "album",
|
||||||
|
}),
|
||||||
|
new TableForeignKey({
|
||||||
|
name: "FK_ARTIST_ID",
|
||||||
|
columnNames: ["artistId"],
|
||||||
|
referencedColumnNames: ["id"],
|
||||||
|
referencedTableName: "artist",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: "track_artists",
|
||||||
|
columns: [
|
||||||
|
{
|
||||||
|
name: "trackId",
|
||||||
|
type: "uuid",
|
||||||
|
isPrimary: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "artistId",
|
||||||
|
type: "uuid",
|
||||||
|
isPrimary: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
indices: [
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_TRACK_ARTISTS_TRACK_ID",
|
||||||
|
columnNames: ["trackId"],
|
||||||
|
}),
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_TRACK_ARTISTS_ARTIST_ID",
|
||||||
|
columnNames: ["artistId"],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
foreignKeys: [
|
||||||
|
new TableForeignKey({
|
||||||
|
name: "FK_TRACK_ARTISTS_TRACK_ID",
|
||||||
|
columnNames: ["trackId"],
|
||||||
|
referencedColumnNames: ["id"],
|
||||||
|
referencedTableName: "track",
|
||||||
|
}),
|
||||||
|
new TableForeignKey({
|
||||||
|
name: "FK_TRACK_ARTISTS_ARTIST_ID",
|
||||||
|
columnNames: ["artistId"],
|
||||||
|
referencedColumnNames: ["id"],
|
||||||
|
referencedTableName: "artist",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.dropTable("track_artists");
|
||||||
|
await queryRunner.dropTable("album_artists");
|
||||||
|
await queryRunner.dropTable("track");
|
||||||
|
await queryRunner.dropTable("album");
|
||||||
|
await queryRunner.dropTable("artist");
|
||||||
|
}
|
||||||
|
}
|
||||||
75
src/database/migrations/03-CreateListensTable.ts
Normal file
75
src/database/migrations/03-CreateListensTable.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
import {
|
||||||
|
MigrationInterface,
|
||||||
|
QueryRunner,
|
||||||
|
Table,
|
||||||
|
TableIndex,
|
||||||
|
TableForeignKey,
|
||||||
|
} from "typeorm";
|
||||||
|
import { TableColumnOptions } from "typeorm/schema-builder/options/TableColumnOptions";
|
||||||
|
|
||||||
|
const primaryUUIDColumn: TableColumnOptions = {
|
||||||
|
name: "id",
|
||||||
|
type: "uuid",
|
||||||
|
isPrimary: true,
|
||||||
|
isGenerated: true,
|
||||||
|
generationStrategy: "uuid",
|
||||||
|
};
|
||||||
|
|
||||||
|
export class CreateListensTable0000000000003 implements MigrationInterface {
|
||||||
|
async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.createTable(
|
||||||
|
new Table({
|
||||||
|
name: "listen",
|
||||||
|
columns: [
|
||||||
|
primaryUUIDColumn,
|
||||||
|
{
|
||||||
|
name: "playedAt",
|
||||||
|
type: "timestamp",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "trackId",
|
||||||
|
type: "uuid",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "userId",
|
||||||
|
type: "uuid",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
indices: [
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_LISTEN_TRACK_ID",
|
||||||
|
columnNames: ["trackId"],
|
||||||
|
}),
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_LISTEN_USER_ID",
|
||||||
|
columnNames: ["userId"],
|
||||||
|
}),
|
||||||
|
new TableIndex({
|
||||||
|
name: "IDX_LISTEN_UNIQUE",
|
||||||
|
isUnique: true,
|
||||||
|
columnNames: ["trackId", "userId", "playedAt"],
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
foreignKeys: [
|
||||||
|
new TableForeignKey({
|
||||||
|
name: "FK_LISTEN_TRACK_ID",
|
||||||
|
columnNames: ["trackId"],
|
||||||
|
referencedColumnNames: ["id"],
|
||||||
|
referencedTableName: "track",
|
||||||
|
}),
|
||||||
|
new TableForeignKey({
|
||||||
|
name: "FK_LISTEN_USER_ID",
|
||||||
|
columnNames: ["userId"],
|
||||||
|
referencedColumnNames: ["id"],
|
||||||
|
referencedTableName: "user",
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.dropTable("listen");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ export class Album {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@ManyToMany((type) => Artist, (artist) => artist.albums)
|
@ManyToMany((type) => Artist, (artist) => artist.albums)
|
||||||
@JoinTable()
|
@JoinTable({ name: "album_artists" })
|
||||||
artists: Artist[];
|
artists: Artist[];
|
||||||
|
|
||||||
@OneToMany((type) => Track, (track) => track.album)
|
@OneToMany((type) => Track, (track) => track.album)
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ export class Track {
|
||||||
album: Album;
|
album: Album;
|
||||||
|
|
||||||
@ManyToMany((type) => Artist)
|
@ManyToMany((type) => Artist)
|
||||||
@JoinTable()
|
@JoinTable({ name: "track_artists" })
|
||||||
artists: Artist[];
|
artists: Artist[];
|
||||||
|
|
||||||
@Column((type) => SpotifyLibraryDetails)
|
@Column((type) => SpotifyLibraryDetails)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue