Migration and models for worlds/plugin_worlds

This commit is contained in:
Tyler Hallada 2021-07-19 23:47:00 -04:00
parent 7eba0c6adc
commit 0e0fdfd59d
7 changed files with 109 additions and 4 deletions

View File

@ -0,0 +1,23 @@
CREATE TABLE IF NOT EXISTS "worlds" (
"id" SERIAL PRIMARY KEY NOT NULL,
"form_id" INTEGER NOT NULL,
"master" VARCHAR(255) NOT NULL,
"created_at" timestamp(3) NOT NULL,
"updated_at" timestamp(3) NOT NULL
);
CREATE UNIQUE INDEX "worlds_unique_form_id_and_master" ON "worlds" ("form_id", "master");
CREATE TABLE IF NOT EXISTS "plugin_worlds" (
"id" SERIAL PRIMARY KEY NOT NULL,
"plugin_id" INTEGER REFERENCES "plugins"(id) NOT NULL,
"world_id" INTEGER REFERENCES "worlds"(id) NOT NULL,
"editor_id" VARCHAR(255) NOT NULL,
"created_at" timestamp(3) NOT NULL,
"updated_at" timestamp(3) NOT NULL
);
CREATE UNIQUE INDEX "plugin_worlds_unique_plugin_id_and_world_id" ON "plugin_worlds" ("plugin_id", "world_id");
CREATE INDEX "plugin_worlds_world_id" ON "plugin_worlds" ("world_id");
ALTER TABLE "cells" ADD COLUMN "world_id" INTEGER REFERENCES "worlds"(id);
CREATE UNIQUE INDEX "cells_unique_form_id_and_world_id" ON "cells" ("form_id", "world_id");
DROP INDEX "cells_unique_form_id";

View File

@ -75,6 +75,8 @@ where
cell.form_id.try_into().unwrap(),
cell.x,
cell.y,
// TODO: fill in world_id here
None,
cell.is_persistent,
)
.await?;

View File

@ -9,6 +9,8 @@ pub struct Cell {
pub form_id: i32,
pub x: Option<i32>,
pub y: Option<i32>,
// TODO: make this not nullable
pub world_id: Option<i32>,
pub is_persistent: bool,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
@ -20,20 +22,22 @@ pub async fn insert(
form_id: i32,
x: Option<i32>,
y: Option<i32>,
world_id: Option<i32>,
is_persistent: bool,
) -> Result<Cell> {
sqlx::query_as!(
Cell,
"INSERT INTO cells
(form_id, x, y, is_persistent, created_at, updated_at)
VALUES ($1, $2, $3, $4, now(), now())
ON CONFLICT (form_id) DO UPDATE
(form_id, x, y, world_id, is_persistent, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, now(), now())
ON CONFLICT (form_id, world_id) DO UPDATE
SET (x, y, is_persistent, updated_at) =
(EXCLUDED.x, EXCLUDED.y, EXCLUDED.is_persistent, now())
RETURNING *",
form_id,
x,
y,
world_id,
is_persistent
)
.fetch_one(pool)

View File

@ -4,3 +4,5 @@ pub mod game;
pub mod game_mod;
pub mod plugin;
pub mod plugin_cell;
pub mod plugin_world;
pub mod world;

View File

@ -2,6 +2,7 @@ use anyhow::{Context, Result};
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use tracing::instrument;
#[derive(Debug, Serialize, Deserialize)]
pub struct PluginCell {
pub id: i32,
@ -33,5 +34,5 @@ pub async fn insert(
)
.fetch_one(pool)
.await
.context("Failed to insert cell")
.context("Failed to insert plugin_cell")
}

View File

@ -0,0 +1,38 @@
use anyhow::{Context, Result};
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use tracing::instrument;
#[derive(Debug, Serialize, Deserialize)]
pub struct PluginWorld {
pub id: i32,
pub plugin_id: i32,
pub world_id: i32,
pub editor_id: String,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
}
#[instrument(level = "debug", skip(pool))]
pub async fn insert(
pool: &sqlx::Pool<sqlx::Postgres>,
plugin_id: i32,
world_id: i32,
editor_id: &str,
) -> Result<PluginWorld> {
sqlx::query_as!(
PluginWorld,
"INSERT INTO plugin_worlds
(plugin_id, world_id, editor_id, created_at, updated_at)
VALUES ($1, $2, $3, now(), now())
ON CONFLICT (plugin_id, world_id) DO UPDATE
SET (editor_id, updated_at) = (EXCLUDED.editor_id, now())
RETURNING *",
plugin_id,
world_id,
editor_id,
)
.fetch_one(pool)
.await
.context("Failed to insert plugin_world")
}

35
src/models/world.rs Normal file
View File

@ -0,0 +1,35 @@
use anyhow::{Context, Result};
use chrono::NaiveDateTime;
use serde::{Deserialize, Serialize};
use tracing::instrument;
#[derive(Debug, Serialize, Deserialize)]
pub struct World {
pub id: i32,
pub form_id: i32,
pub master: String,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
}
#[instrument(level = "debug", skip(pool))]
pub async fn insert(
pool: &sqlx::Pool<sqlx::Postgres>,
form_id: i32,
master: &str,
) -> Result<World> {
sqlx::query_as!(
World,
"INSERT INTO worlds
(form_id, master, created_at, updated_at)
VALUES ($1, $2, now(), now())
ON CONFLICT (form_id, master) DO UPDATE
SET updated_at = now()
RETURNING *",
form_id,
master
)
.fetch_one(pool)
.await
.context("Failed to insert world")
}