* feat: add feature flag loading in projects * fix: make feature flag consume itself * fix: rename container registration to featureFlagRouter * fix: refactor * behavioral feature flags * add environment to server * limit "useTemplateDb" to non feature flagged migrations * filter migrations and entities according to those which are enabled in the environment * run only migrations that are enabled when running 'medusa migrations run' * add logging to the featureflag loader * initial implementation of featureFlagEntity * column descriptors * initial startServerWithEnv (to be refactored) * update commands * final touches * update loaders to fix unit tests * enable all batch job tests * update seed method * add api test capabilities * revert batch job test * revert formatting changes * pr feedback * pr feedback * remove unused imports * rename feature flag decorators * pr feedback Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
121 lines
3.0 KiB
JavaScript
121 lines
3.0 KiB
JavaScript
const path = require("path")
|
|
require("dotenv").config({ path: path.join(__dirname, "../.env") })
|
|
|
|
const { createDatabase, dropDatabase } = require("pg-god")
|
|
const { createConnection, getConnection } = require("typeorm")
|
|
|
|
const DB_USERNAME = process.env.DB_USERNAME || "postgres"
|
|
const DB_PASSWORD = process.env.DB_PASSWORD || ""
|
|
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@localhost`
|
|
|
|
const pgGodCredentials = {
|
|
user: DB_USERNAME,
|
|
password: DB_PASSWORD,
|
|
}
|
|
|
|
class DatabaseFactory {
|
|
constructor() {
|
|
this.connection_ = null
|
|
this.masterConnectionName = "master"
|
|
this.templateDbName = "medusa-integration-template"
|
|
}
|
|
|
|
async createTemplateDb_({ cwd }) {
|
|
try {
|
|
// const cwd = path.resolve(path.join(__dirname, ".."))
|
|
const connection = await this.getMasterConnection()
|
|
const migrationDir = path.resolve(
|
|
path.join(
|
|
cwd,
|
|
`node_modules`,
|
|
`@medusajs`,
|
|
`medusa`,
|
|
`dist`,
|
|
`migrations`,
|
|
`*.js`
|
|
)
|
|
)
|
|
|
|
const { getEnabledMigrations } = require(path.join(
|
|
cwd,
|
|
`node_modules`,
|
|
`@medusajs`,
|
|
`medusa`,
|
|
`dist`,
|
|
`commands`,
|
|
`utils`,
|
|
`get-migrations`
|
|
))
|
|
|
|
// filter migrations to only include those that dont have feature flags
|
|
const enabledMigrations = await getEnabledMigrations(
|
|
[migrationDir],
|
|
(flag) => false
|
|
)
|
|
|
|
await dropDatabase(
|
|
{
|
|
databaseName: this.templateDbName,
|
|
errorIfNonExist: false,
|
|
},
|
|
pgGodCredentials
|
|
)
|
|
await createDatabase(
|
|
{ databaseName: this.templateDbName },
|
|
pgGodCredentials
|
|
)
|
|
|
|
const templateDbConnection = await createConnection({
|
|
type: "postgres",
|
|
name: "templateConnection",
|
|
url: `${DB_URL}/${this.templateDbName}`,
|
|
migrations: enabledMigrations,
|
|
})
|
|
|
|
await templateDbConnection.runMigrations()
|
|
await templateDbConnection.close()
|
|
|
|
return connection
|
|
} catch (err) {
|
|
console.log("error in createTemplateDb_")
|
|
console.log(err)
|
|
}
|
|
}
|
|
|
|
async getMasterConnection() {
|
|
try {
|
|
return await getConnection(this.masterConnectionName)
|
|
} catch (err) {
|
|
return await this.createMasterConnection()
|
|
}
|
|
}
|
|
|
|
async createMasterConnection() {
|
|
const connection = await createConnection({
|
|
type: "postgres",
|
|
name: this.masterConnectionName,
|
|
url: `${DB_URL}`,
|
|
})
|
|
|
|
return connection
|
|
}
|
|
|
|
async createFromTemplate(dbName) {
|
|
const connection = await this.getMasterConnection()
|
|
|
|
await connection.query(`DROP DATABASE IF EXISTS "${dbName}";`)
|
|
await connection.query(
|
|
`CREATE DATABASE "${dbName}" TEMPLATE "${this.templateDbName}";`
|
|
)
|
|
}
|
|
|
|
async destroy() {
|
|
const connection = await this.getMasterConnection()
|
|
|
|
await connection.query(`DROP DATABASE IF EXISTS "${this.templateDbName}";`)
|
|
await connection.close()
|
|
}
|
|
}
|
|
|
|
module.exports = new DatabaseFactory()
|