Skip to content

Commit

Permalink
Merge pull request #498 from fey/improve-migrations
Browse files Browse the repository at this point in the history
fix snippets with null
  • Loading branch information
fey authored Jun 6, 2024
2 parents 846d79e + 2c8501b commit 351adf0
Show file tree
Hide file tree
Showing 29 changed files with 245 additions and 402 deletions.
3 changes: 3 additions & 0 deletions backend/.eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ settings:
- .ts

rules:
class-methods-use-this:
- 2
- exceptMethods: ["up", "down"]
import/no-cycle: [2, { maxDepth: 1 }]
import/prefer-default-export: 0
import/extensions: 0
Expand Down
15 changes: 13 additions & 2 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,21 @@ install:
npm install

db-migrate:
npm run typeorm -- migration:run -d src/data-source
npm run typeorm -- migration:run -d src/data-source.ts

db-generate:
npm run typeorm -- migration:generate src/migrations/migrations -d src/data-source
npm run typeorm -- migration:generate src/migrations -d src/data-source

db-revert:
npm run typeorm -- migration:revert -d src/data-source.ts

db-refresh: db-revert db-migrate

db-create-migration:
npm run typeorm migration:create src/migrations/$(NAME)

db-drop:
npm run typeorm -- schema:drop -d src/data-source.ts

start:
npm run start
Expand Down
8 changes: 4 additions & 4 deletions backend/__tests__/auth.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import { Repository } from 'typeorm';
import { ValidationPipe } from '@nestjs/common';
import * as cookieParser from 'cookie-parser';
import { JwtService } from '@nestjs/jwt';
import { Users } from '../src/entities/user.entity';
import { User } from '../src/entities/user.entity';
import { AuthModule } from '../src/auth/auth.module';
import getDataSourceConfig from '../src/config/data-source.config';
import { AppModule } from '../src/app.module';
import { UsersModule } from '../src/users/users.module';

