Skip to content
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

Create database on initial migration? #1757

Closed
jasonwilson opened this issue May 4, 2021 · 5 comments
Closed

Create database on initial migration? #1757

jasonwilson opened this issue May 4, 2021 · 5 comments
Labels
enhancement New feature or request
Milestone

Comments

@jasonwilson
Copy link
Contributor

jasonwilson commented May 4, 2021

Is your feature request related to a problem? Please describe.
In trying to setup a new database from an initial migration I realized there's no way to create a new database from scratch. Is that accurate?

Describe the solution you'd like
Something akin to rake db:init or rake db:create where mikro-orm will take the runtime config and create a new database if it doesn't exist

Describe alternatives you've considered
Tried creating a migration to do this but mikro-orm migrator fails to initialize because the database doesn't exist. It looks like similar functionality is provided by the schema generator but that's using a lower level connection.

  async up(): Promise<void> {
    this.addSql(`CREATE DATABASE ${config.dbName};`)
  }
@jasonwilson jasonwilson added the enhancement New feature or request label May 4, 2021
@B4nan
Copy link
Member

B4nan commented May 5, 2021

There is SchemaGenerator.ensureDatabase() you could call programatically. It is automatically called when you use the schema generator from CLI too (e.g. creating schema always first ensures the db exists). Not sure if it's good idea to add this to migrations, the feature on its own feels quite distinct. But we can definitely expose that method via CLI (as one of the schema generator commands, so again, not connected to migrations).

Something akin to rake db:init or rake db:create where mikro-orm will take the runtime config and create a new database if it doesn't exist

Never used rails, so please actually describe what you want to propose, as this has no meaning to me :P But both of that sounds like they work more like schema generator rather than migrations (and we already have that).

@jasonwilson
Copy link
Contributor Author

Good point on conflating db creation + migration that could be a little confusing. Saying that, tying that action specifically to schema creation also feels disconnected...

In rails land rake db:create will initialize a new database with the supplied database configuration. If mikro-orm exposed a command like mikro-orm database:create to initialize a new database with the supplied configuration that would be helpful. This would allow for users to spinup a db from scratch without leaving the ORM. Thoughts?

For now I'll experiment with creating a helper using SchemaGenerator.ensureDatabase()

@B4nan
Copy link
Member

B4nan commented May 5, 2021

This would allow for users to spinup a db from scratch without leaving the ORM.

This sounds more like you want the schema to be generated too, otherwise you will have just clear db. If so, then again, schema generator is the way to go and it already supports creating db automatically (mikro-orm schema:create -r).

@jasonwilson
Copy link
Contributor Author

Not quite- in this case we'll have the migrations so the flow would be:

  1. Create database
  2. Run migrations

@B4nan
Copy link
Member

B4nan commented May 5, 2021

I think we could just create the db right ahead, just like we do in the schema generator.

Looking at the code and migration commands (at least up/down) will first ensure the migrations table exists, as they requite to read the data from migrations table. But it only checks if the table exists (and creates it if not), so we should just add the ensureDatabase call before that.

But I still see a value in having the separate command to create the db.

PR welcome, not a big priority but if nobody will pick this up, will do so myself before v5 is out.

@B4nan B4nan added good first issue Good for newcomers help wanted Extra attention is needed labels May 5, 2021
B4nan pushed a commit that referenced this issue May 11, 2021
This command ensures the database exists.

Related: #1757
@B4nan B4nan added this to the 5.0 milestone Aug 27, 2021
@B4nan B4nan removed good first issue Good for newcomers help wanted Extra attention is needed labels Aug 27, 2021
@B4nan B4nan closed this as completed in 02dd67c Aug 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants