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:
6
migrations/sqlite/0001_create_networks.sql
Normal file
6
migrations/sqlite/0001_create_networks.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
-- networks: IP address ranges managed by the IPAM.
|
||||
-- Each network has a unique CIDR block (e.g. "192.168.1.0/24").
|
||||
CREATE TABLE IF NOT EXISTS networks (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
cidr TEXT NOT NULL UNIQUE
|
||||
);
|
||||
9
migrations/sqlite/0002_create_hosts.sql
Normal file
9
migrations/sqlite/0002_create_hosts.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
-- hosts: physical or virtual machines belonging to a network.
|
||||
-- The ip field must fall within the CIDR of the parent network
|
||||
-- (enforced in application code, not at the DB level).
|
||||
CREATE TABLE IF NOT EXISTS hosts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
ip TEXT NOT NULL,
|
||||
network_id INTEGER NOT NULL REFERENCES networks(id) ON DELETE CASCADE
|
||||
);
|
||||
7
migrations/sqlite/0003_create_ports.sql
Normal file
7
migrations/sqlite/0003_create_ports.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
-- ports: global catalog of TCP/UDP port numbers and their known protocol.
|
||||
-- Ports are not tied to a specific host here — host_ports handles that link.
|
||||
-- Port numbers range from 0 to 65535 (fits in INTEGER).
|
||||
CREATE TABLE IF NOT EXISTS ports (
|
||||
number INTEGER PRIMARY KEY,
|
||||
description TEXT
|
||||
);
|
||||
6
migrations/sqlite/0004_create_applications.sql
Normal file
6
migrations/sqlite/0004_create_applications.sql
Normal file
@@ -0,0 +1,6 @@
|
||||
-- applications: software stacks that use one or more ports.
|
||||
-- Examples: "Nginx", "PostgreSQL", "Prometheus".
|
||||
CREATE TABLE IF NOT EXISTS applications (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
7
migrations/sqlite/0005_create_host_ports.sql
Normal file
7
migrations/sqlite/0005_create_host_ports.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
-- host_ports: which ports are open on which host (many-to-many).
|
||||
-- Composite primary key prevents duplicate (host, port) pairs.
|
||||
CREATE TABLE IF NOT EXISTS host_ports (
|
||||
host_id INTEGER 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)
|
||||
);
|
||||
8
migrations/sqlite/0006_create_application_ports.sql
Normal file
8
migrations/sqlite/0006_create_application_ports.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
-- application_ports: which ports an application typically uses (many-to-many).
|
||||
-- port_number is not a strict FK to ports to allow registering an application
|
||||
-- before its port entry exists in the catalog.
|
||||
CREATE TABLE IF NOT EXISTS application_ports (
|
||||
application_id INTEGER NOT NULL REFERENCES applications(id) ON DELETE CASCADE,
|
||||
port_number INTEGER NOT NULL,
|
||||
PRIMARY KEY (application_id, port_number)
|
||||
);
|
||||
Reference in New Issue
Block a user