feat(db): add SQLx migrations and AppState with connection pool

- Add sqlx 0.8 (AnyPool, runtime-tokio, sqlite, postgres, migrate)
- Create 6 migration files for both SQLite and PostgreSQL backends
- Add server/db.rs: create_pool and run_migrations helpers
- Add server/state.rs: AppState with LeptosOptions + AnyPool
- Run migrations at server startup before accepting requests
- Fix Port model: remove host_id (ports are now a global catalog)
- Add HostPort join struct for the host_ports many-to-many table

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-15 21:46:16 +02:00
parent 18804e740c
commit f13097591c
19 changed files with 1192 additions and 23 deletions

View File

@@ -0,0 +1,6 @@
-- networks: IP address ranges managed by the IPAM.
-- BIGSERIAL: auto-incrementing 64-bit integer (PostgreSQL's equivalent of AUTOINCREMENT).
CREATE TABLE IF NOT EXISTS networks (
id BIGSERIAL PRIMARY KEY,
cidr TEXT NOT NULL UNIQUE
);

View File

@@ -0,0 +1,7 @@
-- hosts: physical or virtual machines belonging to a network.
CREATE TABLE IF NOT EXISTS hosts (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
ip TEXT NOT NULL,
network_id BIGINT NOT NULL REFERENCES networks(id) ON DELETE CASCADE
);

View File

@@ -0,0 +1,5 @@
-- ports: global catalog of TCP/UDP port numbers and their known protocol.
CREATE TABLE IF NOT EXISTS ports (
number INTEGER PRIMARY KEY,
description TEXT
);

View File

@@ -0,0 +1,5 @@
-- applications: software stacks that use one or more ports.
CREATE TABLE IF NOT EXISTS applications (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL
);

View File

@@ -0,0 +1,6 @@
-- host_ports: which ports are open on which host (many-to-many).
CREATE TABLE IF NOT EXISTS host_ports (
host_id BIGINT NOT NULL REFERENCES hosts(id) ON DELETE CASCADE,
port_number INTEGER NOT NULL REFERENCES ports(number) ON DELETE CASCADE,
PRIMARY KEY (host_id, port_number)
);

View File

@@ -0,0 +1,6 @@
-- application_ports: which ports an application typically uses (many-to-many).
CREATE TABLE IF NOT EXISTS application_ports (
application_id BIGINT NOT NULL REFERENCES applications(id) ON DELETE CASCADE,
port_number INTEGER NOT NULL,
PRIMARY KEY (application_id, port_number)
);