diff --git a/gulpfile.ts b/gulpfile.ts index 0cfc27f90..5a83004fa 100644 --- a/gulpfile.ts +++ b/gulpfile.ts @@ -179,7 +179,26 @@ export class Gulpfile { chai.use(require("chai-as-promised")); return gulp.src(["./build/es5/test/**/*.js"]) + // .on("end", function() { + // process.env["connection_setup_name"] = "mysql"; + // console.log("Running tests on mysql platform..."); + // }) + // .pipe(mocha()) + // .on("end", function() { + // process.env["connection_setup_name"] = "mariadb"; + // console.log("Running tests on mariadb platform..."); + // }) + // .pipe(mocha()) + .on("end", function() { + process.env["connection_setup_name"] = "postgres"; + console.log("Running tests on postgres platform..."); + }) .pipe(mocha()) + // .on("end", function() { + // process.env["connection_setup_name"] = "sqlite"; + // console.log("Running tests on sqlite platform..."); + // }) + // .pipe(mocha()) .pipe(istanbul.writeReports()); } diff --git a/package.json b/package.json index 94995401b..5ebd4e76a 100644 --- a/package.json +++ b/package.json @@ -46,7 +46,7 @@ "mssql": "^3.3.0", "mysql": "^2.11.1", "oracledb": "^1.11.0", - "pg": "^6.0.3", + "pg": "^6.1.0", "remap-istanbul": "^0.6.4", "sinon": "^1.17.5", "sinon-chai": "^2.8.0", diff --git a/src/driver/postgres/PostgresQueryRunner.ts b/src/driver/postgres/PostgresQueryRunner.ts index 2e2c6c86d..73f7d7748 100644 --- a/src/driver/postgres/PostgresQueryRunner.ts +++ b/src/driver/postgres/PostgresQueryRunner.ts @@ -371,7 +371,7 @@ export class PostgresQueryRunner implements QueryRunner { // update sequence generation if (oldColumn.isGenerated !== newColumn.isGenerated) { if (!oldColumn.isGenerated) { - await this.query(`CREATE SEQUENCE "${tableSchema.name}_id_seq" OWNED BY ${tableSchema.name}.${oldColumn.name}`); + await this.query(`CREATE SEQUENCE "${tableSchema.name}_id_seq" OWNED BY "${tableSchema.name}"."${oldColumn.name}"`); await this.query(`ALTER TABLE "${tableSchema.name}" ALTER COLUMN "${oldColumn.name}" SET DEFAULT nextval('"${tableSchema.name}_id_seq"')`); } else { await this.query(`ALTER TABLE "${tableSchema.name}" ALTER COLUMN "${oldColumn.name}" DROP DEFAULT`); diff --git a/src/metadata-builder/EntityMetadataBuilder.ts b/src/metadata-builder/EntityMetadataBuilder.ts index 937dd3ab2..4fc9290cf 100644 --- a/src/metadata-builder/EntityMetadataBuilder.ts +++ b/src/metadata-builder/EntityMetadataBuilder.ts @@ -266,7 +266,7 @@ export class EntityMetadataBuilder { if (relationCountMetadata) relation.countField = relationCountMetadata.propertyName; }); - + // add lazy initializer for entity relations if (entityMetadata.target instanceof Function) { entityMetadata.relations diff --git a/src/metadata/EntityMetadata.ts b/src/metadata/EntityMetadata.ts index 0d2279c57..630e0d3ae 100644 --- a/src/metadata/EntityMetadata.ts +++ b/src/metadata/EntityMetadata.ts @@ -343,6 +343,7 @@ export class EntityMetadata { this.relations .filter(relation => relation.isLazy) .forEach(relation => this.lazyRelationsWrapper.wrap(newObject, relation)); + return newObject; } diff --git a/test/functional/connection-manager/connection-manager.ts b/test/functional/connection-manager/connection-manager.ts index 5e7baa28e..3dd4d70eb 100644 --- a/test/functional/connection-manager/connection-manager.ts +++ b/test/functional/connection-manager/connection-manager.ts @@ -65,6 +65,7 @@ describe("ConnectionManager", () => { connection.name.should.be.equal("default"); connection.driver.should.be.instanceOf(MysqlDriver); connection.isConnected.should.be.true; + await connection.close(); }); it("should create a postgres connection when postgres driver is specified AND connect to it", async () => { @@ -77,6 +78,7 @@ describe("ConnectionManager", () => { connection.name.should.be.equal("myPostgresConnection"); connection.driver.should.be.instanceOf(PostgresDriver); connection.isConnected.should.be.true; + await connection.close(); }); }); @@ -128,6 +130,7 @@ describe("ConnectionManager", () => { const loadedPost = await connection.entityManager.findOneById(Post, 1); loadedPost.should.be.instanceof(Post); loadedPost.should.be.eql({ id: 1, title: "Hello post" }); + await connection.close(); }); it("should drop the database if dropSchemaOnConnection was set to true (mysql)", async () => { @@ -149,6 +152,7 @@ describe("ConnectionManager", () => { connection = await connectionManager.createAndConnect(options); const loadedPost = await connection.entityManager.findOneById(Post, 1); expect(loadedPost).to.be.undefined; + await connection.close(); }); it("should drop the database if dropSchemaOnConnection was set to true (postgres)", async () => { @@ -170,6 +174,8 @@ describe("ConnectionManager", () => { connection = await connectionManager.createAndConnect(options); const loadedPost = await connection.entityManager.findOneById(Post, 1); expect(loadedPost).to.be.undefined; + + await connection.close(); }); it("should drop the database if dropSchemaOnConnection was set to true (postgres)", async () => { @@ -191,6 +197,7 @@ describe("ConnectionManager", () => { connection = await connectionManager.createAndConnect(options); const loadedPost = await connection.entityManager.findOneById(Post, 1); expect(loadedPost).to.be.undefined; + await connection.close(); }); }); diff --git a/test/functional/lazy-relations/lazy-relations.ts b/test/functional/lazy-relations/lazy-relations.ts index b11cd2767..c21893b34 100644 --- a/test/functional/lazy-relations/lazy-relations.ts +++ b/test/functional/lazy-relations/lazy-relations.ts @@ -4,13 +4,27 @@ import {Connection} from "../../../src/connection/Connection"; import {Post} from "./entity/Post"; import {Category} from "./entity/Category"; +/** + * Because lazy relations are overriding prototype is impossible to run these tests on multiple connections. + * So we run tests only for mysql. + */ describe("lazy-relations", () => { const resourceDir = __dirname + "/../../../../../test/functional/lazy-relations/"; const userSchema = require(resourceDir + "schema/user.json"); const profileSchema = require(resourceDir + "schema/profile.json"); let connections: Connection[]; - before(() => setupTestingConnections({ entities: [Post, Category], entitySchemas: [userSchema, profileSchema], schemaCreate: true }).then(all => connections = all)); + before(async () => connections = await setupTestingConnections({ + entities: [Post, Category], + entitySchemas: [userSchema, profileSchema], + schemaCreate: true, + dropSchemaOnConnection: true, + skipPostgres: true, + skipMariadb: true, + skipSqlite: true, + skipOracle: true, + skipSqlServer: true, + })); beforeEach(() => reloadDatabases(connections)); after(() => closeConnections(connections)); diff --git a/test/functional/repository/basic-methods/repository-basic-methods.ts b/test/functional/repository/basic-methods/repository-basic-methods.ts index 7d63abad4..a155d8b6a 100644 --- a/test/functional/repository/basic-methods/repository-basic-methods.ts +++ b/test/functional/repository/basic-methods/repository-basic-methods.ts @@ -15,7 +15,12 @@ describe("repository > basic methods", () => { const userSchema = require(resourceDir + "schema/user.json"); let connections: Connection[]; - before(() => setupTestingConnections({ entities: [Post, Blog, Category], entitySchemas: [userSchema, questionSchema], reloadAndCreateSchema: true }).then(all => connections = all)); + before(async () => connections = await setupTestingConnections({ + entities: [Post, Blog, Category], + entitySchemas: [userSchema, questionSchema], + schemaCreate: true, + dropSchemaOnConnection: true + })); beforeEach(() => reloadDatabases(connections)); after(() => closeConnections(connections)); @@ -349,7 +354,7 @@ describe("repository > basic methods", () => { it("should execute the query natively and it should return the result", () => Promise.all(connections.map(async connection => { const repository = connection.getRepository(Blog); const promises: Promise[] = []; - for (let i = 0; i < 100; i++) { + for (let i = 0; i < 50; i++) { const blog = new Blog(); blog.title = "hello blog"; blog.text = "hello blog #" + i; diff --git a/test/functional/repository/find-methods/repostiory-find-methods.ts b/test/functional/repository/find-methods/repostiory-find-methods.ts index 2561d1efa..9ef5d5fce 100644 --- a/test/functional/repository/find-methods/repostiory-find-methods.ts +++ b/test/functional/repository/find-methods/repostiory-find-methods.ts @@ -11,14 +11,14 @@ describe("repository > find methods", () => { const userSchema = require(resourceDir + "schema/user.json"); let connections: Connection[]; - beforeEach(() => setupTestingConnections({ + before(() => setupTestingConnections({ entities: [Post], entitySchemas: [userSchema], - reloadAndCreateSchema: true, - skipPostgres: true // TODO: fix it, right now postgres is failing for some reason when pooling enabled + schemaCreate: true, + dropSchemaOnConnection: true }).then(all => connections = all)); beforeEach(() => reloadDatabases(connections)); - afterEach(() => closeConnections(connections)); + after(() => closeConnections(connections)); describe("find and findAndCount", function() { @@ -37,7 +37,7 @@ describe("repository > find methods", () => { savedPosts.length.should.be.equal(100); // check if they all are saved // check find method - const loadedPosts = await postRepository.find(); + const loadedPosts = await postRepository.find({ alias: "post", orderBy: [{ sort: "post.id" }]}); loadedPosts.should.be.instanceOf(Array); loadedPosts.length.should.be.equal(100); loadedPosts[0].id.should.be.equal(0); @@ -46,7 +46,7 @@ describe("repository > find methods", () => { loadedPosts[99].title.should.be.equal("post #99"); // check findAndCount method - let [loadedPosts2, count] = await postRepository.findAndCount(); + let [loadedPosts2, count] = await postRepository.findAndCount({ alias: "post", orderBy: [{ sort: "post.id" }]}); count.should.be.equal(100); loadedPosts2.should.be.instanceOf(Array); loadedPosts2.length.should.be.equal(100); @@ -71,7 +71,7 @@ describe("repository > find methods", () => { savedPosts.length.should.be.equal(100); // check if they all are saved // check find method - const loadedPosts = await postRepository.find({ categoryName: "odd" }); + const loadedPosts = await postRepository.find({ categoryName: "odd" }, { alias: "post", orderBy: [{ sort: "post.id" }]}); loadedPosts.should.be.instanceOf(Array); loadedPosts.length.should.be.equal(50); loadedPosts[0].id.should.be.equal(1); @@ -80,7 +80,7 @@ describe("repository > find methods", () => { loadedPosts[49].title.should.be.equal("post #99"); // check findAndCount method - let [loadedPosts2, count] = await postRepository.findAndCount({ categoryName: "odd" }); + let [loadedPosts2, count] = await postRepository.findAndCount({ categoryName: "odd" }, { alias: "post", orderBy: [{ sort: "post.id" }]}); count.should.be.equal(50); loadedPosts2.should.be.instanceOf(Array); loadedPosts2.length.should.be.equal(50); @@ -106,7 +106,7 @@ describe("repository > find methods", () => { savedPosts.length.should.be.equal(100); // check if they all are saved // check find method - const loadedPosts = await postRepository.find({ categoryName: "odd", isNew: true }); + const loadedPosts = await postRepository.find({ categoryName: "odd", isNew: true }, { alias: "post", orderBy: [{ sort: "post.id" }]}); loadedPosts.should.be.instanceOf(Array); loadedPosts.length.should.be.equal(5); loadedPosts[0].id.should.be.equal(91); @@ -115,7 +115,7 @@ describe("repository > find methods", () => { loadedPosts[4].title.should.be.equal("post #99"); // check findAndCount method - let [loadedPosts2, count] = await postRepository.findAndCount({ categoryName: "odd", isNew: true }); + let [loadedPosts2, count] = await postRepository.findAndCount({ categoryName: "odd", isNew: true }, { alias: "post", orderBy: [{ sort: "post.id" }]}); count.should.be.equal(5); loadedPosts2.should.be.instanceOf(Array); loadedPosts2.length.should.be.equal(5); @@ -146,7 +146,10 @@ describe("repository > find methods", () => { parameters: { likeTitle: "new post #%", categoryName: "even" - } + }, + orderBy: [ + { sort: "post.id" } + ] }; // check find method @@ -232,7 +235,7 @@ describe("repository > find methods", () => { const savedUsers = await Promise.all(promises); savedUsers.length.should.be.equal(100); // check if they all are saved - const loadedUser = await userRepository.findOne(); + const loadedUser = await userRepository.findOne({ alias: "user", orderBy: [{ sort: "user.id" }]}); loadedUser.id.should.be.equal(0); loadedUser.firstName.should.be.equal("name #0"); loadedUser.secondName.should.be.equal("Doe"); @@ -253,7 +256,7 @@ describe("repository > find methods", () => { const savedUsers = await Promise.all(promises); savedUsers.length.should.be.equal(100); // check if they all are saved - const loadedUser = await userRepository.findOne({ firstName: "name #1" }); + const loadedUser = await userRepository.findOne({ firstName: "name #1" }, { alias: "user", orderBy: [{ sort: "user.id" }]}); loadedUser.id.should.be.equal(1); loadedUser.firstName.should.be.equal("name #1"); loadedUser.secondName.should.be.equal("Doe"); @@ -282,7 +285,7 @@ describe("repository > find methods", () => { secondName: "Doe" } }; - const loadedUser = await userRepository.findOne(findOptions); + const loadedUser = await userRepository.findOne(findOptions, { alias: "user", orderBy: [{ sort: "user.id" }]}); loadedUser.id.should.be.equal(99); loadedUser.firstName.should.be.equal("name #99"); loadedUser.secondName.should.be.equal("Doe"); diff --git a/test/utils/test-utils.ts b/test/utils/test-utils.ts index f5f046357..9658de158 100644 --- a/test/utils/test-utils.ts +++ b/test/utils/test-utils.ts @@ -10,11 +10,14 @@ export interface TestingConnectionOptions { entityDirectories?: string[]; secondaryConnections?: boolean; schemaCreate?: boolean; + dropSchemaOnConnection?: boolean; reloadAndCreateSchema?: boolean; skipMysql?: boolean; skipMariadb?: boolean; skipPostgres?: boolean; skipSqlite?: boolean; + skipOracle?: boolean; + skipSqlServer?: boolean; } export function closeConnections(connections: Connection[]) { @@ -43,19 +46,39 @@ export function createTestingConnectionOptions(type: "mysql"|"mysqlSecondary"|"m username: parameters.connections[type].username, password: parameters.connections[type].password, database: parameters.connections[type].database, - storage: parameters.connections[type].storage, - extra: { - max: 500 - } + storage: parameters.connections[type].storage }; } +/*export async function setupTestingConnections(options?: TestingConnectionOptions) { + const parameters: ConnectionOptions = { + driver: createTestingConnectionOptions(process.env["connection_setup_name"]), + autoSchemaCreate: options && options.entities ? options.schemaCreate : false, + dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, + entities: options && options.entities ? options.entities : [], + entitySchemas: options && options.entitySchemas ? options.entitySchemas : [], + entityDirectories: options && options.entityDirectories ? options.entityDirectories : [], + logging: { + // logQueries: true, // uncomment for debugging + logOnlyFailedQueries: true, + logFailedQueryError: true + }, + }; + + const connection = await createConnection(parameters); + if (options && options.reloadAndCreateSchema) + await connection.syncSchema(true); + + return [connection]; +}*/ + export async function setupTestingConnections(options?: TestingConnectionOptions): Promise { const mysqlParameters: ConnectionOptions = { name: "mysqlPrimaryConnection", driver: createTestingConnectionOptions("mysql"), autoSchemaCreate: options && options.entities ? options.schemaCreate : false, + dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, entities: options && options.entities ? options.entities : [], entitySchemas: options && options.entitySchemas ? options.entitySchemas : [], entityDirectories: options && options.entityDirectories ? options.entityDirectories : [], @@ -70,6 +93,7 @@ export async function setupTestingConnections(options?: TestingConnectionOptions name: "mysqlSecondaryConnection", driver: createTestingConnectionOptions("mysqlSecondary"), autoSchemaCreate: options && options.entities ? options.schemaCreate : false, + dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, entities: options && options.entities ? options.entities : [], entitySchemas: options && options.entitySchemas ? options.entitySchemas : [], entityDirectories: options && options.entityDirectories ? options.entityDirectories : [], @@ -84,6 +108,7 @@ export async function setupTestingConnections(options?: TestingConnectionOptions name: "mariadbPrimaryConnection", driver: createTestingConnectionOptions("mariadb"), autoSchemaCreate: options && options.entities ? options.schemaCreate : false, + dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, entities: options && options.entities ? options.entities : [], entitySchemas: options && options.entitySchemas ? options.entitySchemas : [], entityDirectories: options && options.entityDirectories ? options.entityDirectories : [], @@ -98,6 +123,7 @@ export async function setupTestingConnections(options?: TestingConnectionOptions name: "mariadbSecondaryConnection", driver: createTestingConnectionOptions("mariadbSecondary"), autoSchemaCreate: options && options.entities ? options.schemaCreate : false, + dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, entities: options && options.entities ? options.entities : [], entitySchemas: options && options.entitySchemas ? options.entitySchemas : [], entityDirectories: options && options.entityDirectories ? options.entityDirectories : [], @@ -112,6 +138,7 @@ export async function setupTestingConnections(options?: TestingConnectionOptions name: "postgresPrimaryConnection", driver: createTestingConnectionOptions("postgres"), autoSchemaCreate: options && options.entities ? options.schemaCreate : false, + dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, entities: options && options.entities ? options.entities : [], entitySchemas: options && options.entitySchemas ? options.entitySchemas : [], entityDirectories: options && options.entityDirectories ? options.entityDirectories : [], @@ -126,6 +153,7 @@ export async function setupTestingConnections(options?: TestingConnectionOptions name: "postgresSecondaryConnection", driver: createTestingConnectionOptions("postgresSecondary"), autoSchemaCreate: options && options.entities ? options.schemaCreate : false, + dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, entities: options && options.entities ? options.entities : [], entitySchemas: options && options.entitySchemas ? options.entitySchemas : [], entityDirectories: options && options.entityDirectories ? options.entityDirectories : [], @@ -140,6 +168,7 @@ export async function setupTestingConnections(options?: TestingConnectionOptions name: "sqlitePrimaryConnection", driver: createTestingConnectionOptions("sqlite"), autoSchemaCreate: options && options.entities ? options.schemaCreate : false, + dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, entities: options && options.entities ? options.entities : [], entitySchemas: options && options.entitySchemas ? options.entitySchemas : [], entityDirectories: options && options.entityDirectories ? options.entityDirectories : [], @@ -154,6 +183,7 @@ export async function setupTestingConnections(options?: TestingConnectionOptions name: "sqliteSecondaryConnection", driver: createTestingConnectionOptions("sqliteSecondary"), autoSchemaCreate: options && options.entities ? options.schemaCreate : false, + dropSchemaOnConnection: options && options.entities ? options.dropSchemaOnConnection : false, entities: options && options.entities ? options.entities : [], entitySchemas: options && options.entitySchemas ? options.entitySchemas : [], entityDirectories: options && options.entityDirectories ? options.entityDirectories : [],