Skip to content
This repository has been archived by the owner on May 4, 2021. It is now read-only.

Commit

Permalink
Entities Seeding (#106)
Browse files Browse the repository at this point in the history
* Update entities for voting and statistics

* Initialize migrations, setup seeding (Canon & Cohort)

* Add default parent account

* Seed 4 default children

* Add vote counter to child

* Add comments

* Remove unused imports

* Setup story seeding

* Illustration seeding, fix null contraints

* Add readability seeds to avoid py crash

* Warning comment, possibly #96?
  • Loading branch information
lgv-0 authored Apr 20, 2020
1 parent d31c6ae commit 40a3b85
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 31 deletions.
32 changes: 16 additions & 16 deletions src/database/entity/Child.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,28 @@ class Child {
@Column((type) => Progress)
progress: Progress;

@ManyToOne(
(type) => Parent,
(parent) => parent.children
)
@Column({ nullable: true })
total_points: number;

@Column({ nullable: true })
wins: number;

@Column({ nullable: true })
losses: number;

@Column({ nullable: true })
votes: number;

@ManyToOne((type) => Parent, (parent) => parent.children)
parent: Parent;

@ManyToOne(
(type) => Cohort,
(cohort) => cohort.children
)
@ManyToOne((type) => Cohort, (cohort) => cohort.children)
cohort: Cohort;

@OneToMany(
(type) => Illustrations,
(illustrations) => illustrations.child
)
@OneToMany((type) => Illustrations, (illustrations) => illustrations.child)
illustrations: Illustrations[];

@OneToMany(
(type) => Stories,
(stories) => stories.child
)
@OneToMany((type) => Stories, (stories) => stories.child)
stories: Stories[];
}

Expand Down
5 changes: 1 addition & 4 deletions src/database/entity/Cohort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ class Cohort {
@Column((type) => DueDates)
dueDates: DueDates;

@OneToMany(
(type) => Child,
(child) => child.cohort
)
@OneToMany((type) => Child, (child) => child.cohort)
children: Child[];
}

Expand Down
9 changes: 4 additions & 5 deletions src/database/entity/Illustrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ class Illustrations {

@Column()
childId: number;
@ManyToOne(
(type) => Child,
(child) => child.illustrations,
{ onDelete: 'CASCADE' }
)
@ManyToOne((type) => Child, (child) => child.illustrations, { onDelete: 'CASCADE' })
@JoinColumn()
child: Child;

Expand All @@ -25,6 +21,9 @@ class Illustrations {

@Column({ nullable: true })
points: number;

@Column({ nullable: true })
votes: number;
}

export { Illustrations };
2 changes: 1 addition & 1 deletion src/database/entity/Matches.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, PrimaryGeneratedColumn, OneToMany, ManyToOne, Entity } from 'typeorm';
import { Column, PrimaryGeneratedColumn, Entity } from 'typeorm';

@Entity()
class Matches {
Expand Down
9 changes: 4 additions & 5 deletions src/database/entity/Stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ class Stories {

@Column()
childId: number;
@ManyToOne(
(type) => Child,
(child) => child.stories,
{ onDelete: 'CASCADE' }
)
@ManyToOne((type) => Child, (child) => child.stories, { onDelete: 'CASCADE' })
@JoinColumn()
child: Child;

Expand All @@ -30,6 +26,9 @@ class Stories {
@Column({ nullable: true })
points: number;

@Column({ nullable: true })
votes: number;

@Column({ type: 'double precision', nullable: true })
flesch_reading_ease: number;

Expand Down
116 changes: 116 additions & 0 deletions src/database/migration/1587051617754-MainMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
import { Cohort, Canon, Parent, Child, Stories, Illustrations } from '../../database/entity';
import {
CohortSeed,
CanonSeed,
ParentSeed,
ChildSeed,
StorySeed,
IllustrationSeed,
} from '../seeds/seeds';
import Stripe from 'stripe';

export class MainMigration1587051617754 implements MigrationInterface {
name = 'MainMigration1587051617754';

public async up(queryRunner: QueryRunner): Promise<any> {
/* Basic query structure copied from generating a new migration (migration:generate) */
await queryRunner.query(
`CREATE TABLE "admin" ("id" SERIAL NOT NULL, "email" character varying NOT NULL, "password" character varying NOT NULL, "temptoken" character varying, "role" character varying NOT NULL, CONSTRAINT "UQ_de87485f6489f5d0995f5841952" UNIQUE ("email"), CONSTRAINT "PK_e032310bcef831fb83101899b10" PRIMARY KEY ("id"))`,
undefined
);
await queryRunner.query(
`CREATE TABLE "canon" ("week" integer NOT NULL, "base64" character varying NOT NULL, "altbase64" character varying, CONSTRAINT "PK_8c1bfa026b3b545879ae7437a05" PRIMARY KEY ("week"))`,
undefined
);
await queryRunner.query(
`CREATE TABLE "parent" ("id" SERIAL NOT NULL, "name" character varying, "email" character varying NOT NULL, "password" character varying NOT NULL, "stripeID" character varying, CONSTRAINT "UQ_9158391af7b8ca4911efaad8a73" UNIQUE ("email"), CONSTRAINT "PK_bf93c41ee1ae1649869ebd05617" PRIMARY KEY ("id"))`,
undefined
);
await queryRunner.query(
`CREATE TABLE "cohort" ("id" SERIAL NOT NULL, "name" character varying NOT NULL, "week" integer NOT NULL, "activity" character varying NOT NULL, "dueDatesReading" TIMESTAMP NOT NULL, "dueDatesWriting" TIMESTAMP NOT NULL, "dueDatesDrawing" TIMESTAMP NOT NULL, "dueDatesTeamreview" TIMESTAMP, "dueDatesRandomreview" TIMESTAMP, "dueDatesResults" TIMESTAMP, CONSTRAINT "PK_4fb3cca38dc4b461110344e5f9b" PRIMARY KEY ("id"))`,
undefined
);
await queryRunner.query(
`CREATE TABLE "illustrations" ("id" SERIAL NOT NULL, "childId" integer NOT NULL, "week" integer NOT NULL, "illustration" character varying, "points" integer, "votes" integer, CONSTRAINT "PK_ec4a601172b41459d76aeb1f02f" PRIMARY KEY ("id"))`,
undefined
);
await queryRunner.query(
`CREATE TABLE "stories" ("id" SERIAL NOT NULL, "childId" integer NOT NULL, "week" integer NOT NULL, "storyText" character varying, "points" integer, "votes" integer, "flesch_reading_ease" double precision, "smog_index" double precision, "flesch_kincaid_grade" double precision, "coleman_liau_index" double precision, "automated_readability_index" double precision, "dale_chall_readability_score" double precision, "difficult_words" double precision, "linsear_write_formula" double precision, "gunning_fog" double precision, "consolidated_score" character varying, "doc_length" integer, "quote_count" integer, "storyPage1" character varying, "storyPage2" character varying, "storyPage3" character varying, "storyPage4" character varying, "storyPage5" character varying, "transcribedTextT_page1" character varying, "transcribedTextT_page2" character varying, "transcribedTextT_page3" character varying, "transcribedTextT_page4" character varying, "transcribedTextT_page5" character varying, CONSTRAINT "PK_bb6f880b260ed96c452b32a39f0" PRIMARY KEY ("id"))`,
undefined
);
await queryRunner.query(
`CREATE TABLE "child" ("id" SERIAL NOT NULL, "username" character varying NOT NULL, "grade" integer NOT NULL, "subscription" boolean NOT NULL DEFAULT false, "avatar" character varying, "total_points" integer, "wins" integer, "losses" integer, "votes" integer, "parentId" integer, "cohortId" integer, "preferencesDyslexia" boolean NOT NULL DEFAULT false, "progressReading" boolean NOT NULL DEFAULT false, "progressWriting" boolean NOT NULL DEFAULT false, "progressDrawing" boolean NOT NULL DEFAULT false, "progressTeamreview" boolean NOT NULL DEFAULT false, "progressRandomreview" boolean NOT NULL DEFAULT false, CONSTRAINT "PK_4609b9b323ca37c6bc435ec4b6b" PRIMARY KEY ("id"))`,
undefined
);
await queryRunner.query(
`CREATE TABLE "matches" ("id" SERIAL NOT NULL, "team1_child1_id" integer, "team1_child2_id" integer, "team2_child1_id" integer, "team2_child2_id" integer, "week" integer, CONSTRAINT "PK_8a22c7b2e0828988d51256117f4" PRIMARY KEY ("id"))`,
undefined
);
await queryRunner.query(
`ALTER TABLE "illustrations" ADD CONSTRAINT "FK_2675728b1c00556cd980c77b849" FOREIGN KEY ("childId") REFERENCES "child"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
undefined
);
await queryRunner.query(
`ALTER TABLE "stories" ADD CONSTRAINT "FK_71d050c35abe5d607bb746503af" FOREIGN KEY ("childId") REFERENCES "child"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
undefined
);
await queryRunner.query(
`ALTER TABLE "child" ADD CONSTRAINT "FK_8a2f35051e01ce9c6656af13c7c" FOREIGN KEY ("parentId") REFERENCES "parent"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
undefined
);
await queryRunner.query(
`ALTER TABLE "child" ADD CONSTRAINT "FK_faecc77a0cf169a4c97f0a8d812" FOREIGN KEY ("cohortId") REFERENCES "cohort"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
undefined
);
/******************/

/* SEEDING - Need to use queryRunner's Manager to get the proper connection info */
await queryRunner.manager.getRepository(Cohort).save(CohortSeed);
await queryRunner.manager.getRepository(Canon).save(CanonSeed);

//Should match auth exactly
let { id: stripeID } = await new Stripe('sk_test_v666XmnGJcP1Oz3GBg2iFmvd004Q3qp4jZ', {
apiVersion: '2019-12-03',
typescript: true,
}).customers.create({
email: ParentSeed.email,
});
ParentSeed.stripeID = stripeID;
await queryRunner.manager.getRepository(Parent).save(ParentSeed);

await queryRunner.manager.getRepository(Child).save(ChildSeed);

await queryRunner.manager.getRepository(Stories).save(StorySeed);

await queryRunner.manager.getRepository(Illustrations).save(IllustrationSeed);
/******************/
}

public async down(queryRunner: QueryRunner): Promise<any> {
await queryRunner.query(
`ALTER TABLE "child" DROP CONSTRAINT "FK_faecc77a0cf169a4c97f0a8d812"`,
undefined
);
await queryRunner.query(
`ALTER TABLE "child" DROP CONSTRAINT "FK_8a2f35051e01ce9c6656af13c7c"`,
undefined
);
await queryRunner.query(
`ALTER TABLE "stories" DROP CONSTRAINT "FK_71d050c35abe5d607bb746503af"`,
undefined
);
await queryRunner.query(
`ALTER TABLE "illustrations" DROP CONSTRAINT "FK_2675728b1c00556cd980c77b849"`,
undefined
);
await queryRunner.query(`DROP TABLE "matches"`, undefined);
await queryRunner.query(`DROP TABLE "child"`, undefined);
await queryRunner.query(`DROP TABLE "stories"`, undefined);
await queryRunner.query(`DROP TABLE "illustrations"`, undefined);
await queryRunner.query(`DROP TABLE "cohort"`, undefined);
await queryRunner.query(`DROP TABLE "parent"`, undefined);
await queryRunner.query(`DROP TABLE "canon"`, undefined);
await queryRunner.query(`DROP TABLE "admin"`, undefined);
}
}
Loading

0 comments on commit 40a3b85

Please sign in to comment.