Skip to content

Commit

Permalink
Merge pull request #264 from Northeastern-Electric-Racing/charybdis-2…
Browse files Browse the repository at this point in the history
….0-setup

Charybdis Setup
  • Loading branch information
Peyton-McKee authored Jan 16, 2025
2 parents 9eb1c79 + 61cb671 commit 28dffa1
Show file tree
Hide file tree
Showing 29 changed files with 1,203 additions and 103 deletions.
6 changes: 6 additions & 0 deletions charybdis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
# Keep environment variables out of version control
.env

cloud-prisma/cloud-prisma-client/*
local-prisma/local-prisma-client/*
19 changes: 19 additions & 0 deletions charybdis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Charybdis

make sure you [install yarn](https://classic.yarnpkg.com/lang/en/docs/install/#mac-stable) if you don't have it already.

and install prisma globally... it's just easier that way(if you have trouble just follow this: [prisma install instructions](https://www.prisma.io/docs/orm/tools/prisma-cli)).

To initialize the databases run `yarn database:setup` (if this command fails, you should run `yarn database:teardown` before running again.)

You can also individually teardown and bringup the databases by running `yarn database:cloud:setup` or `yarn database:local:setup` and `yarn database:cloud:teardown` or `yarn database:local:teardown`.

To teardown all the databases run `yarn database:teardown`

When updating the local database schema, _Delete the existing migrations files in local-prisma_ and then run `yarn prisma:local:migrate:dev`. Ensure to name the file `init`. This will create the migration file, then run a script to enforce NOT NULL constraints that prisma gurantees put refuses to support in migration sql, copy it to scylla and rerun the diesel migration onto the database to ensure consistency between the two projects, it will also update the diesel schema.rs file according to the new migration (this should be checked to ensure it is based on the expected types, made in your prisma schema).

IMPORTANT: the above will fail if you have more than the two expect migration folder in /scylla-server/migrations : _00000000000000_diesel_initial_setup_, and the working migration folder: _2024-11-10-031516_create_all_ <--- this will we be where the diesel migration file that is getting updated.

When updating the cloud schema, only run `yarn prisma:migrate:cloud:dev` and name it appropriately to whatever changes were made. Ensure that when deploying migrations to the cloud you only run `yarn prisma:migrate:cloud`. You will also have to change the cloud database url in your .env file to point to the cloud database

To access the prisma client for each respective database ensure that you use the proper prisma client from the cloud-prisma and local-prisma prisma files respectively. AKA to access the cloud prisma ensure you are importing `prisma` from `cloud-prisma/prisma.ts`. To access local prisma ensure you are importing `prisma` from `local-prisma/prisma.ts`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'timescaledb') THEN
CREATE EXTENSION timescaledb;
END IF;
END $$;

-- CreateTable
CREATE TABLE "run" (
"id" TEXT NOT NULL,
"runId" SERIAL NOT NULL,
"driverName" TEXT NOT NULL,
"notes" TEXT NOT NULL,
"time" TIMESTAMPTZ NOT NULL,

CONSTRAINT "run_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "data" (
"values" DOUBLE PRECISION[],
"time" TIMESTAMPTZ NOT NULL,
"dataTypeName" TEXT NOT NULL,
"runId" TEXT NOT NULL,

CONSTRAINT "data_pkey" PRIMARY KEY ("time","dataTypeName")
);

-- CreateTable
CREATE TABLE "data_type" (
"name" TEXT NOT NULL,
"unit" TEXT NOT NULL,
"nodeName" TEXT NOT NULL,

CONSTRAINT "data_type_pkey" PRIMARY KEY ("name")
);

-- AddForeignKey
ALTER TABLE "data" ADD CONSTRAINT "data_dataTypeName_fkey" FOREIGN KEY ("dataTypeName") REFERENCES "data_type"("name") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "data" ADD CONSTRAINT "data_runId_fkey" FOREIGN KEY ("runId") REFERENCES "run"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

SELECT * FROM create_hypertable('data', by_range('time'));
SELECT * FROM add_dimension('data', by_hash('dataTypeName', 4));

ALTER TABLE "data" SET (timescaledb.compress,
timescaledb.compress_orderby = 'time DESC',
timescaledb.compress_segmentby = '"runId", "dataTypeName"',
timescaledb.compress_chunk_time_interval='24 hours'
);

SELECT add_compression_policy('data', compress_after => INTERVAL '1d');
3 changes: 3 additions & 0 deletions charybdis/cloud-prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"
3 changes: 3 additions & 0 deletions charybdis/cloud-prisma/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PrismaClient } from "./cloud-prisma-client";

export const prisma = new PrismaClient();
42 changes: 42 additions & 0 deletions charybdis/cloud-prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
output = "cloud-prisma-client"
}

datasource db {
provider = "postgresql"
url = env("CLOUD_DATABASE_URL")
}

model run {
id String @id @default(uuid())
runId Int @default(autoincrement())
driverName String
notes String
time DateTime @db.Timestamptz()
data data[]
}

model data {
values Float[]
time DateTime @db.Timestamptz()
dataTypeName String
dataType data_type @relation(fields: [dataTypeName], references: [name])
runId String
run run @relation(fields: [runId], references: [id])
@@id([time, dataTypeName])
}

model data_type {
name String @id
unit String
nodeName String
data data[]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- CreateTable
CREATE TABLE "run" (
"runId" SERIAL NOT NULL,
"driverName" TEXT NOT NULL DEFAULT '',
"locationName" TEXT NOT NULL DEFAULT '',
"notes" TEXT NOT NULL DEFAULT '',
"time" TIMESTAMPTZ NOT NULL,

CONSTRAINT "run_pkey" PRIMARY KEY ("runId")
);

-- CreateTable
CREATE TABLE "data" (
"values" REAL[] NOT NULL,
"time" TIMESTAMPTZ NOT NULL,
"dataTypeName" TEXT NOT NULL,
"runId" INTEGER NOT NULL,

CONSTRAINT "data_pkey" PRIMARY KEY ("time","dataTypeName")
);

-- CreateTable
CREATE TABLE "data_type" (
"name" TEXT NOT NULL,
"unit" TEXT NOT NULL,
"nodeName" TEXT NOT NULL,

CONSTRAINT "data_type_pkey" PRIMARY KEY ("name")
);

-- AddForeignKey
ALTER TABLE "data" ADD CONSTRAINT "data_dataTypeName_fkey" FOREIGN KEY ("dataTypeName") REFERENCES "data_type"("name") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "data" ADD CONSTRAINT "data_runId_fkey" FOREIGN KEY ("runId") REFERENCES "run"("runId") ON DELETE RESTRICT ON UPDATE CASCADE;
3 changes: 3 additions & 0 deletions charybdis/local-prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (e.g., Git)
provider = "postgresql"
3 changes: 3 additions & 0 deletions charybdis/local-prisma/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PrismaClient } from "./local-prisma-client";

export const prisma = new PrismaClient();
45 changes: 45 additions & 0 deletions charybdis/local-prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
output = "local-prisma-client"
}

datasource db {
provider = "postgresql"
url = env("LOCAL_DATABASE_URL")
}

model run {
runId Int @id @default(autoincrement())
driverName String @default("")
locationName String @default("")
notes String @default("")
time DateTime @db.Timestamptz()
data data[]
}

model data {
values Float[] @db.Real
time DateTime @db.Timestamptz()
dataTypeName String
dataType data_type @relation(fields: [dataTypeName], references: [name])
runId Int
run run @relation(fields: [runId], references: [runId])
@@id([time, dataTypeName])
}

model data_type {
name String @id
unit String
nodeName String
data data[]
}
Loading

0 comments on commit 28dffa1

Please sign in to comment.