Browse Source

query-node: initialize db with script & fix integration test

metmirr 4 years ago
parent
commit
c55e6808ad

+ 0 - 127
query-node/mappings/content-directory/initializeDefaultSchemas.ts

@@ -1,127 +0,0 @@
-import { DB, SubstrateEvent } from '../../generated/indexer'
-import { Channel } from '../../generated/graphql-server/src/modules/channel/channel.model'
-import { Video } from '../../generated/graphql-server/src/modules/video/video.model'
-import { Category } from '../../generated/graphql-server/src/modules/category/category.model'
-import { VideoMedia } from '../../generated/graphql-server/src/modules/video-media/video-media.model'
-import { LicenseEntity } from '../../generated/graphql-server/src/modules/license-entity/license-entity.model'
-import { VideoMediaEncoding } from '../../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
-import { MediaLocationEntity } from '../../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
-import { HttpMediaLocationEntity } from '../../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
-import { JoystreamMediaLocationEntity } from '../../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model'
-import { KnownLicenseEntity } from '../../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model'
-import { Block, Network } from '../../generated/graphql-server/src/modules/block/block.model'
-import BN from 'bn.js'
-import {
-  JoystreamMediaLocation,
-  KnownLicense,
-} from '../../generated/graphql-server/src/modules/variants/variants.model'
-import { nanoid } from 'nanoid'
-
-let done = false
-
-// eslint-disable-next-line @typescript-eslint/naming-convention
-export async function system_ExtrinsicSuccess(db: DB, event: SubstrateEvent): Promise<void> {
-  if (done) {
-    console.log(`Database initialization is done.`)
-    process.exit()
-  }
-
-  const id = '0'
-
-  // ///////// Block /////////////////
-  const happenedIn = new Block()
-  happenedIn.block = 0
-  happenedIn.timestamp = new BN(Date.now())
-  happenedIn.network = Network.BABYLON
-  await db.save<Block>(happenedIn)
-  // ///////// Block /////////////////
-
-  const commonProperties = { id, happenedIn }
-
-  // ///////// HttpMediaLocationEntity /////////////////
-  const httpMediaLocation = new HttpMediaLocationEntity({
-    ...commonProperties,
-    url: '5FyzfM2YtZa75hHYCAo5evNS8bH8P4Kw8EyXqKkC5upVSDBQ',
-  })
-  await db.save<HttpMediaLocationEntity>(httpMediaLocation)
-  // ///////// HttpMediaLocationEntity /////////////////
-
-  // ///////// JoystreamMediaLocationEntity /////////////////
-  const joyMediaLocation = new JoystreamMediaLocationEntity({
-    ...commonProperties,
-    dataObjectId: '5FyzfM2YtZa75hHYCAo5evNS8bH8P4Kw8EyXqKkC5upVSDBQ',
-  })
-  await db.save<JoystreamMediaLocationEntity>(joyMediaLocation)
-  // ///////// JoystreamMediaLocationEntity /////////////////
-
-  // ///////// KnownLicenseEntity /////////////////
-  const knownLicense = new KnownLicenseEntity({ ...commonProperties, code: 'NA' })
-  await db.save<KnownLicenseEntity>(knownLicense)
-  // ///////// KnownLicenseEntity /////////////////
-
-  // ///////// License /////////////////
-  const k = new KnownLicense()
-  k.code = knownLicense.code
-  const license = new LicenseEntity({ ...commonProperties, type: k })
-  await db.save<LicenseEntity>(license)
-  // ///////// License /////////////////
-
-  // ///////// MediaLocationEntity /////////////////
-  const mediaLocEntity = new MediaLocationEntity({ ...commonProperties, joystreamMediaLocation: joyMediaLocation })
-  await db.save<MediaLocationEntity>(mediaLocEntity)
-  // ///////// MediaLocationEntity /////////////////
-
-  // ///////// Channel /////////////////
-  const channel = new Channel({
-    ...commonProperties,
-    handle: `Channel(0) - ${nanoid()}`,
-    description: `Channel 0`,
-    isPublic: false,
-    isCurated: false,
-  })
-  await db.save<Channel>(channel)
-  // ///////// Channel /////////////////
-
-  // ///////// Category /////////////////
-  const category = new Category({ ...commonProperties, name: `Category(0) ${nanoid()}` })
-  await db.save<Category>(category)
-  // ///////// Category /////////////////
-
-  // ///////// VideoMediaEncoding /////////////////
-  const videoMediaEncod = new VideoMediaEncoding({ ...commonProperties, name: 'NA' })
-  await db.save<VideoMediaEncoding>(videoMediaEncod)
-  // ///////// VideoMediaEncoding /////////////////
-
-  // ///////// VideoMedia /////////////////
-  const location = new JoystreamMediaLocation()
-  location.dataObjectId = joyMediaLocation.dataObjectId
-  const videoMedia = new VideoMedia({
-    ...commonProperties,
-    location,
-    locationEntity: mediaLocEntity,
-    encoding: videoMediaEncod,
-    pixelHeight: 0,
-    pixelWidth: 0,
-  })
-  await db.save<VideoMedia>(videoMedia)
-  // ///////// VideoMedia /////////////////
-
-  // ///////// Video /////////////////
-  const v = new Video({ ...commonProperties })
-  v.category = category
-  v.channel = channel
-  v.media = videoMedia
-  v.license = license
-  v.title = `Video(0)`
-  v.description = `Video(0)`
-  v.duration = 0
-  v.thumbnailUrl = 'https://eu-central-1.linodeobjects.com/joystream/1.png'
-  v.isPublic = false
-  v.isCurated = false
-  v.isExplicit = true
-  v.isFeatured = false
-  await db.save<Video>(v)
-  // ///////// Video /////////////////
-
-  done = true
-}

