refactor(db): separate workout plans from completed sessions
This commit is contained in:
+81
-2
@@ -54,9 +54,88 @@ CREATE TABLE \`workouts\` (
|
||||
);
|
||||
`;
|
||||
|
||||
const m0001 = `
|
||||
CREATE TABLE \`workout_sessions\` (
|
||||
\`id\` text PRIMARY KEY NOT NULL,
|
||||
\`workout_id\` text,
|
||||
\`name\` text,
|
||||
\`pool_length\` real DEFAULT 25 NOT NULL,
|
||||
\`started_at\` text NOT NULL,
|
||||
\`finished_at\` text,
|
||||
\`total_distance\` integer,
|
||||
\`total_duration\` real,
|
||||
\`calories\` real,
|
||||
\`avg_heart_rate\` integer,
|
||||
\`source\` text DEFAULT 'manual' NOT NULL,
|
||||
FOREIGN KEY (\`workout_id\`) REFERENCES \`workouts\`(\`id\`) ON UPDATE no action ON DELETE no action
|
||||
);
|
||||
--> statement-breakpoint
|
||||
INSERT INTO \`workout_sessions\` (\`id\`, \`workout_id\`, \`name\`, \`pool_length\`, \`started_at\`, \`finished_at\`, \`total_distance\`, \`total_duration\`, \`calories\`, \`avg_heart_rate\`, \`source\`)
|
||||
SELECT \`id\`, \`id\`, \`name\`, \`pool_length\`, \`started_at\`, \`finished_at\`, \`total_distance\`, \`total_duration\`, \`calories\`, \`avg_heart_rate\`, \`source\`
|
||||
FROM \`workouts\`
|
||||
WHERE \`finished_at\` IS NOT NULL;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE \`laps\` RENAME COLUMN \`workout_id\` TO \`session_id\`;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE \`workouts\` DROP COLUMN \`started_at\`;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE \`workouts\` DROP COLUMN \`finished_at\`;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE \`workouts\` DROP COLUMN \`total_distance\`;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE \`workouts\` DROP COLUMN \`total_duration\`;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE \`workouts\` DROP COLUMN \`calories\`;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE \`workouts\` DROP COLUMN \`avg_heart_rate\`;
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE \`workouts\` DROP COLUMN \`source\`;
|
||||
`;
|
||||
|
||||
const m0002 = `
|
||||
PRAGMA foreign_keys=OFF;--> statement-breakpoint
|
||||
CREATE TABLE \`__new_laps\` (
|
||||
\`id\` text PRIMARY KEY NOT NULL,
|
||||
\`session_id\` text NOT NULL,
|
||||
\`set_position\` integer NOT NULL,
|
||||
\`rep_number\` integer NOT NULL,
|
||||
\`distance\` integer NOT NULL,
|
||||
\`duration\` real NOT NULL,
|
||||
\`stroke_count\` integer,
|
||||
\`heart_rate\` integer,
|
||||
\`swolf\` real,
|
||||
FOREIGN KEY (\`session_id\`) REFERENCES \`workout_sessions\`(\`id\`) ON UPDATE no action ON DELETE no action
|
||||
);
|
||||
--> statement-breakpoint
|
||||
INSERT INTO \`__new_laps\`("id", "session_id", "set_position", "rep_number", "distance", "duration", "stroke_count", "heart_rate", "swolf") SELECT "id", "session_id", "set_position", "rep_number", "distance", "duration", "stroke_count", "heart_rate", "swolf" FROM \`laps\`;--> statement-breakpoint
|
||||
DROP TABLE \`laps\`;--> statement-breakpoint
|
||||
ALTER TABLE \`__new_laps\` RENAME TO \`laps\`;--> statement-breakpoint
|
||||
PRAGMA foreign_keys=ON;--> statement-breakpoint
|
||||
CREATE TABLE \`__new_workout_sessions\` (
|
||||
\`id\` text PRIMARY KEY NOT NULL,
|
||||
\`workout_id\` text,
|
||||
\`name\` text,
|
||||
\`pool_length\` real DEFAULT 25 NOT NULL,
|
||||
\`started_at\` text NOT NULL,
|
||||
\`finished_at\` text,
|
||||
\`total_distance\` integer,
|
||||
\`total_duration\` real,
|
||||
\`calories\` real,
|
||||
\`avg_heart_rate\` integer,
|
||||
\`source\` text DEFAULT 'manual' NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
INSERT INTO \`__new_workout_sessions\`("id", "workout_id", "name", "pool_length", "started_at", "finished_at", "total_distance", "total_duration", "calories", "avg_heart_rate", "source") SELECT "id", "workout_id", "name", "pool_length", "started_at", "finished_at", "total_distance", "total_duration", "calories", "avg_heart_rate", "source" FROM \`workout_sessions\`;--> statement-breakpoint
|
||||
DROP TABLE \`workout_sessions\`;--> statement-breakpoint
|
||||
ALTER TABLE \`__new_workout_sessions\` RENAME TO \`workout_sessions\`;--> statement-breakpoint
|
||||
ALTER TABLE \`workouts\` ADD \`created_at\` text NOT NULL;
|
||||
`;
|
||||
|
||||
export default {
|
||||
journal,
|
||||
migrations: {
|
||||
m0000
|
||||
m0000,
|
||||
m0001,
|
||||
m0002
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user