New blog post is up! How I added search functionality to Rack Root, a FastAPI app.

I cover just about all the Python code I wish I had examples for when I was figuring this out. I go over the database class design, indexes/vectors a little bit, and the API design I ended up implementing.

#blogging #writing #development #search #rackroot #python #backend #fastapi #homelab #learning #databases #sqlalchemy

https://medium.com/@a.j.longchamps/how-i-added-search-functionality-to-rack-root-a-fastapi-app-dbcc772932a6

How I Added Search Functionality to Rack Root, a FastAPI app

The blog post I wish I had when I was figuring out how to add search to a FastAPI app. Includes code samples.

Medium

New blog post is up: how I used Atlas to migrate schemas between different database instances. I discuss my use case a bit and give a concise, useful example.

https://medium.com/@a.j.longchamps/migrating-database-schemas-with-atlas-bd83b969a01d

#blogging #opensource #atlas #atlasgo #database #migration #fastapi #postgres #programming #dba #schema #writing #techwriting #rackroot

Migrating Database Schemas With Atlas

A quick example of using Atlas to migrate database schemas between instances and what I’ve learned along the way.

Medium

I've been able to work on Rack Root some more lately, and been spending a lot of time writing tests for the backend. At some point, I'm going to need to make sure I'm covering ALL the code paths, especially the ones that raise exceptions.

I didn't quite do test-driven development, but I do have most branches of the logic covered for a lot of scenarios. I'm up to 67 tests so far, but I'm sure I'll be over 100 once I add more functionality like associating IPs to devices.

For now though, I'm finally getting to write some of the frontend code to handle DHCP ranges on a given subnet. It does feel good to get back into that code.

#programming #rackroot #python #pytest #opensource #fastapi #vuetify

Search results are working now, all the way through to the front end!

I was curious if I needed to worry about HTTP encoding/decoding of things like spaces/%20, but it looks like everything from the frontend to the backend is helping me with all of that. Search results are coming back as expected, though notably not partial string matches. To find the string DS414 for example, you need the whole DS414 and not just 'DS', which isn't the behavior I want. That'll be the next thing to fix.

I am also going to need to fix how the backend is talking to the database, which may mean fixing all of the fields to be lowercase/snake_case instead of camelCase. I discovered that when I was working on the triggers for finding these search results. My preference would be to name the database field somewhere in the Column(...) definition instead of refactoring most of my API calls, but we'll see what SQLAlchemy has for me there.

Then I'll add search support for networking/DHCP and I can get past this hurdle.

#http #programming #vuetify #fastapi #postgres #sqlalchemy #rackroot #homelab

Tests are passing?! I can go to bed. Yay.

Mini Rack Root update: I've been working on search functionality, because I don't have enough in the backlog already for Rack Root, and I've discovered just how little I know about database searches. After MANY iterations back and forth on all the ways not to do it, I've finally found a working piece of code.

I even used ChatGPT, which I almost never do and it was mostly helpful. Sometimes a search engine won't quite get you there and that's how ChatGPT helped. Here's an example with code, help me fix it.

I've also had oh-so-much fun with database schemas being case sensitive (camel case is out, snake case is in), triggers going...somewhere unknown, calling make_searchable() in the right places, and making sure my test environment was _really_ doing what it should have when I dropped the tables to recreate everything. Testing is close to, but not the same as, day to day use when an app is running, of course.

That's a really good stopping point for tonight and I will take it. 55 tests, all green.

#homelab #programming #rackroot #opensource #sqlalchemy #python #postgresql #alwayslearning

Rack Root update: tonight I finally got the search page working and passing parameters the way I need it to. There's a search box in the top right hand corner that will take in a given string and (eventually) go ask the API for objects that match the search terms.

This took a while to put together since entering a search term will redirect you to `/search?q=foo` and that renders properly the first time. With Vuetify however, if you enter another search term while on that page, the app sees you're already on `/search` and doesn't update the values on the page, even though the search parameter is different.

The solution was thankfully found in a GitHub issue (linked below) where other people were looking for the same kind of functionality. This also taught me about the Navigation Guards that Vue/Vuetify come with to handle page navigation/movements. The app knows when you're on the same page and will make decisions based on that.

During my testing I just echoed the search parameter back to the user, but now that the hard part is done, I just need to write the API for it and render that on the frontend. Surely there won't be any complications with asking the API for multiple different objects back... that'll really test my database and python skills.

https://github.com/vuejs/router/issues/1257#issuecomment-2072052011

#rackroot #programming #homelab #justkeeplearning #vuetify #webdev #developer #fastapi #python

It's been a while since I've posted an update for Rack Root, but I've actually made some progress on it lately.

I've finished my refactor to use SQLAlchemy for the database connections and also (finally) figured out how I want to join tables together and relate things.

I also changed some API endpoints. For example, if you look for /network/$id/gateway and there's no gateway, you get a 404. I also changed the UX around gateways. When you make a new network, there's no gateway assigned, so that part of the page has a green + icon. When you set a gateway, that icon is now a red delete icon. Those update the field and icon dynamically without having to refresh the whole page.

The new networks page also will look at the result and see if you got an HTTP 201. If so, you get redirected to the new page. Else, you get an error message. I'm not sure if I want to add more detail to that, it would require the frontend to parse and guess what went wrong. Maybe that's an assignment for another day.

Finally, there's a new Delete network button on the network detail page.

#rackroot #homelab #vuetify #fastapi #sqlalchemy #frontend #backend #fullstack #pytest

I think I finally have my SQLModel to SQLAlchemy refactor completed for Rack Root. As of now, I at least have all of my tests passing and typos worked out. I know I need to add some more join statements, but I'll do those as I come across them.

I still have a little bit of cleanup to do in removing commented code, fixing comments, organizing imports, and syncing this back to the main branch, but the hard part is done.

Then I can finally get back to the front end work. I've been on the backend side of things for what feels like months.

#sqlalchemy #fastapi #webdev #backend #databases #opensource #rackroot

I'm back in the code for Rack Root tonight and realized why I have some of the database relationships I was thinking of getting rid of yesterday. I need ways to track allocations of IP addresses in DHCP ranges and the way I have it setup, there's a nullable foreign key from an IP record over to a DHCP range.

The code for allocating/deallocating an IP in there is easy - if there's a FK set, then you can't set another one.

This is also where I really wish there were better examples of ForeignKey and ForeignKeyConstraints in SQLModel. So far, my searching hasn't turned anything up which might be an opportunity for a blog post and/or pull request.

#programming #opensource #rackroot #databases

I need to learn more about how databases work and the best way to setup relationships in SQLModel. I was working on Rack Root tonight and am still running into an issue with some circular dependencies I'm having between various tables. I need these relationships to enhance the data/utility of the app, but might need to rethink how I'm actually putting them together.

The other fun part of this is that I'm learning that the way I test the app and the way I run the app isn't always the same. The tests will run fine the first time against my test Postgres container, but subsequent runs don't work because of how I tell the test DB to drop all data before running the tests. It's complaining about the relationships, but really I think I need to tell it to either cascade delete or just ignore something.

#homelab #rackroot #programming #webdev #fastapi #sqlmodel #databases #alwayslearning