+ 42 - 41
query-node/package.json

@@ -1,43 +1,44 @@
 {
-	"name": "query-node-root",
-	"version": "1.0.0",
-	"description": "GraphQL server and Substrate indexer. Generated with ♥ by Hydra-CLI",
-	"scripts": {
-		"build": "./build.sh",
-		"test": "echo \"Error: no test specified\" && exit 1",
-		"clean": "rm -rf ./generated",
-		"processor:start": "./processor-start.sh",
-		"indexer:start": "(cd ./generated/indexer && yarn && DEBUG=${DEBUG} yarn start:indexer --env ../../../.env)",
-		"server:start:dev": "(cd ./generated/graphql-server && yarn start:dev)",
-		"server:start:prod": "(cd ./generated/graphql-server && yarn start:prod)",
-		"configure": "(cd ./generated/graphql-server && yarn config:dev)",
-		"db:up": "(cd ../ && docker-compose up -d db)",
-		"db:drop": "(cd ./generated/graphql-server && yarn db:drop)",
-		"db:migrate": "./db-migrate.sh",
-		"db:schema:migrate": "(cd ./generated/graphql-server && yarn db:create && yarn db:sync && yarn db:migrate)",
-		"db:indexer:migrate": "(cd ./generated/indexer && yarn db:migrate)",
-		"codegen:indexer": "yarn hydra-cli codegen --no-install --no-graphql && cp indexer-tsconfig.json generated/indexer/tsconfig.json",
-		"codegen:server": "yarn hydra-cli codegen --no-install --no-indexer",
-		"cd-classes": "ts-node scripts/get-class-id-and-name.ts",
-		"integration-tests": "./run-tests.sh"
-	},
-	"author": "",
-	"license": "ISC",
-	"devDependencies": {
-		"@dzlzv/hydra-cli": "^0.0.24"
-	},
-	"dependencies": {
-		"@dzlzv/hydra-indexer-lib": "^0.0.22-legacy.1.26.1",
-		"@joystream/types": "^0.14.0",
-		"@types/bn.js": "^4.11.6",
-		"@types/debug": "^4.1.5",
-		"bn.js": "^5.1.2",
-		"debug": "^4.2.0",
-		"dotenvi": "^0.9.1",
-		"nanoid": "^3.1.20",
-		"tslib": "^2.0.0"
-	},
-	"volta": {
-		"extends": "../package.json"
-	}
+  "name": "query-node-root",
+  "version": "1.0.0",
+  "description": "GraphQL server and Substrate indexer. Generated with ♥ by Hydra-CLI",
+  "scripts": {
+    "build": "./build.sh",
+    "test": "echo \"Error: no test specified\" && exit 1",
+    "clean": "rm -rf ./generated",
+    "processor:start": "./processor-start.sh",
+    "indexer:start": "(cd ./generated/indexer && yarn && DEBUG=${DEBUG} yarn start:indexer --env ../../../.env)",
+    "server:start:dev": "(cd ./generated/graphql-server && yarn start:dev)",
+    "server:start:prod": "(cd ./generated/graphql-server && yarn start:prod)",
+    "configure": "(cd ./generated/graphql-server && yarn config:dev)",
+    "db:up": "(cd ../ && docker-compose up -d db)",
+    "db:drop": "(cd ./generated/graphql-server && yarn db:drop)",
+    "db:migrate": "./db-migrate.sh",
+    "db:schema:migrate": "(cd ./generated/graphql-server && yarn db:create && yarn db:sync && yarn db:migrate)",
+    "db:indexer:migrate": "(cd ./generated/indexer && yarn db:migrate)",
+    "codegen:indexer": "yarn hydra-cli codegen --no-install --no-graphql && cp indexer-tsconfig.json generated/indexer/tsconfig.json",
+    "codegen:server": "yarn hydra-cli codegen --no-install --no-indexer",
+    "cd-classes": "ts-node scripts/get-class-id-and-name.ts",
+    "integration-tests": "./run-tests.sh",
+    "db:init": "(cd ./generated/graphql-server && ts-node ../../scripts/initializeDefaultSchemas.ts)"
+  },
+  "author": "",
+  "license": "ISC",
+  "devDependencies": {
+    "@dzlzv/hydra-cli": "^0.0.24"
+  },
+  "dependencies": {
+    "@dzlzv/hydra-indexer-lib": "^0.0.22-legacy.1.26.1",
+    "@joystream/types": "^0.14.0",
+    "@types/bn.js": "^4.11.6",
+    "@types/debug": "^4.1.5",
+    "bn.js": "^5.1.2",
+    "debug": "^4.2.0",
+    "dotenvi": "^0.9.1",
+    "nanoid": "^3.1.20",
+    "tslib": "^2.0.0"
+  },
+  "volta": {
+    "extends": "../package.json"
+  }
 }

