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

Migration files in multiple folders #562

Open
DenisKnez opened this issue May 11, 2021 · 8 comments
Open

Migration files in multiple folders #562

DenisKnez opened this issue May 11, 2021 · 8 comments

Comments

@DenisKnez
Copy link

In my projects I have a service folder and each service is inside it's separate folder. I would like to have a folder in each service that contains migrations only for the service it's in. (This looks much cleaner and it's easier to find stuff then when it's all in one folder)

And the library would take all of those folders from each of the services and take them like it's all in a single file.

Currently there is a source file that you provide when doing one of the migrate.NewWith... my suggestion would be to place the source file as a last parameter and make it a ...sourceUrl so then you can input as many sources as you would like. And the function just takes the migrations from all of the folders. This would give great flexibility to where you place migrations and how you want to structure your project.

@dhui
Copy link
Member

dhui commented May 12, 2021

Do you plan on deploying your services independently? Are all of your services using the same database?

@DenisKnez
Copy link
Author

The services are just and architectual style we use in our project so the services will always stay together and will use the same database. We do plan to use multiple databases but each database would have its own migrations and we would have a migrate instance for each database so that is not a problem

@DenisKnez
Copy link
Author

DenisKnez commented May 12, 2021

So instead of this:
Screenshot 2021-05-12 at 09 48 08

I would like to do this:
Screenshot 2021-05-12 at 09 48 29

Migrations 1 and 5 would contain create table and alter table for csv table that is used for serviceCSV businesslogic and migration 3 would be for mail create table and you get where this is going

@dhui
Copy link
Member

dhui commented May 12, 2021

You can do this by creating multiple instances of migrate.Migrate. migrate won't provide a method to multiple instances of migrate.Migrate for simplicity and flexibility.

@dhui dhui closed this as completed May 12, 2021
@DenisKnez
Copy link
Author

If a single database is used in the project with multiple instances of migrate there will be a collision, right? Cause each instance wants a schema_migrations table in the database and also if there is multiple instances then each of them look at their own migrations. The multiple instances work for multiple databases IF each service has its own database. Correct me if im wrong but, we tried multiple instances on a sam database but if I remembed correctly it would just fail cause each instance wants its own schema_migrations table

@dhui
Copy link
Member

dhui commented May 12, 2021

Are you using an RDBMS? Can you use a different database per service using the same RDBMS? e.g. call CREATE DATABASE for each service

One migrate.Migrate instance cannot:

  1. manage multiple databases - the schema migrations table tracks what the latest migration is and whether or not it applied successfully. So you'd need a schema migrations table for each database, which requires multiple instances of migrate.Migrate
  2. combine migrations from multiple sources - There's no sane way to merge migrations from multiple sources. Applying migrations in the global lexicographical order should work assuming there are no dependencies between the migrations from different sources. Also, one service shouldn't be blocked by the migrations running for another service.

If you want to use a single db, you can either:

  1. combine all of your migrations for a single migration source but you'd still have services blocking each other when running migrations.
  2. specify a different schema migrations table for each migrate.Migrate instance (e.g. each service) if the db driver supports that config

@dhui dhui reopened this May 12, 2021
@Activity00
Copy link

hello, How did you solve this problem in the end.

@FarhanSajid1
Copy link

hello, How did you solve this problem in the end.

Not sure if this is example what you're looking for, but this was what I used when I ran into this issue: #95

Basically set the migration table per service in the dsn when running the migration like such:
3d5d577#diff-bf118447bab23f56311aa9cab0352f8be824d3166b7423020fe5ebb6e5444358R359

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants