Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Defaults for Laravel Scanner: DB, Redis #3090

Merged
merged 14 commits into from
Dec 18, 2023

Conversation

KTanAug21
Copy link
Contributor

@KTanAug21 KTanAug21 commented Dec 4, 2023

Change Summary

What and Why:
We need to set defaults for apps deployed in Fly.io, see here!. This is also true for Laravel apps. So this PR contains changes to detect DBs and redis connection set in the local .env file, and set those as defaults for the Laravel Fly App, closely following the changes already available in the rails scanner.

How:
Checks the .env file of a Laravel directory and scans for db connection strings like "mysql", "pgsql", and "sqlite". If any of the connections above are available, sets it as the default DesiredDatabase value.
Additionally, detects the "redis" keyword and sets the configuration value of "RedisDesired" as true.

Related to:

  1. Setting of defaults Fresh Produce
  2. The Rails Scanner( this PR would take inspiration on the rails scanner for detecting defaults )

Documentation

(will add docs in the laravel section of our docs)

  • Fresh Produce
  • In superfly/docs, or asked for help from docs team
  • n/a

@KTanAug21 KTanAug21 marked this pull request as ready for review December 10, 2023 10:46

// Set up Regex to match
// -not commented out, with DB_CONNECTION
dbReg := regexp.MustCompile("DB_CONNECTION *= *[a-zA-Z]+")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this regex matches # DB_CONNECTION = ..., does it? (comment line)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!! The regex above is incorrect! It ain't gonna ignore commented out DB_CONNECTIONS, thanks for the eagle eyes on this!

Fixed this by only accepting whitespace before the DB_CONNECTION and reject variations with other characters before the string, ultimately ignoring lines where comment characters are added!

Copy link
Member

@dangra dangra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can certainly be improved but looks good as first iteration!

@KTanAug21
Copy link
Contributor Author

It can certainly be improved but looks good as first iteration!

Hi @dangra! Thank you for taking the time check my PR, I appreciate it! I've fixed the issue you've pointed above, and I'm eager to know how I can improve it, so when you're free, please do list some notes I can use to improve my PR

Comment on lines 119 to 123
/*
Determine the default db
Determine whether redis connection is desired
returns ( db, redis, skipDB )
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See how to document Functions https://tip.golang.org/doc/comment#func

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! this helps, let me work on it, thank you!

Comment on lines 132 to 136
// Set up Regex to match
// -not commented out, with DB_CONNECTION
dbReg := regexp.MustCompile("^ *DB_CONNECTION *= *[a-zA-Z]+")
// -not commented out with redis keyword
redisReg := regexp.MustCompile("^[^#]*redis")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static regular patterns (and its regex.MustCompile() calls) are ideal as global variables as they can catch regex syntax errors earlier. More details at https://stackoverflow.com/a/72185182

Comment on lines 153 to 161
if strings.Contains(text, "mysql") {
db = DatabaseKindMySQL
skipDb = false
} else if strings.Contains(text, "pgsql") {
db = DatabaseKindPostgres
skipDb = false
} else if strings.Contains(text, "sqlite") {
db = DatabaseKindSqlite
skipDb = false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

text contains is very loose matching, a better approach would URI parsing. See https://gobyexample.com/url-parsing

Copy link
Contributor

@fideloper fideloper Dec 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Laravel's config isn't a connection string that'll work with parseURL, - in this case DB_CONNECTION is one of mysql, pgsql, sqlite, sqlsrv and in rarer cases, something custom. It's not a full DB connection string tho.

(There is a lesser-used DATABASE_URL env var that Laravel can use, but it's rarer to see used).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fideloper TIL. The PR is fine as it is. My comment is a nitpick of things that can be improved.

In this case it can use regex group matching to extract the value from the line and compare more strictly by using switch/case Go idiom.

And it is good that you mention it, support for DATABASE_URL would be good to have too, not a must ofc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it is good that you mention it, support for DATABASE_URL would be good to have too, not a must ofc.

Agree!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Team thanks!! I added the DATABSE_URL case in the regex to check connections from!

@KTanAug21 KTanAug21 merged commit 301c8e2 into master Dec 18, 2023
42 checks passed
@KTanAug21 KTanAug21 deleted the detect_db_defaults_laravel_scanner branch December 18, 2023 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants