vapor / vapor

💧 A server-side Swift HTTP web framework.
https://vapor.codes
MIT License
24.48k stars 1.44k forks source link

Migrations not running on Vapor 4 beta template. #2131

Closed Rubenfer closed 4 years ago

Rubenfer commented 4 years ago

I've tried to setup a simple Vapor 4 project and when I configure it the migrations doesn't run.

Steps to reproduce

vapor new Vapor4 --branch=4
cd Vapor4
open -a Xcode Package.swift

Expected behavior

When the project builds and run, if you try to access http://localhost:8080/todos it returns an empty array of todos.

Expected JSON response: []

Actual behavior

http://localhost:8080/todos returns [ ERROR ] error: no such table: todos When the server starts no info messages about migrations, so looks like the migrations are not running.

Actual JSON response:

{
    "error": true,
    "reason": "error: no such table: todos"
}

Environment

Configuration file contains:

func configure(_ app: Application) throws {
    app.databases.use(.sqlite(file: "db.sqlite"), as: .sqlite)
    app.migrations.add(CreateTodo())    
    try routes(app)
}
calebkleveter commented 4 years ago

In Fluent 4, you need to run the migrations separately from running the server:

vapor run migrate

I believe there is a flag that you can set for auto-migrating, but I don't remember what that is.

Rubenfer commented 4 years ago

Thanks @calebkleveter. Working and the flag to auto migrate is --auto-migrate.

florinadrianodagiu commented 4 years ago

I experienced the same "error: no such table:" situation. From what Rubenfer sais, "vapor run migrate" seems to have solved his issue.

Not my case, though.

What happened: "vapor run migrate" created my db.sqlite file inside my project's folder (neighboring Package.swift). When the application started, it searched for it in /Users/myUserName/Library/Developer/Xcode/DerivedData/MyProjectName-followed_by_some_long_mumbo_jumbo_string/Build/Products/Debug.

Simply moving the db.sqlite file inside the DerivedData_based contraption solved my problem (as a test --- I feel it should live (and be searched for) in a better chosen location...)

Rubenfer commented 4 years ago

@florinadrianodagiu You need set the working directory of your project to Package.swift folder. You can change it on schema settings > Run > Options.

dmoroz0v commented 11 months ago

I call app.autoMigrate()

app.databases.use(DatabaseConfigurationFactory.sqlite(.file("db.sqlite")), as: .sqlite)
app.migrations.add(CreateFlowStorage())
app.autoMigrate()