Database migration is the process of migrating data from one or more source databases to one or more target databases by using a database migration service. When a migration is finished, the dataset in the source databases resides fully, though possibly restructured, in the target databases.
Create a new entity file, eg. ProductAttribute.ts
@ObjectType()
@Entity()
export class ProductAttribute extends BaseEntity {
@Field()
@PrimaryGeneratedColumn("uuid")
id: string;
@Field()
@Column("varchar", { length: 75 })
title: string;
@Field()
@Column({
type: "enum",
enum: product_attribute,
})
value: product_attribute;
}
Generate a new migration file by running the following command:
npx typeorm migration:generate -n CreateProductAttributeTable
A new {Timestamps}-CreateProductAttributeTable
file was generated in current directory level or if you specify its location by setting cli.migrationsDir
to whatever you want in ormconfig.json
:
{
"type": "postgres",
"host": "localhost",
"port": 5432,
"username": "postgres",
"password": "postgres",
"database": "database",
"entities": ["dist/entities/**/*{.ts,.js}"],
"migrations": ["dist/migrations/**/*{.ts,.js}"],
"seeds": ["dist/seeds/**/*{.ts,.js}"],
"factories": ["dist/factories/**/*{.ts,.js}"],
"cli": {
"entitiesDir": "src/entities",
"migrationsDir": "src/migrations"
}
}
Then run yarn build
to build new dist
And finally, run npx typeorm migration:run
Note: set synchronize
to false
in createConnection