Swiftpack.co - MatsMoll/AutoMigrator as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by MatsMoll.
MatsMoll/AutoMigrator 0.1.0
Auto Migration generation for Vapor projects
⭐️ 10
🕓 2 years ago
iOS macOS
.package(url: "https://github.com/MatsMoll/AutoMigrator.git", from: "0.1.0")

AutoMigrator

A package that generates version based migrations from Fluent.Model types.

The project will check your database scheme and understand when you have added, changed or deleted a table.

.package(url: "https://github.com/MatsMoll/AutoMigrator", from: "0.1.0")

All that is required is to add a new target in your product, with a main.swift file and add the following code

@testable import YourApp
import Vapor
import AutoMigrator

var env = try Environment.detect()
let app = Application(env)

// Setup your database connection
try configure(app)

try AutoMigrator(app: app).generateMigrations(
    tables: [
        generateTable(SomeModel.self),
        generateTable(SomeOtherModel.self),
        ...
    ],
    outputDir: app.directory.workingDirectory
)

Example output could be something like:

extension MigrationBatch2.User: AsyncMigration {
    func prepare(on database: Database) async throws {
        try await database.schema("User")
            .field("email", .string, .required)
            .field("siwaID", .string)
            .update()
    }

    func revert(on database: Database) async throws {
        try await database.schema("User")
            .deleteField("email")
            .deleteField("siwaID")
            .update()
    }
}

extension MigrationBatch2.OtherModel: AsyncMigration {
    func prepare(on database: Database) async throws {
        try await database.schema("WorkoutSummary")
            .field("workoutID", .uuid, .references("Workout", .id, onDelete: .cascade, onUpdate: .cascade))
            .field("id", .uuid, .identifier(auto: false))
            .field("averagePower", .int64)
            .field("normalizedPower", .int64)
            .field("maxPower", .int64)
            .field("averageHeartRate", .int64)
            .field("averageHeartRate", .int64)
            .field("startedAt", .datetime, .required)
            .create()
    }

    func revert(on database: Database) async throws {
        try await database.schema("WorkoutSummary")
            .delete()
    }
}

GitHub

link
Stars: 10
Last commit: 1 year ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics