Skip to content

Commit

Permalink
refactor: database schema (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 authored Mar 5, 2024
2 parents 3964a1a + 6ebec11 commit a074372
Show file tree
Hide file tree
Showing 28 changed files with 5,225 additions and 556 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

- name: Build
env:
TURSO_URL: 'libsql://resonance-market-yjl9903.turso.io'
TURSO_URL: 'libsql://backup-yjl9903.turso.io'
TURSO_AUTH_TOKEN: ${{ secrets.TURSO_AUTH_TOKEN }}
run: pnpm build --preset=cloudflare_module

Expand Down
4 changes: 1 addition & 3 deletions components/City.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import {
TableRow
} from '@/components/ui/table';
import { type CityInfo, cities } from '~/lib/city';
import Price from './Price.vue';
import CreateLog from './CreateLog.vue';
Expand Down Expand Up @@ -50,7 +48,7 @@ const store = useLatestLogs();
>
</TableHeader>
<TableBody>
<TableRow v-for="product in city.products" :key="product.name">
<TableRow v-for="product in city.products.filter((p) => p.valuable)" :key="product.name">
<TableCell>{{ product.name }}</TableCell>
<TableCell class="border-r"
><Price
Expand Down
2 changes: 0 additions & 2 deletions components/CreateLog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import {
FormMessage
} from '@/components/ui/form';
import { cities, type CityInfo, type ProductInfo } from '~/lib/city';
const props = defineProps<{ city: CityInfo; product: ProductInfo }>();
const open = ref(false);
Expand Down
3 changes: 2 additions & 1 deletion components/Price.vue
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script setup lang="ts">
import { format } from '@formkit/tempo';
import type { Log } from '~/drizzle/schema';
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '@/components/ui/tooltip';
import { format } from '@formkit/tempo';
const props = defineProps<{ timestamp: number; log: Log | undefined }>();
Expand Down
33 changes: 27 additions & 6 deletions drizzle/dev/initialize.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cities } from '../../lib/city';
import { products as allProducts } from '../../utils/products';

import { products, users } from '../schema';
import { products, transactions, users } from '../schema';

import { database } from './connect';

Expand All @@ -10,11 +10,32 @@ await database
.onConflictDoNothing()
.returning({ id: users.id });

