ソースを参照

Minor fixes ported from `user-accounts` branch (#136)

* Fix empty-table state import issue and make generate-schema-file.sh executable w/o bash

* Fix: Incorrect batch number in the logs
Leszek Wiesner 1 年間 前
コミット
f74af81ad5
2 ファイル変更14 行追加9 行削除
  1. 3 3
      scripts/generate-schema-file.sh
  2. 11 6
      src/utils/offchainState.ts

+ 3 - 3
scripts/generate-schema-file.sh

@@ -1,9 +1,9 @@
-#!/bin/bash
+#!/bin/sh
 
 
-SCRIPT_PATH="$(dirname "${BASH_SOURCE[0]}")"
+SCRIPT_PATH="$(dirname "$0")"
 cd $SCRIPT_PATH/..
 cd $SCRIPT_PATH/..
 
 
-if [[ -d schema ]]
+if [ -d schema ]
 then
 then
     echo "Generating schema file from schema directory..."
     echo "Generating schema file from schema directory..."
     find schema -type f -exec cat {} \; > schema.graphql
     find schema -type f -exec cat {} \; > schema.graphql

+ 11 - 6
src/utils/offchainState.ts

@@ -92,6 +92,9 @@ export class OffchainState {
     const { data }: ExportedState = JSON.parse(fs.readFileSync(exportFilePath, 'utf-8'))
     const { data }: ExportedState = JSON.parse(fs.readFileSync(exportFilePath, 'utf-8'))
     this.logger.info('Importing offchain state')
     this.logger.info('Importing offchain state')
     for (const [entityName, { type, values }] of Object.entries(data)) {
     for (const [entityName, { type, values }] of Object.entries(data)) {
+      if (!values.length) {
+        continue
+      }
       this.logger.info(
       this.logger.info(
         `${type === 'update' ? 'Updating' : 'Inserting'} ${values.length} ${entityName} entities...`
         `${type === 'update' ? 'Updating' : 'Inserting'} ${values.length} ${entityName} entities...`
       )
       )
@@ -99,7 +102,7 @@ export class OffchainState {
         // We're using "batched" updates, because otherwise the process becomes extremely slow
         // We're using "batched" updates, because otherwise the process becomes extremely slow
         const meta = em.connection.getMetadata(entityName)
         const meta = em.connection.getMetadata(entityName)
         const batchSize = 1000
         const batchSize = 1000
-        let batchNumber = 1
+        let batchNumber = 0
         const fieldNames = Object.keys(values[0])
         const fieldNames = Object.keys(values[0])
         const fieldTypes = Object.fromEntries(
         const fieldTypes = Object.fromEntries(
           fieldNames.map((fieldName) => {
           fieldNames.map((fieldName) => {
@@ -110,10 +113,11 @@ export class OffchainState {
           })
           })
         )
         )
         while (values.length) {
         while (values.length) {
-          ++batchNumber
           const batch = values.splice(0, batchSize)
           const batch = values.splice(0, batchSize)
           this.logger.info(
           this.logger.info(
-            `Executing batch #${batchNumber} of ${batch.length} entities (${values.length} entities left)...`
+            `Executing batch #${++batchNumber} of ${batch.length} entities (${
+              values.length
+            } entities left)...`
           )
           )
           let paramCounter = 1
           let paramCounter = 1
           await em.query(
           await em.query(
@@ -139,12 +143,13 @@ export class OffchainState {
         // For inserts we also use batches, but this is because otherwise the query may fail
         // For inserts we also use batches, but this is because otherwise the query may fail
         // if the number of entities is very large
         // if the number of entities is very large
         const batchSize = 1000
         const batchSize = 1000
-        let batchNumber = 1
+        let batchNumber = 0
         while (values.length) {
         while (values.length) {
-          ++batchNumber
           const batch = values.splice(0, batchSize)
           const batch = values.splice(0, batchSize)
           this.logger.info(
           this.logger.info(
-            `Executing batch #${batchNumber} of ${batch.length} entities (${values.length} entities left)...`
+            `Executing batch #${++batchNumber} of ${batch.length} entities (${
+              values.length
+            } entities left)...`
           )
           )
           await em.getRepository(entityName).insert(batch)
           await em.getRepository(entityName).insert(batch)
         }
         }