
For my Coding with Callie website builder to function properly, an admin user (aka me), a home page, and a wild card page need to exist in my database π©π»βπ»
So, I wrote a Seed function that my Go API calls when it starts up to create everything automatically π±
β It pulls my admin password from my environment variables, hashes that password using bcrypt, and then adds an admin user to the Users table.
β Then, it creates a published home page so that end users have something to see immediately when they load βwww.coding-with-callie.comβ.
β Finally, it creates a wildcard page so that any other route an end user types into their browser shows a βPage Not Foundβ component.
A potential issue with this plan is that we only want the Seed function to add these items if they donβt already exist. I didnβt want to make extra database calls to check for an admin user, home page, or wildcard page before first; so, instead, I added a unique constraint on the username and email columns in the Users table and the menu_name and path columns in the Pages table π π»ββοΈ
Then, I added βON CONFLICT (columns with unique constraints) DO NOTHINGβ to my SQL commands. This way, my Seed function works as I intended and only adds the data I want to the database one time π₯³
What do you think?! π€