feat: schema diff sql tools (#17116)

This commit is contained in:
Jason Rasmussen
2025-03-28 10:40:09 -04:00
committed by GitHub
parent 3fde5a8328
commit 4b4bcd23f4
132 changed files with 5837 additions and 1246 deletions

View File

@@ -1,7 +1,7 @@
import { Insertable, Kysely } from 'kysely';
import { randomBytes } from 'node:crypto';
import { Writable } from 'node:stream';
import { Assets, DB, Partners, Sessions, Users } from 'src/db';
import { Assets, DB, Partners, Sessions } from 'src/db';
import { AuthDto } from 'src/dtos/auth.dto';
import { AssetType } from 'src/enum';
import { AccessRepository } from 'src/repositories/access.repository';
@@ -35,6 +35,7 @@ import { TrashRepository } from 'src/repositories/trash.repository';
import { UserRepository } from 'src/repositories/user.repository';
import { VersionHistoryRepository } from 'src/repositories/version-history.repository';
import { ViewRepository } from 'src/repositories/view-repository';
import { UserTable } from 'src/tables/user.table';
import { newTelemetryRepositoryMock } from 'test/repositories/telemetry.repository.mock';
import { newUuid } from 'test/small.factory';
import { automock } from 'test/utils';
@@ -57,7 +58,7 @@ class CustomWritable extends Writable {
}
type Asset = Partial<Insertable<Assets>>;
type User = Partial<Insertable<Users>>;
type User = Partial<Insertable<UserTable>>;
type Session = Omit<Insertable<Sessions>, 'token'> & { token?: string };
type Partner = Insertable<Partners>;
@@ -103,7 +104,7 @@ export class TestFactory {
static user(user: User = {}) {
const userId = user.id || newUuid();
const defaults: Insertable<Users> = {
const defaults: Insertable<UserTable> = {
email: `${userId}@immich.cloud`,
name: `User ${userId}`,
deletedAt: null,

View File

@@ -1,14 +0,0 @@
import { AuditEntity } from 'src/entities/audit.entity';
import { DatabaseAction, EntityType } from 'src/enum';
import { authStub } from 'test/fixtures/auth.stub';
export const auditStub = {
delete: Object.freeze<AuditEntity>({
id: 3,
entityId: 'asset-deleted',
action: DatabaseAction.DELETE,
entityType: EntityType.ASSET,
ownerId: authStub.admin.user.id,
createdAt: new Date(),
}),
};

View File

@@ -17,7 +17,6 @@ export const userStub = {
createdAt: new Date('2021-01-01'),
deletedAt: null,
updatedAt: new Date('2021-01-01'),
tags: [],
assets: [],
metadata: [],
quotaSizeInBytes: null,
@@ -36,7 +35,6 @@ export const userStub = {
createdAt: new Date('2021-01-01'),
deletedAt: null,
updatedAt: new Date('2021-01-01'),
tags: [],
assets: [],
metadata: [
{
@@ -62,7 +60,6 @@ export const userStub = {
createdAt: new Date('2021-01-01'),
deletedAt: null,
updatedAt: new Date('2021-01-01'),
tags: [],
assets: [],
quotaSizeInBytes: null,
quotaUsageInBytes: 0,
@@ -81,7 +78,6 @@ export const userStub = {
createdAt: new Date('2021-01-01'),
deletedAt: null,
updatedAt: new Date('2021-01-01'),
tags: [],
assets: [],
quotaSizeInBytes: null,
quotaUsageInBytes: 0,
@@ -100,7 +96,6 @@ export const userStub = {
createdAt: new Date('2021-01-01'),
deletedAt: null,
updatedAt: new Date('2021-01-01'),
tags: [],
assets: [],
quotaSizeInBytes: null,
quotaUsageInBytes: 0,

View File

@@ -1,9 +1,8 @@
import { randomUUID } from 'node:crypto';
import { ApiKey, Asset, AuthApiKey, AuthUser, Library, Partner, User, UserAdmin } from 'src/database';
import { AuthDto } from 'src/dtos/auth.dto';
import { OnThisDayData } from 'src/entities/memory.entity';
import { AssetStatus, AssetType, MemoryType, Permission, UserStatus } from 'src/enum';
import { ActivityItem, MemoryItem } from 'src/types';
import { ActivityItem, MemoryItem, OnThisDayData } from 'src/types';
export const newUuid = () => randomUUID() as string;
export const newUuids = () =>

View File

@@ -0,0 +1,41 @@
import { Check, Column, DatabaseConstraintType, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
@Check({ expression: '1=1' })
export class Table1 {
@Column({ type: 'uuid' })
id!: string;
}
export const description = 'should create a check constraint with a default name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.CHECK,
name: 'CHK_8d2ecfd49b984941f6b2589799',
tableName: 'table1',
expression: '1=1',
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,41 @@
import { Check, Column, DatabaseConstraintType, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
@Check({ name: 'CHK_test', expression: '1=1' })
export class Table1 {
@Column({ type: 'uuid' })
id!: string;
}
export const description = 'should create a check constraint with a specific name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.CHECK,
name: 'CHK_test',
tableName: 'table1',
expression: '1=1',
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,33 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ type: 'boolean', default: true })
column1!: boolean;
}
export const description = 'should register a table with a column with a default value (boolean)';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'boolean',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
default: 'true',
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,35 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
const date = new Date(2023, 0, 1);
@Table()
export class Table1 {
@Column({ type: 'character varying', default: date })
column1!: string;
}
export const description = 'should register a table with a column with a default value (date)';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
default: "'2023-01-01T00:00:00.000Z'",
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,33 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ type: 'character varying', default: () => 'now()' })
column1!: string;
}
export const description = 'should register a table with a column with a default function';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
default: 'now()',
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,32 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ type: 'character varying', default: null })
column1!: string;
}
export const description = 'should register a nullable column from a default of null';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: true,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,33 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ type: 'integer', default: 0 })
column1!: string;
}
export const description = 'should register a table with a column with a default value (number)';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'integer',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
default: '0',
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,33 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ type: 'character varying', default: 'foo' })
column1!: string;
}
export const description = 'should register a table with a column with a default value (string)';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
default: "'foo'",
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,39 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
enum Test {
Foo = 'foo',
Bar = 'bar',
}
@Table()
export class Table1 {
@Column({ enum: Test })
column1!: string;
}
export const description = 'should use a default enum naming convention';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'enum',
enumName: 'table1_column1_enum',
enumValues: ['foo', 'bar'],
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,41 @@
import { Column, ColumnIndex, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@ColumnIndex()
@Column()
column1!: string;
}
export const description = 'should create a column with an index';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [
{
name: 'IDX_50c4f9905061b1e506d38a2a38',
columnNames: ['column1'],
tableName: 'table1',
unique: false,
synchronize: true,
},
],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,32 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ default: null })
column1!: string;
}
export const description = 'should infer nullable from the default value';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: true,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,32 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column()
column1!: string;
}
export const description = 'should register a table with a column with a default name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,32 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ name: 'column-1' })
column1!: string;
}
export const description = 'should register a table with a column with a specific name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column-1',
tableName: 'table1',
type: 'character varying',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,32 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column('column-1')
column1!: string;
}
export const description = 'should register a table with a column with a specific name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column-1',
tableName: 'table1',
type: 'character varying',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,32 @@
import { Column, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ nullable: true })
column1!: string;
}
export const description = 'should set nullable correctly';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: true,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,40 @@
import { Column, DatabaseConstraintType, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ type: 'uuid', unique: true })
id!: string;
}
export const description = 'should create a unique key constraint with a default name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.UNIQUE,
name: 'UQ_b249cc64cf63b8a22557cdc8537',
tableName: 'table1',
columnNames: ['id'],
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,40 @@
import { Column, DatabaseConstraintType, DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@Column({ type: 'uuid', unique: true, uniqueConstraintName: 'UQ_test' })
id!: string;
}
export const description = 'should create a unique key constraint with a specific name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.UNIQUE,
name: 'UQ_test',
tableName: 'table1',
columnNames: ['id'],
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,73 @@
import { DatabaseConstraintType, DatabaseSchema, ForeignKeyColumn, PrimaryColumn, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@PrimaryColumn({ type: 'uuid' })
id!: string;
}
@Table()
export class Table2 {
@ForeignKeyColumn(() => Table1, {})
parentId!: string;
}
export const description = 'should infer the column type from the reference column';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: true,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.PRIMARY_KEY,
name: 'PK_b249cc64cf63b8a22557cdc8537',
tableName: 'table1',
columnNames: ['id'],
synchronize: true,
},
],
synchronize: true,
},
{
name: 'table2',
columns: [
{
name: 'parentId',
tableName: 'table2',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.FOREIGN_KEY,
name: 'FK_3fcca5cc563abf256fc346e3ff4',
tableName: 'table2',
columnNames: ['parentId'],
referenceColumnNames: ['id'],
referenceTableName: 'table1',
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,80 @@
import { DatabaseConstraintType, DatabaseSchema, ForeignKeyColumn, PrimaryColumn, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@PrimaryColumn({ type: 'uuid' })
id!: string;
}
@Table()
export class Table2 {
@ForeignKeyColumn(() => Table1, { unique: true })
parentId!: string;
}
export const description = 'should create a foreign key constraint with a unique constraint';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: true,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.PRIMARY_KEY,
name: 'PK_b249cc64cf63b8a22557cdc8537',
tableName: 'table1',
columnNames: ['id'],
synchronize: true,
},
],
synchronize: true,
},
{
name: 'table2',
columns: [
{
name: 'parentId',
tableName: 'table2',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.FOREIGN_KEY,
name: 'FK_3fcca5cc563abf256fc346e3ff4',
tableName: 'table2',
columnNames: ['parentId'],
referenceColumnNames: ['id'],
referenceTableName: 'table1',
synchronize: true,
},
{
type: DatabaseConstraintType.UNIQUE,
name: 'REL_3fcca5cc563abf256fc346e3ff',
tableName: 'table2',
columnNames: ['parentId'],
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,41 @@
import { Column, DatabaseSchema, Index, Table } from 'src/sql-tools';
@Table()
@Index({ columns: ['id'] })
export class Table1 {
@Column({ type: 'uuid' })
id!: string;
}
export const description = 'should create an index with a default name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [
{
name: 'IDX_b249cc64cf63b8a22557cdc853',
tableName: 'table1',
unique: false,
columnNames: ['id'],
synchronize: true,
},
],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,41 @@
import { Column, DatabaseSchema, Index, Table } from 'src/sql-tools';
@Table()
@Index({ name: 'IDX_test', columns: ['id'] })
export class Table1 {
@Column({ type: 'uuid' })
id!: string;
}
export const description = 'should create an index with a specific name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [
{
name: 'IDX_test',
tableName: 'table1',
unique: false,
columnNames: ['id'],
synchronize: true,
},
],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,41 @@
import { Column, DatabaseSchema, Index, Table } from 'src/sql-tools';
@Table()
@Index({ expression: '"id" IS NOT NULL' })
export class Table1 {
@Column({ nullable: true })
column1!: string;
}
export const description = 'should create an index based off of an expression';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: true,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [
{
name: 'IDX_376788d186160c4faa5aaaef63',
tableName: 'table1',
unique: false,
expression: '"id" IS NOT NULL',
synchronize: true,
},
],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,42 @@
import { Column, DatabaseSchema, Index, Table } from 'src/sql-tools';
@Table()
@Index({ columns: ['id'], where: '"id" IS NOT NULL' })
export class Table1 {
@Column({ nullable: true })
column1!: string;
}
export const description = 'should create an index with a where clause';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'column1',
tableName: 'table1',
type: 'character varying',
nullable: true,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [
{
name: 'IDX_9f4e073964c0395f51f9b39900',
tableName: 'table1',
unique: false,
columnNames: ['id'],
where: '"id" IS NOT NULL',
synchronize: true,
},
],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,40 @@
import { DatabaseConstraintType, DatabaseSchema, PrimaryColumn, Table } from 'src/sql-tools';
@Table()
export class Table1 {
@PrimaryColumn({ type: 'uuid' })
id!: string;
}
export const description = 'should add a primary key constraint to the table with a default name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: true,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.PRIMARY_KEY,
name: 'PK_b249cc64cf63b8a22557cdc8537',
tableName: 'table1',
columnNames: ['id'],
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,40 @@
import { DatabaseConstraintType, DatabaseSchema, PrimaryColumn, Table } from 'src/sql-tools';
@Table({ primaryConstraintName: 'PK_test' })
export class Table1 {
@PrimaryColumn({ type: 'uuid' })
id!: string;
}
export const description = 'should add a primary key constraint to the table with a specific name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: true,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.PRIMARY_KEY,
name: 'PK_test',
tableName: 'table1',
columnNames: ['id'],
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,19 @@
import { DatabaseSchema, Table } from 'src/sql-tools';
@Table()
export class Table1 {}
export const description = 'should register a table with a default name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,19 @@
import { DatabaseSchema, Table } from 'src/sql-tools';
@Table({ name: 'table-1' })
export class Table1 {}
export const description = 'should register a table with a specific name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table-1',
columns: [],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,19 @@
import { DatabaseSchema, Table } from 'src/sql-tools';
@Table('table-1')
export class Table1 {}
export const description = 'should register a table with a specific name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table-1',
columns: [],
indexes: [],
constraints: [],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,41 @@
import { Column, DatabaseConstraintType, DatabaseSchema, Table, Unique } from 'src/sql-tools';
@Table()
@Unique({ columns: ['id'] })
export class Table1 {
@Column({ type: 'uuid' })
id!: string;
}
export const description = 'should add a unique constraint to the table with a default name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.UNIQUE,
name: 'UQ_b249cc64cf63b8a22557cdc8537',
tableName: 'table1',
columnNames: ['id'],
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -0,0 +1,41 @@
import { Column, DatabaseConstraintType, DatabaseSchema, Table, Unique } from 'src/sql-tools';
@Table()
@Unique({ name: 'UQ_test', columns: ['id'] })
export class Table1 {
@Column({ type: 'uuid' })
id!: string;
}
export const description = 'should add a unique constraint to the table with a specific name';
export const schema: DatabaseSchema = {
name: 'public',
tables: [
{
name: 'table1',
columns: [
{
name: 'id',
tableName: 'table1',
type: 'uuid',
nullable: false,
isArray: false,
primary: false,
synchronize: true,
},
],
indexes: [],
constraints: [
{
type: DatabaseConstraintType.UNIQUE,
name: 'UQ_test',
tableName: 'table1',
columnNames: ['id'],
synchronize: true,
},
],
synchronize: true,
},
],
warnings: [],
};

View File

@@ -12,12 +12,13 @@ export default defineConfig({
include: ['src/**/*.spec.ts'],
coverage: {
provider: 'v8',
include: ['src/cores/**', 'src/interfaces/**', 'src/services/**', 'src/utils/**'],
include: ['src/cores/**', 'src/interfaces/**', 'src/services/**', 'src/utils/**', 'src/sql-tools/**'],
exclude: [
'src/services/*.spec.ts',
'src/services/api.service.ts',
'src/services/microservices.service.ts',
'src/services/index.ts',
'src/sql-tools/schema-from-database.ts',
],
thresholds: {
lines: 85,