Skip to content

Commit

Permalink
fix: change user login to unique (#130)
Browse files Browse the repository at this point in the history
* fix: change user login to unique

* fix: add unique on schema
  • Loading branch information
danilolutz authored Jan 20, 2022
1 parent ffb4343 commit b2bf5a7
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 59 deletions.
127 changes: 69 additions & 58 deletions src/database/migration/1637774086085-create_user.ts
Original file line number Diff line number Diff line change
@@ -1,65 +1,76 @@
import {MigrationInterface, QueryRunner, Table, TableForeignKey} from "typeorm";
import {
MigrationInterface,
QueryRunner,
Table,
TableForeignKey,
} from 'typeorm';

export class createUser1637774086085 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: 'user',
columns: [
{
name: 'id',
type: 'int',
isPrimary: true,
},
{
name: 'name',
type: 'varchar',
},
{
name: 'login',
type: 'varchar',
isUnique: true,
},
{
name: 'password',
type: 'varchar',
isNullable: true,
},
{
name: 'language',
type: 'varchar',
},
{
name: 'notes',
type: 'varchar',
isNullable: true,
},
{
name: 'document',
type: 'varchar',
},
{
name: 'userTypeId',
type: 'int',
},
],
}),
true
);

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(new Table({
name: "user",
columns: [
{
name: "id",
type: "int",
isPrimary: true
},
{
name: "name",
type: "varchar",
},
{
name: "login",
type: "varchar",
isNullable: true,
},
{
name: "password",
type: "varchar",
isNullable: true,
},
{
name: "language",
type: "varchar",
},
{
name: "notes",
type: "varchar",
isNullable: true,
},
{
name: "document",
type: "varchar",
},
{
name: "userTypeId",
type: "int",
},
]
}), true)
await queryRunner.createForeignKey(
'user',
new TableForeignKey({
columnNames: ['userTypeId'],
referencedColumnNames: ['id'],
referencedTableName: 'user_type',
onDelete: 'CASCADE',
})
);
}

await queryRunner.createForeignKey("user", new TableForeignKey({
columnNames: ["userTypeId"],
referencedColumnNames: ["id"],
referencedTableName: "user_type",
onDelete: "CASCADE"
}));
}
public async down(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable('user');

public async down(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable("user");

const userTypeId = table.foreignKeys.find(fk => fk.columnNames.indexOf("userTypeId") !== -1);
await queryRunner.dropForeignKey("user", userTypeId);

await queryRunner.dropTable("user");
}
const userTypeId = table.foreignKeys.find(
(fk) => fk.columnNames.indexOf('userTypeId') !== -1
);
await queryRunner.dropForeignKey('user', userTypeId);

await queryRunner.dropTable('user');
}
}
2 changes: 1 addition & 1 deletion src/electron/database/models/User.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export class User {
@Column()
name: string;

@Column({nullable: true})
@Column({unique: true})
login: string;

@Column({nullable: true})
Expand Down

0 comments on commit b2bf5a7

Please sign in to comment.