+ 3 - 0
query-node/run-tests.sh

@@ -35,6 +35,9 @@ docker-compose up -d db
 # Migrate the databases
 yarn workspace query-node-root db:migrate
 
+# Seed initial schemas
+yarn workspace query-node-root db:init
+
 docker-compose up -d graphql-server
 
 # Start the joystream-node before the indexer

+ 155 - 0
query-node/scripts/initializeDefaultSchemas.ts

@@ -0,0 +1,155 @@
+/* eslint-disable import/first */
+import 'reflect-metadata'
+
+import { loadConfig } from '../generated/graphql-server/src/config'
+loadConfig()
+
+import BN from 'bn.js'
+import { nanoid } from 'nanoid'
+import { SnakeNamingStrategy } from '@dzlzv/hydra-indexer-lib/lib'
+import { createConnection, ConnectionOptions, getConnection, EntityManager } from 'typeorm'
+
+import { Video } from '../generated/graphql-server/src/modules/video/video.model'
+import { Channel } from '../generated/graphql-server/src/modules/channel/channel.model'
+import { Block, Network } from '../generated/graphql-server/src/modules/block/block.model'
+import { Category } from '../generated/graphql-server/src/modules/category/category.model'
+import { VideoMedia } from '../generated/graphql-server/src/modules/video-media/video-media.model'
+import { LicenseEntity } from '../generated/graphql-server/src/modules/license-entity/license-entity.model'
+import { JoystreamMediaLocation, KnownLicense } from '../generated/graphql-server/src/modules/variants/variants.model'
+import { KnownLicenseEntity } from '../generated/graphql-server/src/modules/known-license-entity/known-license-entity.model'
+import { VideoMediaEncoding } from '../generated/graphql-server/src/modules/video-media-encoding/video-media-encoding.model'
+import { MediaLocationEntity } from '../generated/graphql-server/src/modules/media-location-entity/media-location-entity.model'
+import { HttpMediaLocationEntity } from '../generated/graphql-server/src/modules/http-media-location-entity/http-media-location-entity.model'
+import { JoystreamMediaLocationEntity } from '../generated/graphql-server/src/modules/joystream-media-location-entity/joystream-media-location-entity.model'
+
+function getConnectionOptions() {
+  const connectionConfig: ConnectionOptions = {
+    type: 'postgres',
+    host: process.env.WARTHOG_DB_HOST,
+    port: parseInt(process.env.WARTHOG_DB_PORT!),
+    username: process.env.WARTHOG_DB_USERNAME,
+    password: process.env.WARTHOG_DB_PASSWORD,
+    database: process.env.WARTHOG_DB_DATABASE,
+    entities: [process.env.WARTHOG_DB_ENTITIES!],
+    namingStrategy: new SnakeNamingStrategy(),
+    logging: true,
+  }
+  return connectionConfig
+}
+
+export async function main(): Promise<void> {
+  console.log(`Initializing...`)
+  await createConnection(getConnectionOptions())
+  getConnection().transaction(async (db: EntityManager) => {
+    const id = '0'
+    const createdAt = new Date()
+    const createdById = '0'
+    const version = 0
+
+    // ///////// Block /////////////////
+    const happenedIn = new Block({
+      createdAt,
+      createdById,
+      version,
+      block: 0,
+      timestamp: new BN(Date.now()),
+      network: Network.BABYLON,
+    })
+    await db.save<Block>(happenedIn)
+    // ///////// Block /////////////////
+
+    const commonProperties = { id, happenedIn, createdAt, createdById, version }
+
+    // ///////// HttpMediaLocationEntity /////////////////
+    const httpMediaLocation = new HttpMediaLocationEntity({
+      ...commonProperties,
+      url: '5FyzfM2YtZa75hHYCAo5evNS8bH8P4Kw8EyXqKkC5upVSDBQ',
+    })
+    await db.save<HttpMediaLocationEntity>(httpMediaLocation)
+    // ///////// HttpMediaLocationEntity /////////////////
+
+    // ///////// JoystreamMediaLocationEntity /////////////////
+    const joyMediaLocation = new JoystreamMediaLocationEntity({
+      ...commonProperties,
+      dataObjectId: '5FyzfM2YtZa75hHYCAo5evNS8bH8P4Kw8EyXqKkC5upVSDBQ',
+    })
+    await db.save<JoystreamMediaLocationEntity>(joyMediaLocation)
+    // ///////// JoystreamMediaLocationEntity /////////////////
+
+    // ///////// KnownLicenseEntity /////////////////
+    const knownLicense = new KnownLicenseEntity({ ...commonProperties, code: 'NA' })
+    await db.save<KnownLicenseEntity>(knownLicense)
+    // ///////// KnownLicenseEntity /////////////////
+
+    // ///////// License /////////////////
+    const k = new KnownLicense()
+    k.code = knownLicense.code
+    const license = new LicenseEntity({ ...commonProperties, type: k })
+    await db.save<LicenseEntity>(license)
+    // ///////// License /////////////////
+
+    // ///////// MediaLocationEntity /////////////////
+    const mediaLocEntity = new MediaLocationEntity({ ...commonProperties, joystreamMediaLocation: joyMediaLocation })
+    await db.save<MediaLocationEntity>(mediaLocEntity)
+    // ///////// MediaLocationEntity /////////////////
+
+    // ///////// Channel /////////////////
+    const channel = new Channel({
+      ...commonProperties,
+      handle: `Channel(0) - ${nanoid()}`,
+      description: `Channel 0`,
+      isPublic: false,
+      isCurated: false,
+    })
+    await db.save<Channel>(channel)
+    // ///////// Channel /////////////////
+
+    // ///////// Category /////////////////
+    const category = new Category({ ...commonProperties, name: `Category(0) ${nanoid()}` })
+    await db.save<Category>(category)
+    // ///////// Category /////////////////
+
+    // ///////// VideoMediaEncoding /////////////////
+    const videoMediaEncod = new VideoMediaEncoding({ ...commonProperties, name: 'NA' })
+    await db.save<VideoMediaEncoding>(videoMediaEncod)
+    // ///////// VideoMediaEncoding /////////////////
+
+    // ///////// VideoMedia /////////////////
+    const location = new JoystreamMediaLocation()
+    location.dataObjectId = joyMediaLocation.dataObjectId
+    const videoMedia = new VideoMedia({
+      ...commonProperties,
+      location,
+      locationEntity: mediaLocEntity,
+      encoding: videoMediaEncod,
+      pixelHeight: 0,
+      pixelWidth: 0,
+    })
+    await db.save<VideoMedia>(videoMedia)
+    // ///////// VideoMedia /////////////////
+
+    // ///////// Video /////////////////
+    const v = new Video({ ...commonProperties })
+    v.category = category
+    v.channel = channel
+    v.media = videoMedia
+    v.license = license
+    v.title = `Video(0)`
+    v.description = `Video(0)`
+    v.duration = 0
+    v.thumbnailUrl = 'https://eu-central-1.linodeobjects.com/joystream/1.png'
+    v.isPublic = false
+    v.isCurated = false
+    v.isExplicit = true
+    v.isFeatured = false
+    await db.save<Video>(v)
+    // ///////// Video /////////////////
+  })
+}
+
+main()
+  .then(() => {
+    console.log(`Done.`)
+    process.exit()
+  })
+  .catch(console.log)