describe('AuthController (e2e)', () => {
let app: NestExpressApplication;
let usersRepo: Repository<Users>;
let usersRepo: Repository<User>;
let testData: Record<string, any>;
let users: Array<Record<string, unknown>>;
let moduleFixture: TestingModule;
let usersData: Users[];
let usersData: User[];
let jwtService: JwtService;

// FIXME: все тесты проходят если beforeEach
Expand All @@ -32,7 +32,7 @@ describe('AuthController (e2e)', () => {
UsersModule,
AuthModule,
TypeOrmModule.forRoot(getDataSourceConfig()),
TypeOrmModule.forFeature([Users]),
TypeOrmModule.forFeature([User]),
],
}).compile();

Expand Down
14 changes: 7 additions & 7 deletions backend/__tests__/snippets.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import { AuthModule } from '../src/auth/auth.module';
import { AppService } from '../src/app.service';
import { UsersService } from '../src/users/users.service';
import { AppModule } from '../src/app.module';
import { Users } from '../src/entities/user.entity';
import { Snippets } from '../src/entities/snippet.entity';
import { User } from '../src/entities/user.entity';
import { Snippet } from '../src/entities/snippet.entity';
import getDataSourceConfig from '../src/config/data-source.config';

describe('SnippetsController (e2e)', () => {
let app: NestExpressApplication;
let usersRepo: Repository<Users>;
let snippetsRepo: Repository<Snippets>;
let usersRepo: Repository<User>;
let snippetsRepo: Repository<Snippet>;
let testData: Record<string, any>;
let users: Array<Record<string, unknown>>;
let snippets: Array<Record<string, unknown>>;
let moduleFixture: TestingModule;
let usersData: Users[];
let snippetsData: Snippets[];
let usersData: User[];
let snippetsData: Snippet[];
let jwtService: JwtService;
let token: string;

Expand All @@ -36,7 +36,7 @@ describe('SnippetsController (e2e)', () => {
UsersModule,
AuthModule,
TypeOrmModule.forRoot(getDataSourceConfig()),
TypeOrmModule.forFeature([Users, Snippets]),
TypeOrmModule.forFeature([User, Snippet]),
],
providers: [AppService, UsersService],
}).compile();
Expand Down
14 changes: 7 additions & 7 deletions backend/__tests__/users.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ import { AppService } from '../src/app.service';
import { UsersService } from '../src/users/users.service';
import { CheckEmail } from '../src/users/validation/check-email';
import { AppModule } from '../src/app.module';
import { Users } from '../src/entities/user.entity';
import { Snippets } from '../src/entities/snippet.entity';
import { User } from '../src/entities/user.entity';
import { Snippet } from '../src/entities/snippet.entity';
import getDataSourceConfig from '../src/config/data-source.config';

describe('UsersController (e2e)', () => {
let app: NestExpressApplication;
let usersRepo: Repository<Users>;
let snippetsRepo: Repository<Snippets>;
let usersRepo: Repository<User>;
let snippetsRepo: Repository<Snippet>;
let testData: Record<string, any>;
let users: Array<Record<string, unknown>>;
let snippets: Array<Record<string, unknown>>;
let moduleFixture: TestingModule;
let usersData: Users[];
let snippetsData: Snippets[];
let usersData: User[];
let snippetsData: Snippet[];
let jwtService: JwtService;
let token: string;

Expand All @@ -38,7 +38,7 @@ describe('UsersController (e2e)', () => {
UsersModule,
AuthModule,
TypeOrmModule.forRoot(getDataSourceConfig()),
TypeOrmModule.forFeature([Users, Snippets]),
TypeOrmModule.forFeature([User, Snippet]),
],
providers: [AppService, UsersService, CheckEmail],
}).compile();
Expand Down
40 changes: 7 additions & 33 deletions backend/src/config/data-source.config.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,35 @@
import { DataSourceOptions } from 'typeorm';
import { Users } from '../entities/user.entity';
import { Snippets } from '../entities/snippet.entity';
import { migration1663236009774 } from '../migrations/1663236009774-migration';
import { migration1670352324202 } from '../migrations/1670352324202-migration';
import { AdduserRecoverHash1677580680097 } from '../migrations/1677580680097-AdduserRecoverHash';
import { FillNullSlugsSnippets1682678760453 } from '../migrations/1682678760453-fill-null-slugs-snippets';
import { RenameLoginToUsername1691073864288 } from '../migrations/1691073864288-RenameLoginToUsername';
import { Migrations1699462100238 } from '../migrations/1699462100238-migrations';

export default (): DataSourceOptions => {
const defaultConfig = {
entities: [`${__dirname}/../entities/**/*{.ts,.js}`],
migrations: [`${__dirname}/../migrations/**/*{.ts,.js}`],
};
switch (process.env.NODE_ENV) {
case 'production':
return {
...defaultConfig,
type: 'postgres',
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
port: Number(process.env.DATABASE_PORT),
url: process.env.DATABASE_URL,
synchronize: false,
ssl: { rejectUnauthorized: false },
entities: [Users, Snippets],
migrations: [
migration1663236009774,
migration1670352324202,
AdduserRecoverHash1677580680097,
FillNullSlugsSnippets1682678760453,
RenameLoginToUsername1691073864288,
Migrations1699462100238,
],
};
case 'test':
return {
...defaultConfig,
type: 'sqlite',
database: ':memory:',
synchronize: true,
entities: [Users, Snippets],
migrations: [
migration1663236009774,
migration1670352324202,
AdduserRecoverHash1677580680097,
RenameLoginToUsername1691073864288,
Migrations1699462100238,
],
};
default:
return {
...defaultConfig,
type: 'sqlite',
database: 'runit.sqlite',
synchronize: false,
entities: [Users, Snippets],
migrations: [
migration1663236009774,
migration1670352324202,
AdduserRecoverHash1677580680097,
RenameLoginToUsername1691073864288,
Migrations1699462100238,
],
};
}
};
10 changes: 5 additions & 5 deletions backend/src/entities/snippet.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import type { Users } from './user.entity';
import type { User } from './user.entity';

@Entity()
export class Snippets {
@Entity('snippets')
export class Snippet {
@PrimaryGeneratedColumn()
id: number;

Expand All @@ -26,9 +26,9 @@ export class Snippets {
@Column('text')
language: string;

@ManyToOne('Users', 'snippets', { onDelete: 'CASCADE' })
@ManyToOne('User', 'snippets')
@JoinColumn()
user: Users;
user: User;

@CreateDateColumn()
created_at: string;
Expand Down
10 changes: 5 additions & 5 deletions backend/src/entities/user.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
UpdateDateColumn,
} from 'typeorm';
import { encrypt } from '../users/secure/encrypt';
import type { Snippets } from './snippet.entity';
import type { Snippet } from './snippet.entity';

@Entity()
export class Users {
@Entity('users')
export class User {
@PrimaryGeneratedColumn()
id: number;

Expand All @@ -28,8 +28,8 @@ export class Users {
@Column('text')
password: string;

@OneToMany('Snippets', 'user')
snippets: Snippets[];
@OneToMany('Snippet', 'user')
snippets: Snippet[];

@Column({ nullable: true })
recover_hash: string;
Expand Down
111 changes: 0 additions & 111 deletions backend/src/migrations/1663236009774-migration.ts

This file was deleted.

16 changes: 0 additions & 16 deletions backend/src/migrations/1670352324202-migration.ts

This file was deleted.

Loading

0 comments on commit 351adf0

Please sign in to comment.