mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fixed down migrations generation, changed migration interface signature
This commit is contained in:
parent
9a1cc0d842
commit
580d70dcf2
@ -47,9 +47,10 @@ More env variable names you can find in `ConnectionOptionsEnvReader` class.
|
||||
* `skipSchemaSync` in entity options has been renamed to `skipSync`
|
||||
* `setLimit` and `setOffset` in `QueryBuilder` were renamed into `limit` and `offset`
|
||||
* `nativeInterface` has been removed from a driver interface and implementations.
|
||||
Now
|
||||
* now typeorm works with the latest version of mssql (version 4)
|
||||
* fixed how orm creates default values for SqlServer - now it creates constraints for it as well
|
||||
* migrations interface has changed - now `up` and `down` accept only `QueryRunner`. To use `Connection` and `EntityManager` use properties
|
||||
of `QueryRunner`, e.g. `queryRunner.connection` and `queryRunner.manager`
|
||||
|
||||
### DEPRECATIONS
|
||||
|
||||
@ -65,6 +66,7 @@ Now
|
||||
* now relations for multiple primary keys are generated properly
|
||||
* now ormconfig is read from `.env`, `.js`, `.json`, `.yml`, `.xml` formats
|
||||
* all database-specific types are supported now
|
||||
* now migrations generation is supported. Use `typeorm migrations:generate` command
|
||||
|
||||
### OTHER API CHANGES
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "typeorm",
|
||||
"private": true,
|
||||
"version": "0.1.0-alpha.18",
|
||||
"version": "0.1.0-alpha.19",
|
||||
"description": "Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, WebSQL, MongoDB databases.",
|
||||
"license": "MIT",
|
||||
"readmeFilename": "README.md",
|
||||
|
||||
@ -4,13 +4,13 @@ import {QueryRunner} from "../../../src/query-runner/QueryRunner";
|
||||
|
||||
export class FirstReleaseMigration1481283582 implements MigrationInterface {
|
||||
|
||||
async up(queryRunner: QueryRunner, connection: Connection): Promise<any> {
|
||||
async up(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.renameColumn("post", "title", "name");
|
||||
// alternatively you can do:
|
||||
// await queryRunner.query("ALTER TABLE `post` CHANGE `title` `name` VARCHAR(255)");
|
||||
}
|
||||
|
||||
async down(queryRunner: QueryRunner, connection: Connection): Promise<any> {
|
||||
async down(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.renameColumn("post", "name", "title");
|
||||
// alternatively you can do:
|
||||
// await queryRunner.query("ALTER TABLE `post` CHANGE `name` `title` VARCHAR(255)");
|
||||
|
||||
@ -4,11 +4,11 @@ import {QueryRunner} from "../../../src/query-runner/QueryRunner";
|
||||
|
||||
export class SecondReleaseMigration1481521933 implements MigrationInterface {
|
||||
|
||||
async up(queryRunner: QueryRunner, connection: Connection): Promise<any> {
|
||||
async up(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query("ALTER TABLE `post` CHANGE `name` `title` VARCHAR(500)");
|
||||
}
|
||||
|
||||
async down(queryRunner: QueryRunner, connection: Connection): Promise<any> {
|
||||
async down(queryRunner: QueryRunner): Promise<any> {
|
||||
await queryRunner.query("ALTER TABLE `post` CHANGE `title` `name` VARCHAR(255)");
|
||||
}
|
||||
|
||||
|
||||
@ -64,10 +64,10 @@ export class MigrationCreateCommand {
|
||||
|
||||
export class ${name}${timestamp} implements MigrationInterface {
|
||||
|
||||
public async up(queryRunner: QueryRunner, connection: Connection, entityManager?: EntityManager): Promise<any> {
|
||||
public async up(queryRunner: QueryRunner): Promise<any> {
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner, connection: Connection, entityManager?: EntityManager): Promise<any> {
|
||||
public async down(queryRunner: QueryRunner): Promise<any> {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -81,11 +81,11 @@ export class MigrationGenerateCommand {
|
||||
downSqls.push(" await queryRunner.query(`" + query.down.replace(new RegExp("`", "g"), "\\`") + "`);");
|
||||
});
|
||||
}
|
||||
const fileContent = MigrationGenerateCommand.getTemplate(argv.name, timestamp, upSqls, downSqls);
|
||||
const fileContent = MigrationGenerateCommand.getTemplate(argv.name, timestamp, upSqls, downSqls.reverse());
|
||||
const path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename;
|
||||
await CommandUtils.createFile(path, fileContent);
|
||||
|
||||
if (!upSqls.length) {
|
||||
if (upSqls.length) {
|
||||
console.log(`Migration ${path} has been generated successfully.`);
|
||||
} else {
|
||||
console.error(`No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migrations:create" command`);
|
||||
@ -112,12 +112,12 @@ export class MigrationGenerateCommand {
|
||||
|
||||
export class ${name}${timestamp} implements MigrationInterface {
|
||||
|
||||
public async up(queryRunner: QueryRunner, connection: Connection, entityManager?: EntityManager): Promise<any> {
|
||||
public async up(queryRunner: QueryRunner): Promise<any> {
|
||||
${upSqls.join(`
|
||||
`)}
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner, connection: Connection, entityManager?: EntityManager): Promise<any> {
|
||||
public async down(queryRunner: QueryRunner): Promise<any> {
|
||||
${downSqls.join(`
|
||||
`)}
|
||||
}
|
||||
|
||||
@ -408,10 +408,14 @@ export class Connection {
|
||||
* After finishing with entity manager, don't forget to release it (to release database connection back to pool).
|
||||
*/
|
||||
createIsolatedManager(queryRunner?: QueryRunner): EntityManager {
|
||||
if (queryRunner && queryRunner.manager && queryRunner.manager !== this.manager)
|
||||
return queryRunner.manager;
|
||||
|
||||
if (!queryRunner)
|
||||
queryRunner = this.createQueryRunner();
|
||||
|
||||
return new EntityManagerFactory().create(this, queryRunner);
|
||||
Object.assign(queryRunner, { manager: new EntityManagerFactory().create(this, queryRunner) });
|
||||
return queryRunner.manager;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -420,10 +424,7 @@ export class Connection {
|
||||
* After finishing with entity manager, don't forget to release it (to release database connection back to pool).
|
||||
*/
|
||||
createIsolatedRepository<Entity>(entityClassOrName: ObjectType<Entity>|string, queryRunner?: QueryRunner): Repository<Entity> {
|
||||
if (!queryRunner)
|
||||
queryRunner = this.createQueryRunner();
|
||||
|
||||
return new RepositoryFactory().createRepository(this, this.getMetadata(entityClassOrName), queryRunner);
|
||||
return this.createIsolatedManager(queryRunner).getRepository(entityClassOrName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -37,6 +37,7 @@ import {
|
||||
UpdateWriteOpResult
|
||||
} from "./typings";
|
||||
import {Connection} from "../../connection/Connection";
|
||||
import {EntityManager} from "../../entity-manager/EntityManager";
|
||||
|
||||
/**
|
||||
* Runs queries on a single MongoDB connection.
|
||||
@ -47,6 +48,16 @@ export class MongoQueryRunner implements QueryRunner {
|
||||
// Public Implemented Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Connection used by this query runner.
|
||||
*/
|
||||
connection: Connection;
|
||||
|
||||
/**
|
||||
* Entity manager isolated for this query runner.
|
||||
*/
|
||||
manager: EntityManager;
|
||||
|
||||
/**
|
||||
* Indicates if connection for this query runner is released.
|
||||
* Once its released, query runner cannot run queries anymore.
|
||||
@ -69,7 +80,9 @@ export class MongoQueryRunner implements QueryRunner {
|
||||
// Constructor
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(protected connection: Connection, databaseConnection: Db) {
|
||||
constructor(connection: Connection, databaseConnection: Db) {
|
||||
this.connection = connection;
|
||||
this.manager = connection.manager;
|
||||
this.databaseConnection = databaseConnection;
|
||||
}
|
||||
|
||||
|
||||
@ -10,6 +10,8 @@ import {PrimaryKeySchema} from "../../schema-builder/schema/PrimaryKeySchema";
|
||||
import {IndexSchema} from "../../schema-builder/schema/IndexSchema";
|
||||
import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRunnerAlreadyReleasedError";
|
||||
import {MysqlDriver} from "./MysqlDriver";
|
||||
import {Connection} from "../../connection/Connection";
|
||||
import {EntityManager} from "../../entity-manager/EntityManager";
|
||||
|
||||
/**
|
||||
* Runs queries on a single mysql database connection.
|
||||
@ -20,6 +22,16 @@ export class MysqlQueryRunner implements QueryRunner {
|
||||
// Public Implemented Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Connection used by this query runner.
|
||||
*/
|
||||
connection: Connection;
|
||||
|
||||
/**
|
||||
* Entity manager isolated for this query runner.
|
||||
*/
|
||||
manager: EntityManager;
|
||||
|
||||
/**
|
||||
* Indicates if connection for this query runner is released.
|
||||
* Once its released, query runner cannot run queries anymore.
|
||||
@ -60,6 +72,8 @@ export class MysqlQueryRunner implements QueryRunner {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(protected driver: MysqlDriver) {
|
||||
this.connection = driver.connection;
|
||||
this.manager = driver.connection.manager;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -10,6 +10,8 @@ import {PrimaryKeySchema} from "../../schema-builder/schema/PrimaryKeySchema";
|
||||
import {IndexSchema} from "../../schema-builder/schema/IndexSchema";
|
||||
import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRunnerAlreadyReleasedError";
|
||||
import {OracleDriver} from "./OracleDriver";
|
||||
import {EntityManager} from "../../entity-manager/EntityManager";
|
||||
import {Connection} from "../../connection/Connection";
|
||||
|
||||
/**
|
||||
* Runs queries on a single oracle database connection.
|
||||
@ -22,6 +24,16 @@ export class OracleQueryRunner implements QueryRunner {
|
||||
// Public Implemented Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Connection used by this query runner.
|
||||
*/
|
||||
connection: Connection;
|
||||
|
||||
/**
|
||||
* Entity manager isolated for this query runner.
|
||||
*/
|
||||
manager: EntityManager;
|
||||
|
||||
/**
|
||||
* Indicates if connection for this query runner is released.
|
||||
* Once its released, query runner cannot run queries anymore.
|
||||
@ -62,6 +74,8 @@ export class OracleQueryRunner implements QueryRunner {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(protected driver: OracleDriver) {
|
||||
this.connection = driver.connection;
|
||||
this.manager = driver.connection.manager;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -10,6 +10,8 @@ import {ForeignKeySchema} from "../../schema-builder/schema/ForeignKeySchema";
|
||||
import {PrimaryKeySchema} from "../../schema-builder/schema/PrimaryKeySchema";
|
||||
import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRunnerAlreadyReleasedError";
|
||||
import {PostgresDriver} from "./PostgresDriver";
|
||||
import {EntityManager} from "../../entity-manager/EntityManager";
|
||||
import {Connection} from "../../connection/Connection";
|
||||
|
||||
/**
|
||||
* Runs queries on a single postgres database connection.
|
||||
@ -20,6 +22,16 @@ export class PostgresQueryRunner implements QueryRunner {
|
||||
// Public Implemented Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Connection used by this query runner.
|
||||
*/
|
||||
connection: Connection;
|
||||
|
||||
/**
|
||||
* Entity manager isolated for this query runner.
|
||||
*/
|
||||
manager: EntityManager;
|
||||
|
||||
/**
|
||||
* Indicates if connection for this query runner is released.
|
||||
* Once its released, query runner cannot run queries anymore.
|
||||
@ -65,6 +77,8 @@ export class PostgresQueryRunner implements QueryRunner {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(protected driver: PostgresDriver) {
|
||||
this.connection = driver.connection;
|
||||
this.manager = driver.connection.manager;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -11,6 +11,8 @@ import {PrimaryKeySchema} from "../../schema-builder/schema/PrimaryKeySchema";
|
||||
import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRunnerAlreadyReleasedError";
|
||||
import {RandomGenerator} from "../../util/RandomGenerator";
|
||||
import {SqliteDriver} from "./SqliteDriver";
|
||||
import {EntityManager} from "../../entity-manager/EntityManager";
|
||||
import {Connection} from "../../connection/Connection";
|
||||
|
||||
/**
|
||||
* Runs queries on a single sqlite database connection.
|
||||
@ -24,6 +26,16 @@ export class SqliteQueryRunner implements QueryRunner {
|
||||
// Public Implemented Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Connection used by this query runner.
|
||||
*/
|
||||
connection: Connection;
|
||||
|
||||
/**
|
||||
* Entity manager isolated for this query runner.
|
||||
*/
|
||||
manager: EntityManager;
|
||||
|
||||
/**
|
||||
* Indicates if connection for this query runner is released.
|
||||
* Once its released, query runner cannot run queries anymore.
|
||||
@ -54,6 +66,8 @@ export class SqliteQueryRunner implements QueryRunner {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(protected driver: SqliteDriver) {
|
||||
this.connection = driver.connection;
|
||||
this.manager = driver.connection.manager;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -10,6 +10,8 @@ import {PrimaryKeySchema} from "../../schema-builder/schema/PrimaryKeySchema";
|
||||
import {IndexSchema} from "../../schema-builder/schema/IndexSchema";
|
||||
import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRunnerAlreadyReleasedError";
|
||||
import {SqlServerDriver} from "./SqlServerDriver";
|
||||
import {EntityManager} from "../../entity-manager/EntityManager";
|
||||
import {Connection} from "../../connection/Connection";
|
||||
|
||||
/**
|
||||
* Runs queries on a single mysql database connection.
|
||||
@ -20,6 +22,16 @@ export class SqlServerQueryRunner implements QueryRunner {
|
||||
// Public Implemented Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Connection used by this query runner.
|
||||
*/
|
||||
connection: Connection;
|
||||
|
||||
/**
|
||||
* Entity manager isolated for this query runner.
|
||||
*/
|
||||
manager: EntityManager;
|
||||
|
||||
/**
|
||||
* Indicates if connection for this query runner is released.
|
||||
* Once its released, query runner cannot run queries anymore.
|
||||
@ -64,6 +76,8 @@ export class SqlServerQueryRunner implements QueryRunner {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(protected driver: SqlServerDriver) {
|
||||
this.connection = driver.connection;
|
||||
this.manager = driver.connection.manager;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -9,6 +9,8 @@ import {ForeignKeySchema} from "../../schema-builder/schema/ForeignKeySchema";
|
||||
import {IndexSchema} from "../../schema-builder/schema/IndexSchema";
|
||||
import {QueryRunnerAlreadyReleasedError} from "../../query-runner/error/QueryRunnerAlreadyReleasedError";
|
||||
import {WebsqlDriver} from "./WebsqlDriver";
|
||||
import {Connection} from "../../connection/Connection";
|
||||
import {EntityManager} from "../../entity-manager/EntityManager";
|
||||
|
||||
/**
|
||||
* Declare a global function that is only available in browsers that support WebSQL.
|
||||
@ -24,6 +26,16 @@ export class WebsqlQueryRunner implements QueryRunner {
|
||||
// Public Implemented Properties
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Connection used by this query runner.
|
||||
*/
|
||||
connection: Connection;
|
||||
|
||||
/**
|
||||
* Entity manager isolated for this query runner.
|
||||
*/
|
||||
manager: EntityManager;
|
||||
|
||||
/**
|
||||
* Indicates if connection for this query runner is released.
|
||||
* Once its released, query runner cannot run queries anymore.
|
||||
@ -64,6 +76,8 @@ export class WebsqlQueryRunner implements QueryRunner {
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
constructor(protected driver: WebsqlDriver) {
|
||||
this.connection = driver.connection;
|
||||
this.manager = driver.connection.manager;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -86,7 +86,7 @@ export class MigrationExecutor {
|
||||
// run all pending migrations in a sequence
|
||||
try {
|
||||
await PromiseUtils.runInSequence(pendingMigrations, migration => {
|
||||
return migration.instance!.up(this.queryRunner, this.connection, entityManager)
|
||||
return migration.instance!.up(this.queryRunner)
|
||||
.then(() => { // now when migration is executed we need to insert record about it into the database
|
||||
return this.insertExecutedMigration(migration);
|
||||
})
|
||||
@ -155,7 +155,7 @@ export class MigrationExecutor {
|
||||
}
|
||||
|
||||
try {
|
||||
await migrationToRevert.instance!.down(this.queryRunner, this.connection, entityManager);
|
||||
await migrationToRevert.instance!.down(this.queryRunner);
|
||||
await this.deleteExecutedMigration(migrationToRevert);
|
||||
this.connection.logger.log("info", `Migration ${migrationToRevert.name} has been reverted successfully.`);
|
||||
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
import {Connection} from "../connection/Connection";
|
||||
import {QueryRunner} from "../query-runner/QueryRunner";
|
||||
import {EntityManager} from "../entity-manager/EntityManager";
|
||||
|
||||
/**
|
||||
* Migrations should implement this interface and all its methods.
|
||||
@ -10,11 +8,11 @@ export interface MigrationInterface {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
up(queryRunner: QueryRunner, connection: Connection, entityManager?: EntityManager): Promise<any>;
|
||||
up(queryRunner: QueryRunner): Promise<any>;
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
down(queryRunner: QueryRunner, connection: Connection, entityManager?: EntityManager): Promise<any>;
|
||||
down(queryRunner: QueryRunner): Promise<any>;
|
||||
|
||||
}
|
||||
@ -3,6 +3,8 @@ import {ColumnMetadata} from "../metadata/ColumnMetadata";
|
||||
import {TableSchema} from "../schema-builder/schema/TableSchema";
|
||||
import {ForeignKeySchema} from "../schema-builder/schema/ForeignKeySchema";
|
||||
import {IndexSchema} from "../schema-builder/schema/IndexSchema";
|
||||
import {Connection} from "../connection/Connection";
|
||||
import {EntityManager} from "../entity-manager/EntityManager";
|
||||
|
||||
/**
|
||||
* Runs queries on a single database connection.
|
||||
@ -11,6 +13,16 @@ import {IndexSchema} from "../schema-builder/schema/IndexSchema";
|
||||
*/
|
||||
export interface QueryRunner {
|
||||
|
||||
/**
|
||||
* Connection used by this query runner.
|
||||
*/
|
||||
readonly connection: Connection;
|
||||
|
||||
/**
|
||||
* Entity manager isolated for this query runner.
|
||||
*/
|
||||
readonly manager: EntityManager;
|
||||
|
||||
/**
|
||||
* Indicates if connection for this query runner is released.
|
||||
* Once its released, query runner cannot run queries anymore.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user