{"version":3,"file":"index.mjs","sources":["../../src/schema/index.ts"],"sourcesContent":["import createDebug from 'debug';\n\nimport createSchemaBuilder from './builder';\nimport createSchemaDiff from './diff';\nimport createSchemaStorage from './storage';\nimport { metadataToSchema } from './schema';\nimport { createRenameHelpers } from './rename-helpers';\n\nimport type { Schema, SchemaDiff } from './types';\nimport type { RenameHelpers } from './rename-helpers';\nimport type { Database } from '..';\n\nexport type * from './types';\nexport type { AttributeRenameHandler, AttributeRenames, RenameHelpers } from './rename-helpers';\n\nconst debug = createDebug('strapi::database');\n\nexport interface SchemaProvider extends RenameHelpers {\n  builder: ReturnType<typeof createSchemaBuilder>;\n  schemaDiff: ReturnType<typeof createSchemaDiff>;\n  schemaStorage: ReturnType<typeof createSchemaStorage>;\n  sync(): Promise<SchemaDiff['status']>;\n  syncSchema(): Promise<SchemaDiff['status']>;\n  reset(): Promise<void>;\n  create(): Promise<void>;\n  drop(): Promise<void>;\n  schema: Schema;\n}\n\ninterface State {\n  schema?: Schema;\n}\n\nexport const createSchemaProvider = (db: Database): SchemaProvider => {\n  const state: State = {};\n\n  return {\n    get schema() {\n      if (!state.schema) {\n        debug('Converting metadata to database schema');\n        state.schema = metadataToSchema(db.metadata);\n      }\n\n      return state.schema;\n    },\n    builder: createSchemaBuilder(db),\n    schemaDiff: createSchemaDiff(db),\n    schemaStorage: createSchemaStorage(db),\n\n    // Guarded, logged runtime helpers for generated rename migrations\n    // (`db.schema.renameColumn(knex, …)` etc.). See rename-helpers.ts.\n    ...createRenameHelpers({ db }),\n\n    /**\n     * Drops the database schema\n     */\n    async drop() {\n      debug('Dropping database schema');\n\n      const DBSchema = await db.dialect.schemaInspector.getSchema();\n      await this.builder.dropSchema(DBSchema);\n    },\n\n    /**\n     * Creates the database schema\n     */\n    async create() {\n      debug('Created database schema');\n      await this.builder.createSchema(this.schema);\n    },\n\n    /**\n     * Resets the database schema\n     */\n    async reset() {\n      debug('Resetting database schema');\n      await this.drop();\n      await this.create();\n    },\n\n    async syncSchema(): Promise<SchemaDiff['status']> {\n      debug('Synchronizing database schema');\n\n      const databaseSchema = await db.dialect.schemaInspector.getSchema();\n      const storedSchema = await this.schemaStorage.read();\n\n      /*\n        3way diff - DB schema / previous metadataSchema / new metadataSchema\n\n        - When something doesn't exist in the previous metadataSchema -> It's not tracked by us and should be ignored\n        - If no previous metadataSchema => use new metadataSchema so we start tracking them and ignore everything else\n        - Apply this logic to Tables / Columns / Indexes / FKs ...\n        - Handle errors (indexes or fks on incompatible stuff ...)\n\n      */\n\n      const { status, diff } = await this.schemaDiff.diff({\n        previousSchema: storedSchema?.schema,\n        databaseSchema,\n        userSchema: this.schema,\n      });\n\n      if (status === 'CHANGED') {\n        await this.builder.updateSchema(diff);\n      }\n\n      await this.schemaStorage.add(this.schema);\n\n      return status;\n    },\n\n    // TODO: support options to migrate softly or forcefully\n    // TODO: support option to disable auto migration & run a CLI command instead to avoid doing it at startup\n    // TODO: Allow keeping extra indexes / extra tables / extra columns (globally or on a per table basis)\n    async sync(): Promise<SchemaDiff['status']> {\n      return db.runInPhase('schema sync', async () => {\n        if (await db.migrations.shouldRun()) {\n          debug('Found migrations to run');\n          await db.migrations.up();\n\n          return this.syncSchema();\n        }\n\n        const oldSchema = await this.schemaStorage.read();\n\n        if (!oldSchema) {\n          debug('Schema not persisted yet');\n          return this.syncSchema();\n        }\n\n        const { hash: oldHash } = oldSchema;\n        const hash = await this.schemaStorage.hashSchema(this.schema);\n\n        if (oldHash !== hash) {\n          debug('Schema changed');\n\n          return this.syncSchema();\n        }\n\n        debug('Schema unchanged');\n\n        return 'UNCHANGED';\n      });\n    },\n  };\n};\n"],"names":["debug","createDebug","createSchemaProvider","db","state","schema","metadataToSchema","metadata","builder","createSchemaBuilder","schemaDiff","createSchemaDiff","schemaStorage","createSchemaStorage","createRenameHelpers","drop","DBSchema","dialect","schemaInspector","getSchema","dropSchema","create","createSchema","reset","syncSchema","databaseSchema","storedSchema","read","status","diff","previousSchema","userSchema","updateSchema","add","sync","runInP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