remove migration, update typeorm

This commit is contained in:
einhorn_b 2021-09-23 16:44:55 +02:00
parent 1952844c80
commit c9e04d1e6f
4 changed files with 4717 additions and 60 deletions

4712
backend/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -23,7 +23,7 @@ import { isAuthorized } from './auth/auth'
// TODO implement
// import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
const DB_VERSION = '0002-update_user'
const DB_VERSION = '0001-init_db'
const context = (args: any) => {
const authorization = args.req.headers.authorization

View File

@ -1,7 +1,7 @@
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
// import { Group } from "./Group"
@Entity()
@Entity('state_users')
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number
@ -9,16 +9,16 @@ export class User extends BaseEntity {
// @ManyToOne(type => Group, group => group.users)
// group: Group;
@Column({ type: 'binary', length: 32 })
@Column({ type: 'binary', length: 32, name: 'public_key' })
pubkey: Buffer
@Column()
email: string
@Column()
@Column({ name: 'first_name' })
firstName: string
@Column()
@Column({ name: 'last_name' })
lastName: string
@Column()

View File

@ -1,55 +0,0 @@
/* FIRST MIGRATION
*
* This migration is special since it takes into account that
* the database can be setup already but also may not be.
* Therefore you will find all `CREATE TABLE` statements with
* a `IF NOT EXISTS`, all `INSERT` with an `IGNORE` and in the
* downgrade function all `DROP TABLE` with a `IF EXISTS`.
* This ensures compatibility for existing or non-existing
* databases.
*/
export async function upgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
// write upgrade logic as parameter of queryFn
// rename table
await queryFn(`
ALTER TABLE state_users
RENAME TO user;
`)
// drop not used columns
await queryFn(`
ALTER TABLE user
DROP COLUMN index_id;
`)
// rename from snake case to camel case (cakePHP standard to typeorm standard)
await queryFn(`
ALTER TABLE user
CHANGE COLUMN group_id groupId int(10) unsigned NOT NULL DEFAULT '0',
CHANGE COLUMN public_key pubkey binary(32) NOT NULL,
CHANGE COLUMN first_name firstName varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
CHANGE COLUMN last_name lastName varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL;
`)
}
export async function downgrade(queryFn: (query: string, values?: any[]) => Promise<Array<any>>) {
// write downgrade logic as parameter of queryFn
// rename table
await queryFn(`
ALTER TABLE user
RENAME TO state_users;
`)
// put back dropped column
await queryFn(`
ALTER TABLE state_users
ADD index_id smallint(6) NOT NULL DEFAULT '0';
`)
// rename from camel case to snake case (typeorm standard to cakePHP standard)
await queryFn(`
ALTER TABLE state_users
CHANGE COLUMN groupId group_id int(10) unsigned NOT NULL DEFAULT '0',
CHANGE COLUMN pubkey public_key binary(32) NOT NULL,
CHANGE COLUMN firstName first_name varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
CHANGE COLUMN lastName last_name varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL;
`)
}