How to properly use the migrator to run migrations programmatically #3246
-
https://docs.adonisjs.com/guides/database/migrations#running-migrations-programmatically I have a multi db app, where each tenant gets their own instance of the database and would like to run migrations as new databases are created. I did try following the docs, but the migrations never run in the database, public async migrate() {
this.connect()
const migrator = new Migrator(Database, Application, {
direction: 'up',
dryRun: true,
connectionName: this.database,
})
await migrator.run()
this.closeConnection()
console.log(migrator.migratedFiles)
} Just so you can now what code is running in the other functions. public createConnection(): PostgreConfig {
return {
client: 'pg',
connection: {
host: this.host,
port: this.port,
user: this.user,
password: Encryption.decrypt(this.password) as string,
database: this.database,
ssl: {
rejectUnauthorized: false,
},
},
migrations: {
naturalSort: true,
paths: [Application.appRoot + '/database/tenants_migrations'],
disableRollbacksInProduction: true,
},
seeders: {
paths: [Application.appRoot + '/database/tenants_seeders'],
},
healthCheck: false,
debug: Env.get('NODE_ENV') === 'development',
}
}
public connect() {
if (!Database.manager.has(this.database)) {
Database.manager.add(this.database, this.createConnection())
Database.manager.connect(this.database)
}
}
public closeConnection() {
if (Database.manager.has(this.database)) {
Database.manager.close(this.database, true)
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Never mind, just noticed the
|
Beta Was this translation helpful? Give feedback.
Never mind, just noticed the
dryRun
setting