yogthos / migratus

MIGRATE ALL THE THINGS!
642 stars 93 forks source link

In which order are migrations run? #170

Closed little-dude closed 5 years ago

little-dude commented 5 years ago

Hi,

In Luminus I created these three migrations:

20190718150120-messages.down.sql
20190718150120-messages.up.sql
20190718150401-users.down.sql
20190718150401-users.up.sql
20190718151709-messages-status.down.sql
20190718151709-messages-status.up.sql

message-status references the tables created by the two other migrations:

CREATE TYPE status as ENUM ('sent', 'received', 'not sent');

--;;

CREATE TABLE messages_status (
       message_id INTEGER REFERENCES messages(id),
       user_id INTEGER REFERENCES users(id),
       status status NOT NULL
);

When running (migrate), I get:

user=> (migrate)                                                                                                                                                                              
2019-07-18 17:30:28,705 [nRepl-session-a6fcdac2-878b-4bfb-9c25-9965dd698e20] INFO  migratus.core - Starting migrations                                                                        
2019-07-18 17:30:28,715 [nRepl-session-a6fcdac2-878b-4bfb-9c25-9965dd698e20] DEBUG migratus.migrations - Looking for migrations in #object[java.io.File 0x574d6465 /home/little-dude/code/clojure
/test-sms/resources/migrations]                                                                                                                                                            
2019-07-18 17:30:28,716 [nRepl-session-a6fcdac2-878b-4bfb-9c25-9965dd698e20] INFO  migratus.core - Running up for [20190718151709]                                                            
2019-07-18 17:30:28,716 [nRepl-session-a6fcdac2-878b-4bfb-9c25-9965dd698e20] INFO  migratus.core - Up 20190718151709-messages-status                                                          
2019-07-18 17:30:28,719 [nRepl-session-a6fcdac2-878b-4bfb-9c25-9965dd698e20] DEBUG migratus.migration.sql - found 2 up migrations                                                             
2019-07-18 17:30:28,721 [nRepl-session-a6fcdac2-878b-4bfb-9c25-9965dd698e20] ERROR migratus.migration.sql - failed to execute command:                                                        

CREATE TABLE messages_status (                                                                                                                                                                
message_id INTEGER REFERENCES messages(id),                                                                                                                                                   
user_id INTEGER REFERENCES users(id),                                                                                                                                                         
status status NOT NULL                                                                                                                                                                        
);                                                                                                                                                                                            

2019-07-18 17:30:28,721 [nRepl-session-a6fcdac2-878b-4bfb-9c25-9965dd698e20] ERROR migratus.migration.sql - ERROR: relation "messages" does not exist                                         
2019-07-18 17:30:28,721 [nRepl-session-a6fcdac2-878b-4bfb-9c25-9965dd698e20] ERROR migratus.database - Migration messages-status failed because Batch entry 0 
CREATE TABLE messages_status (
message_id INTEGER REFERENCES messages(id),
user_id INTEGER REFERENCES users(id),
status status NOT NULL
) was aborted: ERROR: relation "messages" does not exist  Call getNextException to see other errors in the batch. backing out 

I'm confused because I don't understand:

Migratus does not use a single global version for a store. It considers each migration independently, and runs all uncompleted migrations in sorted order.

Sorry if this is a dumb question, I'm still working my way through Web Development with Clojure, Third Edition so I'm pretty new to this.

yogthos commented 5 years ago

Hi,

The migrations are run based in order of the timestamps, but it looks like the db might've gotten into a bad state. Migratus creates a table called schema_migrations that tracks the migrations that have already been applied. The ones in the table will be skipped when migrations are run. One thing to try would be to run (user/reset-db) from the REPL.

little-dude commented 5 years ago

@yogthos thank you, I dropped schema_migrations and the custom datatype and it all works now!

yogthos commented 5 years ago

👍