ponder 0.8.1 → 0.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ponder",
3
- "version": "0.8.1",
3
+ "version": "0.8.2",
4
4
  "description": "An open-source framework for crypto application backends",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -672,20 +672,7 @@ export const createDatabase = ({
672
672
  ),
673
673
  } satisfies PonderApp;
674
674
 
675
- // If schema is empty, create tables
676
- if (previousApp === undefined) {
677
- await tx
678
- .insertInto("_ponder_meta")
679
- .values({ key: "status", value: null })
680
- .execute();
681
- await tx
682
- .insertInto("_ponder_meta")
683
- .values({
684
- key: "app",
685
- value: newApp,
686
- })
687
- .execute();
688
-
675
+ const createEnums = async () => {
689
676
  for (
690
677
  let i = 0;
691
678
  i < schemaBuild.statements.enums.sql.length;
@@ -704,6 +691,9 @@ export const createDatabase = ({
704
691
  throw e;
705
692
  });
706
693
  }
694
+ };
695
+
696
+ const createTables = async () => {
707
697
  for (
708
698
  let i = 0;
709
699
  i < schemaBuild.statements.tables.sql.length;
@@ -722,6 +712,47 @@ export const createDatabase = ({
722
712
  throw e;
723
713
  });
724
714
  }
715
+ };
716
+
717
+ const dropTables = async () => {
718
+ for (const tableName of getTableNames(schemaBuild.schema)) {
719
+ await tx.schema
720
+ .dropTable(tableName.sql)
721
+ .cascade()
722
+ .ifExists()
723
+ .execute();
724
+ await tx.schema
725
+ .dropTable(tableName.reorg)
726
+ .cascade()
727
+ .ifExists()
728
+ .execute();
729
+ }
730
+ };
731
+
732
+ const dropEnums = async () => {
733
+ for (const enumName of schemaBuild.statements.enums.json) {
734
+ await tx.schema.dropType(enumName.name).ifExists().execute();
735
+ }
736
+ };
737
+
738
+ // If schema is empty, create tables
739
+ // If schema is empty, create tables
740
+ if (previousApp === undefined) {
741
+ await tx
742
+ .insertInto("_ponder_meta")
743
+ .values({ key: "status", value: null })
744
+ .execute();
745
+ await tx
746
+ .insertInto("_ponder_meta")
747
+ .values({
748
+ key: "app",
749
+ value: newApp,
750
+ })
751
+ .execute();
752
+
753
+ await createEnums();
754
+ await createTables();
755
+
725
756
  common.logger.info({
726
757
  service: "database",
727
758
  msg: `Created tables [${newApp.table_names.join(", ")}]`,
@@ -748,61 +779,16 @@ export const createDatabase = ({
748
779
  .execute();
749
780
  await tx
750
781
  .updateTable("_ponder_meta")
751
- .set({
752
- value: newApp,
753
- })
782
+ .set({ value: newApp })
754
783
  .where("key", "=", "app")
755
784
  .execute();
756
785
 
757
- for (const tableName of getTableNames(schemaBuild.schema)) {
758
- await tx.schema
759
- .dropTable(tableName.sql)
760
- .cascade()
761
- .ifExists()
762
- .execute();
763
- await tx.schema
764
- .dropTable(tableName.reorg)
765
- .cascade()
766
- .ifExists()
767
- .execute();
768
- }
786
+ await dropTables();
787
+ await dropEnums();
788
+
789
+ await createEnums();
790
+ await createTables();
769
791
 
770
- for (
771
- let i = 0;
772
- i < schemaBuild.statements.enums.sql.length;
773
- i++
774
- ) {
775
- await sql
776
- .raw(schemaBuild.statements.enums.sql[i]!)
777
- .execute(tx)
778
- .catch((_error) => {
779
- const error = _error as Error;
780
- if (!error.message.includes("already exists")) throw error;
781
- const e = new NonRetryableError(
782
- `Unable to create enum '${preBuild.namespace}'.'${schemaBuild.statements.enums.json[i]!.name}' because an enum with that name already exists.`,
783
- );
784
- e.stack = undefined;
785
- throw e;
786
- });
787
- }
788
- for (
789
- let i = 0;
790
- i < schemaBuild.statements.tables.sql.length;
791
- i++
792
- ) {
793
- await sql
794
- .raw(schemaBuild.statements.tables.sql[i]!)
795
- .execute(tx)
796
- .catch((_error) => {
797
- const error = _error as Error;
798
- if (!error.message.includes("already exists")) throw error;
799
- const e = new NonRetryableError(
800
- `Unable to create table '${preBuild.namespace}'.'${schemaBuild.statements.tables.json[i]!.tableName}' because a table with that name already exists.`,
801
- );
802
- e.stack = undefined;
803
- throw e;
804
- });
805
- }
806
792
  common.logger.info({
807
793
  service: "database",
808
794
  msg: `Created tables [${newApp.table_names.join(", ")}]`,
@@ -817,8 +803,7 @@ export const createDatabase = ({
817
803
  // If crash recovery is not possible, error
818
804
  if (
819
805
  common.options.command === "dev" ||
820
- previousApp.build_id !== newApp.build_id ||
821
- previousApp.checkpoint === encodeCheckpoint(zeroCheckpoint)
806
+ previousApp.build_id !== newApp.build_id
822
807
  ) {
823
808
  const error = new NonRetryableError(
824
809
  `Schema '${preBuild.namespace}' was previously used by a different Ponder app. Drop the schema first, or use a different schema. Read more: https://ponder.sh/docs/getting-started/database#database-schema`,
@@ -845,6 +830,35 @@ export const createDatabase = ({
845
830
 
846
831
  // Crash recovery is possible, recover
847
832
 
833
+ if (previousApp.checkpoint === encodeCheckpoint(zeroCheckpoint)) {
834
+ await tx
835
+ .updateTable("_ponder_meta")
836
+ .set({ value: null })
837
+ .where("key", "=", "status")
838
+ .execute();
839
+ await tx
840
+ .updateTable("_ponder_meta")
841
+ .set({ value: newApp })
842
+ .where("key", "=", "app")
843
+ .execute();
844
+
845
+ await dropTables();
846
+ await dropEnums();
847
+
848
+ await createEnums();
849
+ await createTables();
850
+
851
+ common.logger.info({
852
+ service: "database",
853
+ msg: `Created tables [${newApp.table_names.join(", ")}]`,
854
+ });
855
+
856
+ return {
857
+ status: "success",
858
+ checkpoint: encodeCheckpoint(zeroCheckpoint),
859
+ } as const;
860
+ }
861
+
848
862
  const checkpoint = previousApp.checkpoint;
849
863
  newApp.checkpoint = checkpoint;
850
864
 
@@ -855,9 +869,7 @@ export const createDatabase = ({
855
869
  .execute();
856
870
  await tx
857
871
  .updateTable("_ponder_meta")
858
- .set({
859
- value: newApp,
860
- })
872
+ .set({ value: newApp })
861
873
  .where("key", "=", "app")
862
874
  .execute();
863
875