for (const city of cities) {
for (const product of city.products) {
for (const product of allProducts) {
await database
.insert(products)
.values({
city: product.city,
name: product.name,
type: product.type,
valuable: product.valuable,
baseVolume: product.baseVolume,
basePrice: product.basePrice,
cost: product.cost
})
.onConflictDoNothing();
}

for (const product of allProducts) {
for (const transaction of product.transactions) {
await database
.insert(products)
.values({ city: city.name, name: product.name })
.insert(transactions)
.values({
name: transaction.name,
sourceCity: transaction.sourceCity,
targetCity: transaction.targetCity,
mileage: transaction.mileage,
basePrice: transaction.basePrice
})
.onConflictDoNothing();
}
}
45 changes: 45 additions & 0 deletions drizzle/migrations/0000_demonic_namorita.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
CREATE TABLE `logs` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`source_city` text NOT NULL,
`target_city` text NOT NULL,
`type` text NOT NULL,
`trend` text NOT NULL,
`price` integer NOT NULL,
`percent` integer NOT NULL,
`uploaded_at` integer NOT NULL,
`uploader_id` integer NOT NULL,
FOREIGN KEY (`uploader_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`source_city`,`name`) REFERENCES `products`(`city`,`name`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`name`,`source_city`,`target_city`) REFERENCES `transactions`(`name`,`source_city`,`target_city`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `products` (
`name` text NOT NULL,
`city` text NOT NULL,
`type` text NOT NULL,
`valuable` integer NOT NULL,
`base_volume` integer,
`base_price` integer NOT NULL,
`cost` integer,
PRIMARY KEY(`city`, `name`)
);
--> statement-breakpoint
CREATE TABLE `transactions` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL,
`source_city` text NOT NULL,
`target_city` text NOT NULL,
`mileage` integer NOT NULL,
`base_price` integer NOT NULL,
FOREIGN KEY (`source_city`,`name`) REFERENCES `products`(`city`,`name`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `users` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`name` text NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `logs_name_source_city_target_city_uploaded_at_unique` ON `logs` (`name`,`source_city`,`target_city`,`uploaded_at`);--> statement-breakpoint
CREATE UNIQUE INDEX `transactions_name_source_city_target_city_unique` ON `transactions` (`name`,`source_city`,`target_city`);--> statement-breakpoint
CREATE UNIQUE INDEX `users_name_unique` ON `users` (`name`);
26 changes: 0 additions & 26 deletions drizzle/migrations/0000_true_doctor_doom.sql

This file was deleted.

1 change: 0 additions & 1 deletion drizzle/migrations/0001_tricky_emma_frost.sql

This file was deleted.

1 change: 0 additions & 1 deletion drizzle/migrations/0002_breezy_ikaris.sql

This file was deleted.

161 changes: 154 additions & 7 deletions drizzle/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "5",
"dialect": "sqlite",
"id": "444952ed-6ff9-46f5-ae3c-ea5d564417f3",
"id": "8f304cf5-0165-44ba-b4c8-68ea9b2d2b13",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"logs": {
Expand All @@ -21,8 +21,15 @@
"notNull": true,
"autoincrement": false
},
"city": {
"name": "city",
"source_city": {
"name": "source_city",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"target_city": {
"name": "target_city",
"type": "text",
"primaryKey": false,
"notNull": true,
Expand All @@ -35,8 +42,8 @@
"notNull": true,
"autoincrement": false
},
"target_city": {
"name": "target_city",
"trend": {
"name": "trend",
"type": "text",
"primaryKey": false,
"notNull": true,
Expand Down Expand Up @@ -71,7 +78,18 @@
"autoincrement": false
}
},
"indexes": {},
"indexes": {
"logs_name_source_city_target_city_uploaded_at_unique": {
"name": "logs_name_source_city_target_city_uploaded_at_unique",
"columns": [
"name",
"source_city",
"target_city",
"uploaded_at"
],
"isUnique": true
}
},
"foreignKeys": {
"logs_uploader_id_users_id_fk": {
"name": "logs_uploader_id_users_id_fk",
Expand All @@ -91,7 +109,7 @@
"tableFrom": "logs",
"tableTo": "products",
"columnsFrom": [
"city",
"source_city",
"name"
],
"columnsTo": [
Expand All @@ -100,6 +118,23 @@
],
"onDelete": "no action",
"onUpdate": "no action"
},
"transaction_log_fk": {
"name": "transaction_log_fk",
"tableFrom": "logs",
"tableTo": "transactions",
"columnsFrom": [
"name",
"source_city",
"target_city"
],
"columnsTo": [
"name",
"source_city",
"target_city"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
Expand All @@ -121,6 +156,41 @@
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"type": {
"name": "type",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"valuable": {
"name": "valuable",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"base_volume": {
"name": "base_volume",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
},
"base_price": {
"name": "base_price",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"cost": {
"name": "cost",
"type": "integer",
"primaryKey": false,
"notNull": false,
"autoincrement": false
}
},
"indexes": {},
Expand All @@ -136,6 +206,83 @@
},
"uniqueConstraints": {}
},
"transactions": {
"name": "transactions",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": true
},
"name": {
"name": "name",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"source_city": {
"name": "source_city",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"target_city": {
"name": "target_city",
"type": "text",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"mileage": {
"name": "mileage",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
},
"base_price": {
"name": "base_price",
"type": "integer",
"primaryKey": false,
"notNull": true,
"autoincrement": false
}
},
"indexes": {
"transactions_name_source_city_target_city_unique": {
"name": "transactions_name_source_city_target_city_unique",
"columns": [
"name",
"source_city",
"target_city"
],
"isUnique": true
}
},
"foreignKeys": {
"transaction_source_fk": {
"name": "transaction_source_fk",
"tableFrom": "transactions",
"tableTo": "products",
"columnsFrom": [
"source_city",
"name"
],
"columnsTo": [
"city",
"name"
],
"onDelete": "no action",
"onUpdate": "no action"
}
},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
},
"users": {
"name": "users",
"columns": {
Expand Down
Loading

0 comments on commit a074372

Please sign in to comment.