vapor / fluent

Vapor ORM (queries, models, and relations) for NoSQL and SQL databases
https://docs.vapor.codes/4.0/fluent/overview/
MIT License
1.32k stars 172 forks source link

Fluent cannot decode utf8mb4_unicode_ci to string from mysql #714

Open pankajsoni19 opened 3 years ago

pankajsoni19 commented 3 years ago

Describe the bug

Fail to decode data correctly to string from mysql.

To Reproduce

Mariadb: AWS RDS

> SELECT VERSION();
>> 10.4.13-MariaDB-log
CREATE TABLE `channels` (
  `channel` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
  PRIMARY KEY (`channel`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Insert into table

INSERT INTO `channels` (`channel`)
VALUES ('zafé');

Vapor model: Channel.swift


import Vapor
import Fluent
import FluentSQL

final class Channel: Model {

    static let schema = "channels"

    public init() { }

    @ID(custom: "channel", generatedBy: .user)
    var id: String?

    @Timestamp(key: "created_at", on: .create)
    var created_at: Date?
}

extension Channel: Content { }

Read value

Channel.query(on: appInstance.db).all().whenSuccess { (channels: [Channel]) in
    channels.forEach { (channel: Channel) in
        print("meta: \(channel.id) ")
    }
}

Expected: meta: zafé Actual: meta: zaf�

Package.swift

// swift-tools-version:5.3
import PackageDescription

let package = Package(
    name: "Server",
    platforms: [
        .macOS(.v10_15)
    ],
    products: [
        .library(name: "Server", targets: ["App"]), // product name
    ],
    dependencies: [
        .package(url: "https://github.com/vapor/vapor.git", from: "4.35.0"), //vapor dep
        .package(url: "https://github.com/vapor/leaf.git", from: "4.0.0"),
        .package(url: "https://github.com/soto-project/soto.git", from: "5.0.0"), //aws sdk
        .package(url: "https://github.com/vapor/fluent.git", from: "4.0.0"),    // DB ORM
        .package(url: "https://github.com/vapor/fluent-mysql-driver.git", from: "4.0.0"), //mysql
        .package(url: "https://github.com/vapor/redis.git", from: "4.0.0"), //redis
        .package(url: "https://github.com/CoreOffice/CoreXLSX.git", .upToNextMinor(from: "0.13.0")), //xlsx decoding
        .package(url: "https://github.com/pankajsoni19/swift-backtrace.git", from: "1.4.0") //swift error logging utility
    ],
    targets: [
        .target(
            name: "App",
            dependencies: [
                .product(name: "SotoS3", package: "soto"),
                .product(name: "SotoSES", package: "soto"),

                .product(name: "Leaf", package: "leaf"),
                .product(name: "Fluent", package: "fluent"),
                .product(name: "FluentMySQLDriver", package: "fluent-mysql-driver"),
                .product(name: "Vapor", package: "vapor"),

                .product(name: "Redis", package: "redis"),
                .product(name: "CoreXLSX", package: "CoreXLSX"),
                .product(name: "Backtrace", package: "swift-backtrace")
            ],
            swiftSettings: [
                // Enable better optimizations when building in Release configuration. Despite the use of
                // the `.unsafeFlags` construct required by SwiftPM, this flag is recommended for Release
                // builds. See <https://github.com/swift-server/guides#building-for-production> for details.
                .unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
        ]),
        .target(name: "Run", dependencies: [
            .target(name: "App")
        ])
    ]
)