Feat(medusa): implement feature flags (#1768)
* 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>
This commit is contained in:
@@ -2,7 +2,7 @@ const path = require("path")
|
||||
const { spawn } = require("child_process")
|
||||
const { setPort } = require("./use-api")
|
||||
|
||||
module.exports = ({ cwd, redisUrl, uploadDir, verbose }) => {
|
||||
module.exports = ({ cwd, redisUrl, uploadDir, verbose, env }) => {
|
||||
const serverPath = path.join(__dirname, "test-server.js")
|
||||
|
||||
// in order to prevent conflicts in redis, use a different db for each worker
|
||||
@@ -21,6 +21,7 @@ module.exports = ({ cwd, redisUrl, uploadDir, verbose }) => {
|
||||
COOKIE_SECRET: "test",
|
||||
REDIS_URL: redisUrl ? redisUrlWithDatabase : undefined, // If provided, will use a real instance, otherwise a fake instance
|
||||
UPLOAD_DIR: uploadDir, // If provided, will be used for the fake local file service
|
||||
...env,
|
||||
},
|
||||
stdio: verbose
|
||||
? ["inherit", "inherit", "inherit", "ipc"]
|
||||
|
||||
28
integration-tests/helpers/start-server-with-environment.js
Normal file
28
integration-tests/helpers/start-server-with-environment.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const setupServer = require("./setup-server")
|
||||
const { initDb } = require("./use-db")
|
||||
|
||||
const startServerWithEnvironment = async ({ cwd, verbose, env }) => {
|
||||
if (env) {
|
||||
Object.entries(env).forEach(([key, value]) => {
|
||||
process.env[key] = value
|
||||
})
|
||||
}
|
||||
|
||||
const dbConnection = await initDb({
|
||||
cwd,
|
||||
})
|
||||
|
||||
Object.entries(env).forEach(([key, value]) => {
|
||||
delete process.env[key]
|
||||
})
|
||||
|
||||
const medusaProcess = await setupServer({
|
||||
cwd,
|
||||
verbose,
|
||||
env,
|
||||
})
|
||||
|
||||
return [medusaProcess, dbConnection]
|
||||
}
|
||||
|
||||
export default startServerWithEnvironment
|
||||
@@ -79,6 +79,19 @@ const instance = DbTestUtil
|
||||
module.exports = {
|
||||
initDb: async function ({ cwd }) {
|
||||
const configPath = path.resolve(path.join(cwd, `medusa-config.js`))
|
||||
const { projectConfig, featureFlags } = require(configPath)
|
||||
|
||||
const featureFlagsLoader = require(path.join(
|
||||
cwd,
|
||||
`node_modules`,
|
||||
`@medusajs`,
|
||||
`medusa`,
|
||||
`dist`,
|
||||
`loaders`,
|
||||
`feature-flags`
|
||||
)).default
|
||||
|
||||
const featureFlagsRouter = featureFlagsLoader({ featureFlags })
|
||||
|
||||
const modelsLoader = require(path.join(
|
||||
cwd,
|
||||
@@ -89,9 +102,9 @@ module.exports = {
|
||||
`loaders`,
|
||||
`models`
|
||||
)).default
|
||||
|
||||
const entities = modelsLoader({}, { register: false })
|
||||
|
||||
const { projectConfig } = require(configPath)
|
||||
if (projectConfig.database_type === "sqlite") {
|
||||
connectionType = "sqlite"
|
||||
const dbConnection = await createConnection({
|
||||
@@ -108,12 +121,48 @@ module.exports = {
|
||||
|
||||
await dbFactory.createFromTemplate(databaseName)
|
||||
|
||||
// get migraitons with enabled featureflags
|
||||
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`
|
||||
))
|
||||
|
||||
const enabledMigrations = await getEnabledMigrations(
|
||||
[migrationDir],
|
||||
(flag) => featureFlagsRouter.isFeatureEnabled(flag)
|
||||
)
|
||||
|
||||
const enabledEntities = entities.filter(
|
||||
(e) => typeof e.isFeatureEnabled === "undefined" || e.isFeatureEnabled()
|
||||
)
|
||||
|
||||
const dbConnection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
entities,
|
||||
entities: enabledEntities,
|
||||
migrations: enabledMigrations,
|
||||
})
|
||||
|
||||
await dbConnection.runMigrations()
|
||||
|
||||
instance.setDb(dbConnection)
|
||||
return dbConnection
|
||||
}
|
||||
|
||||
@@ -31,10 +31,28 @@ class DatabaseFactory {
|
||||
`@medusajs`,
|
||||
`medusa`,
|
||||
`dist`,
|
||||
`migrations`
|
||||
`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,
|
||||
@@ -51,7 +69,7 @@ class DatabaseFactory {
|
||||
type: "postgres",
|
||||
name: "templateConnection",
|
||||
url: `${DB_URL}/${this.templateDbName}`,
|
||||
migrations: [`${migrationDir}/*.js`],
|
||||
migrations: enabledMigrations,
|
||||
})
|
||||
|
||||
await templateDbConnection.runMigrations()
|
||||
@@ -92,7 +110,7 @@ class DatabaseFactory {
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
let connection = await this.getMasterConnection()
|
||||
const connection = await this.getMasterConnection()
|
||||
|
||||
await connection.query(`DROP DATABASE IF EXISTS "${this.templateDbName}";`)
|
||||
await connection.close()
|
||||
|
||||
Reference in New Issue
Block a user