Comments (5)
I had the same issue and looked at the code but I wasn't sure how to fix it or whether it is possible to fix it without changing the public interface of the library. In my opinion migrations should be executed from the CI/CD pipeline as a commandline tool and not by a service at startup (by using the migration tool as a library). I find the latter to be an antipattern and it makes changing/refactoring the code more difficult (because of the exported public stuff).
In my opinion what you need to perform migrations is only a goto <target>
command. The up
and down
commands can be translated to goto latest
and goto latest-1
respectively.
If we assume that we have a goto <target>
command then it should do the following:
- Migrations that are newer than
<target>
and have been up-migrated have to be reverted by down-migrating them in descending order. - The
<target>
migration along with the older migrations have to be up-migrated in ascending order (only those that haven't yet been applied).
Based on the above algorithm if we have a situation like this:
[X] 0001.sql
[ ] 0002.sql
[X] 0003.sql
[ ] 0004.sql
[X] 0005.sql
[X] 0006.sql
[X] 0007.sql
[ ] 0008.sql
... and we perform a goto 0005.sql
then it should result in the following operations:
- down-migrate 0007.sql
- down-migrate 0006.sql
- up-migrate 0002.sql
- up-migrate 0004.sql
Result:
[X] 0001.sql
[X] 0002.sql
[X] 0003.sql
[X] 0004.sql
[X] 0005.sql
[ ] 0006.sql
[ ] 0007.sql
[ ] 0008.sql
However, I think if you have unapplied migrations that are older than some applied migrations then you already have a problem that should be resolved manually - which means that the goto <target>
command should fail in the above situation instead of doing both down- and up- migrations.
You can easily end up with gaps in the range of your applied migrations by working on several branches and merging them.
from sql-migrate.
@pasztorpisti Cool to see you build your own tool, but please name it something else. Right now it's going to lead to a ton of confusion and it makes it impossible to have both installed at the same time (not to mention broken builds).
from sql-migrate.
@rubenv For now I think it's better if I keep the tool private for myself (less hassle for many reasons). I'll have to think about a name and need free time to change the references everywhere (CI, docker hub) if I decide to make the change. I'm a barbarian who names things by simply joins keywords so "migrate" and "sql-migrate" are pretty obvious choices.
from sql-migrate.
Same problem.
When you go down and a previous up query is not done, it is added to the migrations (with this up query, see the call to catchup). The problem is that the direction of the migration is always down, so when you do the switch dir {
of applyMigrations:508, you go to the down part, so the query is executed (probably a create), but the id is deleted (but not existing anyway) instead of being inserted (as an up query).
from sql-migrate.
Proposed PR #235
from sql-migrate.
Related Issues (20)
- IgnoreUnknown is not working
- SetTable fails race detector HOT 3
- Support for multiple migration dirs
- Where may I ask questions? HOT 2
- gorp retracted by module author warning HOT 2
- Support for exclusive connection while executing migration HOT 3
- ToApply doesn't consider IgnoreUnknown flags when apply migrations up dir HOT 1
- Multiple `migrate Up` blocks in a single file? HOT 1
- Migrate transactions for multiple shards HOT 5
- [Proposal] Addition of 'reset' to sql-migrate command HOT 3
- Go install fails on Go 1.19 HOT 5
- schema field in dbconfig.yml being ignored
- Doing a migrate down when there is a migration βholeβ is corrupting the database
- MemoryMigrationSource execution order is wrong HOT 2
- [BUG] Missing integrity checks on migrations lead to corrupt DB schema state HOT 3
- Logic Bug with ExecVersion HOT 1
- Possible release to fix build issue on Alpine Linux HOT 1
- Migration failed: pq: unterminated dollar-quoted string at or near HOT 1
- sql-migrate in docker registry
- How to distinguish the different branches HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sql-migrate.