k1LoW / tbls

tbls is a CI-Friendly tool for document a database, written in Go.
MIT License
3.42k stars 165 forks source link

Planetscale Database Support #567

Closed azoom-joseph-hagan closed 6 months ago

azoom-joseph-hagan commented 6 months ago

What happened

I tried to connect to a Planetscale MySQL database using a .tbls.yml file in my project root like below but could not connect with tbls.

dsn: mysql://dbusername:pscale_password@aws.connect.psdb.cloud/dbname?sslaccept=strict

docPath: doc/schema

What you expected to happened

To be able to generate a document for my database.

What stack trace or error message from tbls did you see?

...% tbls doc
Error 1105 (HY000): unknown error: Code: UNAVAILABLE
server does not allow insecure connections, client must use SSL/TLS```

Anything else we need to know?

If I change the ssl accept to ?ssl={"rejectUnauthorized":true} it still gets the same error.

It works with a locally hosted MySQL database running in a Docker container so the issue lies with Planetscale's SSL/TSL policies.

Environment

MacOS: 14.4.1 (23E224) tbls v1.73.3

Is there anyway I can use tbls with my Planetscale Database?

Thank you.

k1LoW commented 6 months ago

Hi @azoom-joseph-hagan! Thank you for your report.

tbls does not support Planetscale. From the error it looks like a secure connection error, but I have no way to confirm this.

If you can show me the Go code that allows you to connect to Planetscale, I might be able to find out something.

azoom-joseph-hagan commented 6 months ago

Thank you for the quick reply.

I'm not using Go directly myself, but looking at the Planetscale Documentation it seems to connect like this:

`.env`
DSN=username:password@tcp(aws.connect.psdb.cloud)/dbname?tls=true&interpolateParams=true
`main.go`
package main

import (
  "database/sql"
  "log"
  "os"

  _ "github.com/go-sql-driver/mysql"
)

func main() {
    db, err := sql.Open("mysql", os.Getenv("DSN"))
    if err != nil {
        log.Fatalf("failed to connect: %v", err)
    }
    defer db.Close()

    if err := db.Ping(); err != nil {
        log.Fatalf("failed to ping: %v", err)
    }

    log.Println("Successfully connected to PlanetScale!")
}

Thank you again for your time and help.

k1LoW commented 6 months ago

Thank you.

If it can connect with

username:password@tcp(aws.connect.psdb.cloud)/dbname?tls=true&interpolateParams=true

then tbls will connect with

mysql://username:password@aws.connect.psdb.cloud/dbname?tls=true&interpolateParams=true.

azoom-joseph-hagan commented 6 months ago

Thank you so much. That worked. Here is the final working code for connecting PlanetScale DB To tabls for future reference:

# DSN (Database Source Name) to connect database
dsn: mysql://username:password@aws.connect.psdb.cloud/dbname?tls=true&interpolateParams=true

# Path to generate document
docPath: doc/schema