chore(medusa): Typeorm upgrade to 0.3.11 (#3041)
This commit is contained in:
@@ -2,7 +2,7 @@ const path = require("path")
|
||||
|
||||
const { getConfigFile } = require("medusa-core-utils")
|
||||
const { dropDatabase } = require("pg-god")
|
||||
const { createConnection } = require("typeorm")
|
||||
const { DataSource } = require("typeorm")
|
||||
const dbFactory = require("./use-template-db")
|
||||
|
||||
const DB_HOST = process.env.DB_HOST
|
||||
@@ -27,13 +27,13 @@ const keepTables = [
|
||||
"currency",
|
||||
]
|
||||
|
||||
let connectionType = "postgresql"
|
||||
let dataSourceType = "postgresql"
|
||||
|
||||
const DbTestUtil = {
|
||||
db_: null,
|
||||
|
||||
setDb: function (connection) {
|
||||
this.db_ = connection
|
||||
setDb: function (dataSource) {
|
||||
this.db_ = dataSource
|
||||
},
|
||||
|
||||
clear: async function () {
|
||||
@@ -47,7 +47,7 @@ const DbTestUtil = {
|
||||
|
||||
const manager = this.db_.manager
|
||||
|
||||
if (connectionType === "sqlite") {
|
||||
if (dataSourceType === "sqlite") {
|
||||
await manager.query(`PRAGMA foreign_keys = OFF`)
|
||||
} else {
|
||||
await manager.query(`SET session_replication_role = 'replica';`)
|
||||
@@ -64,7 +64,7 @@ const DbTestUtil = {
|
||||
await manager.query(`DELETE
|
||||
FROM "${entity.tableName}";`)
|
||||
}
|
||||
if (connectionType === "sqlite") {
|
||||
if (dataSourceType === "sqlite") {
|
||||
await manager.query(`PRAGMA foreign_keys = ON`)
|
||||
} else {
|
||||
await manager.query(`SET session_replication_role = 'origin';`)
|
||||
@@ -72,7 +72,7 @@ const DbTestUtil = {
|
||||
},
|
||||
|
||||
shutdown: async function () {
|
||||
await this.db_.close()
|
||||
await this.db_.destroy()
|
||||
return await dropDatabase({ DB_NAME }, pgGodCredentials)
|
||||
},
|
||||
}
|
||||
@@ -88,14 +88,12 @@ module.exports = {
|
||||
require("@medusajs/medusa/dist/loaders/feature-flags").default
|
||||
|
||||
const featureFlagsRouter = featureFlagsLoader({ featureFlags })
|
||||
|
||||
const modelsLoader = require("@medusajs/medusa/dist/loaders/models").default
|
||||
|
||||
const entities = modelsLoader({}, { register: false })
|
||||
|
||||
if (projectConfig.database_type === "sqlite") {
|
||||
connectionType = "sqlite"
|
||||
const dbConnection = await createConnection({
|
||||
dataSourceType = "sqlite"
|
||||
const dataSource = new DataSource({
|
||||
type: "sqlite",
|
||||
database: projectConfig.database_database,
|
||||
synchronize: true,
|
||||
@@ -103,8 +101,10 @@ module.exports = {
|
||||
extra: database_extra ?? {},
|
||||
})
|
||||
|
||||
instance.setDb(dbConnection)
|
||||
return dbConnection
|
||||
const dbDataSource = await dataSource.initialize()
|
||||
|
||||
instance.setDb(dbDataSource)
|
||||
return dbDataSource
|
||||
} else {
|
||||
await dbFactory.createFromTemplate(DB_NAME)
|
||||
|
||||
@@ -138,7 +138,7 @@ module.exports = {
|
||||
(e) => typeof e.isFeatureEnabled === "undefined" || e.isFeatureEnabled()
|
||||
)
|
||||
|
||||
const dbConnection = await createConnection({
|
||||
const dbDataSource = new DataSource({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
entities: enabledEntities.concat(moduleModels),
|
||||
@@ -147,10 +147,12 @@ module.exports = {
|
||||
name: "integration-tests",
|
||||
})
|
||||
|
||||
await dbConnection.runMigrations()
|
||||
await dbDataSource.initialize()
|
||||
|
||||
instance.setDb(dbConnection)
|
||||
return dbConnection
|
||||
await dbDataSource.runMigrations()
|
||||
|
||||
instance.setDb(dbDataSource)
|
||||
return dbDataSource
|
||||
}
|
||||
},
|
||||
useDb: function () {
|
||||
|
||||
@@ -4,13 +4,15 @@ require("dotenv").config({ path: path.join(__dirname, "../.env.test") })
|
||||
|
||||
const { getConfigFile } = require("medusa-core-utils")
|
||||
const { createDatabase, dropDatabase } = require("pg-god")
|
||||
const { createConnection, getConnection } = require("typeorm")
|
||||
const { DataSource } = require("typeorm")
|
||||
|
||||
const DB_HOST = process.env.DB_HOST
|
||||
const DB_USERNAME = process.env.DB_USERNAME
|
||||
const DB_PASSWORD = process.env.DB_PASSWORD
|
||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}`
|
||||
|
||||
let masterDataSource
|
||||
|
||||
const pgGodCredentials = {
|
||||
user: DB_USERNAME,
|
||||
password: DB_PASSWORD,
|
||||
@@ -19,16 +21,14 @@ const pgGodCredentials = {
|
||||
|
||||
class DatabaseFactory {
|
||||
constructor() {
|
||||
this.connection_ = null
|
||||
this.masterConnectionName = "master"
|
||||
this.dataSource_ = null
|
||||
this.masterDataSourceName = "master"
|
||||
this.templateDbName = "medusa-integration-template"
|
||||
}
|
||||
|
||||
async createTemplateDb_({ cwd }) {
|
||||
const { configModule } = getConfigFile(cwd, `medusa-config`)
|
||||
|
||||
const connection = await this.getMasterConnection()
|
||||
|
||||
const dataSource = await this.getMasterDataSource()
|
||||
const migrationDir = path.resolve(
|
||||
path.join(
|
||||
__dirname,
|
||||
@@ -68,51 +68,52 @@ class DatabaseFactory {
|
||||
pgGodCredentials
|
||||
)
|
||||
|
||||
const templateDbConnection = await createConnection({
|
||||
const templateDbDataSource = new DataSource({
|
||||
type: "postgres",
|
||||
name: "templateConnection",
|
||||
name: "templateDataSource",
|
||||
url: `${DB_URL}/${this.templateDbName}`,
|
||||
migrations: enabledMigrations.concat(moduleMigrations),
|
||||
})
|
||||
|
||||
await templateDbConnection.runMigrations()
|
||||
await templateDbConnection.close()
|
||||
await templateDbDataSource.initialize()
|
||||
|
||||
return connection
|
||||
await templateDbDataSource.runMigrations()
|
||||
|
||||
await templateDbDataSource.destroy()
|
||||
|
||||
return dataSource
|
||||
}
|
||||
|
||||
async getMasterConnection() {
|
||||
try {
|
||||
return getConnection(this.masterConnectionName)
|
||||
} catch (err) {
|
||||
return await this.createMasterConnection()
|
||||
}
|
||||
async getMasterDataSource() {
|
||||
masterDataSource = masterDataSource || (await this.createMasterDataSource())
|
||||
return masterDataSource
|
||||
}
|
||||
|
||||
async createMasterConnection() {
|
||||
const connection = await createConnection({
|
||||
async createMasterDataSource() {
|
||||
const dataSource = new DataSource({
|
||||
type: "postgres",
|
||||
name: this.masterConnectionName,
|
||||
name: this.masterDataSourceName,
|
||||
url: `${DB_URL}`,
|
||||
})
|
||||
await dataSource.initialize()
|
||||
|
||||
return connection
|
||||
return dataSource
|
||||
}
|
||||
|
||||
async createFromTemplate(dbName) {
|
||||
const connection = await this.getMasterConnection()
|
||||
const dataSource = await this.getMasterDataSource()
|
||||
|
||||
await connection.query(`DROP DATABASE IF EXISTS "${dbName}";`)
|
||||
await connection.query(
|
||||
await dataSource.query(`DROP DATABASE IF EXISTS "${dbName}";`)
|
||||
await dataSource.query(
|
||||
`CREATE DATABASE "${dbName}" TEMPLATE "${this.templateDbName}";`
|
||||
)
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
const connection = await this.getMasterConnection()
|
||||
const dataSource = await this.getMasterDataSource()
|
||||
|
||||
await connection.query(`DROP DATABASE IF EXISTS "${this.templateDbName}";`)
|
||||
await connection.close()
|
||||
await dataSource.query(`DROP DATABASE IF EXISTS "${this.templateDbName}";`)
|
||||
await dataSource.destroy()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user