New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ensureDatabase fails on certain conditions #3713
Comments
Please share more details, at least your ORM config and full stack trace instead of just an error message. |
Sorry, i thought it was clear after linking your commit. I try to create a testcase for you tomorrow. |
Well, what should be clear? That commit fixed another issue, its not something we should just revert. |
Tried to do exactly what you described but it works just fine for me. const orm = await MikroORM.init({
entities: [...],
dbName: ':mydatabase',
user: ':myuser',
type: 'postgresql',
});
await orm.schema.ensureDatabase(); While adding |
If you do it like that, it should create a db with the name :myuser in the background. Please check and delete the database before trying again. |
Just would like to add that I am experiencing same problem since patch 5.5.1 so I needed to rollback back to 5.5.0.
and here is connection string
When I delete database and run |
Well, if you use a connection url, you need to escape the special characters (like the colon in username, as in the OP. The one you provided almost cetainly dont fail - thats pretty much the default that is tested. I need to see a failing configuration. |
I tried to run here is my console output and config file I used. % npx mikro-orm -v
5.5.2
backend % npx mikro-orm migration:up
mikro-orm migration:up
Migrate up to the latest version
Options:
-t, --to Migrate up to specific version [string]
-f, --from Start migration from specific version [string]
-o, --only Migrate only specified versions [string]
-v, --version Show version number [boolean]
-h, --help Show help [boolean]
DriverException: database "mod" does not exist
at PostgreSqlExceptionConverter.convertException (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/@mikro-orm/core/platforms/ExceptionConverter.js:8:16)
at PostgreSqlExceptionConverter.convertException (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/@mikro-orm/postgresql/PostgreSqlExceptionConverter.js:42:22)
at PostgreSqlDriver.convertException (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:192:54)
at /Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:196:24
at processTicksAndRejections (node:internal/process/task_queues:95:5)
at async SchemaGenerator.createDatabase (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/@mikro-orm/knex/schema/SchemaGenerator.js:348:9)
at async SchemaGenerator.ensureDatabase (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/@mikro-orm/knex/schema/SchemaGenerator.js:39:13)
at async Migrator.ensureDatabase (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/@mikro-orm/migrations/Migrator.js:147:25)
at async Migrator.runMigrations (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/@mikro-orm/migrations/Migrator.js:279:9)
at async Function.handleUpDownCommand (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/@mikro-orm/cli/commands/MigrationCommandFactory.js:97:9)
previous error: database "mod" does not exist
at Parser.parseErrorMessage (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/pg-protocol/src/parser.ts:369:69)
at Parser.handlePacket (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/pg-protocol/src/parser.ts:188:21)
at Parser.parse (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/Users/bbellovic/WebstormProjects/modulaire/backend/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (node:events:513:28)
at Socket.emit (node:domain:489:12)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
length: 88,
severity: 'FATAL',
code: '3D000',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'postinit.c',
line: '885',
routine: 'InitPostgres'
}
backend % npx mikro-orm -v
5.5.0
backend % npx mikro-orm migration:up
Processing 'Migration20221026153928'
Applied 'Migration20221026153928'
Processing 'Migration20221026183921'
Applied 'Migration20221026183921'
Processing 'Migration20221028063004'
Applied 'Migration20221028063004'
Processing 'Migration20221031092906'
Applied 'Migration20221031092906'
Processing 'Migration20221031180428'
Applied 'Migration20221031180428'
Processing 'Migration20221031210421'
Applied 'Migration20221031210421'
Processing 'Migration20221102081936'
Applied 'Migration20221102081936'
Processing 'Migration20221107200040'
Applied 'Migration20221107200040'
Successfully migrated up to the latest version config.ts import { TsMorphMetadataProvider } from '@mikro-orm/reflection';
import { SqlHighlighter } from '@mikro-orm/sql-highlighter';
export default {
entities: ['dist/**/*.entity.js'],
entitiesTs: ['src/**/*.entity.ts'],
clientUrl: process.env.DATABASE_URL,
type: 'postgresql',
highlighter: new SqlHighlighter(),
metadataProvider: TsMorphMetadataProvider,
autoLoadEntities: true,
migrations: {
snapshot: false,
tableName: 'migrations',
path: './migrations',
pattern: /^[\w-]+\d+\.ts$/,
transactional: true,
disableForeignKeys: false,
allOrNothing: true,
emit: 'ts'
}
}; snapshot of pacakge.json
|
Thanks, I got it reproduced, apparently it fails even if the |
Thanks for your investigation. I noticed that this happens also on |
let me know if latest dev version works |
There is no change on my end, it still throws that a database with the same name as my postgres-user does not exists, which is true but shouldn't be necessary. As a workaround i'm overwriting the prototype of PostgreSqlSchemaHelper:
Just to resolve a little bit of confusion:
const orm = await MikroORM.init({
type: 'postgresql',
dbName: dbC.database + '-' + Date.now().toString(),
user: dbC.username, // pitapi_test
password: dbC.password,
host: dbC.host,
port: dbC.port,
entities,
pool: {
min: 0,
max: 20
}
})
await orm.getSchemaGenerator().ensureDatabase()
|
Well, then I really need to see a failing repro, a complete one, not a snippet that uses some undefined variables (what's
It works fine even with such names... But next time please don't use placeholders, and provide the exact failing values. You think something is not relevant and generalize, but what you said is simply not enough to reproduce anything - the issue is not that simple. I obviously don't wanna see your exact user/pass/host, feel free to mangle the values, as long as it fails the same - the point is that you should come up with a code I can run myself that will reproduce. |
@B4nan I can confirm that However, Thanks a lot for the quick fix. |
Hmm I don't see how it could behave differently, those commands are using the exact same way to init the ORM. Will try one more time while using the CLI, as till now I was reproducing directly with programmatic API instead of CLI. |
Ok, I can reproduce the |
@ml1nk maybe it will be a similar story with your issue - are you using the CLI too? What command? Also enable |
The issue with |
I was able to trace down a subsequent issue with that - inside the edit: looks like the schema generator class also holds the knex instance, so it could have some side effects there too, not just with migrations |
When a database is missing, the ensuring mechanism is reconnecting, hence the cached knex instance becomes outdated. Instead of replacing it, it's better to use a getter and always have up to date value. Closes #3713
Please try |
It's fixed. Thank you.
Sorry that was my bad, i will try to do better next time. |
Amazing, I was running out of ideas :D Found one more issue fixed by 11756ba where the |
FYI I will be reverting this change because of #3769, postgres requires the database name to be present, and apparently defaults to the user name if not provided. So while there was a bug in with the |
Describe the bug
ensureDatabase fails if
Bug was introduced by b1a867d
Versions
The text was updated successfully, but these errors were encountered: