Skip to content

Commit

Permalink
chore(voucher): using knex migrate for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
siddhart1o1 committed May 17, 2024
1 parent 4310a7d commit 78a9229
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 16 deletions.
13 changes: 9 additions & 4 deletions apps/voucher/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./instrumentation.node")
try {
const db = await import("./services/db/schema")
await db.createWithdrawLinksTable()
console.log("Table created")
const db = await import("./services/db/knex")
// TODO use ts migration files.
await db.knex.migrate.latest({
directory: "./services/db/migrations",
loadExtensions: [".mjs"],
extension: "mjs",
})
} catch (err: unknown) {
console.log("Error creating table", err)
console.log("Error making migrations", err)
throw err
}
}
}
2 changes: 1 addition & 1 deletion apps/voucher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"codegen": "graphql-codegen --config codegen.yml",
"test:unit": "jest --config=config/jest.config.js --forceExit",
"db:status": "knex --knexfile ./services/db/connection.ts migrate:status",
"db:create": "knex --knexfile ./services/db/connection.ts migrate:make -x ts",
"db:create": "knex --knexfile ./services/db/connection.ts migrate:make -x mjs",
"db:migrate": "knex --knexfile ./services/db/connection.ts migrate:latest",
"db:rollback": "knex --knexfile ./services/db/connection.ts migrate:rollback",
"db:create:seed": "knex --knexfile ./services/db/connection.ts seed:make -x ts",
Expand Down
1 change: 0 additions & 1 deletion apps/voucher/services/db/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const knexConfig: Knex.Config = {
connection: env.PG_CON,
migrations: {
directory: "./migrations",
extension: "ts",
},
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const up = async (knex) => {
await knex.schema.createTable("WithdrawLinks", (table) => {
table.uuid("id").primary()
table.text("userId").notNullable()

table.text("identifierCode").unique().notNullable()
table.text("voucherSecret").unique().notNullable()
table.text("k1").unique().notNullable()
table.text("uniqueHash").unique().notNullable()

table.timestamp("createdAt").defaultTo(knex.fn.now())
table.timestamp("paidAt")

table.enu("status", ["ACTIVE", "PENDING", "PAID"]).notNullable().defaultTo("PENDING")

table.decimal("voucherAmountInCents").notNullable()
table.decimal("salesAmountInCents").notNullable()
table
.float("commissionPercentage")
.notNullable()
.unsigned()
.defaultTo(0)
.comment("min = 0 and max = 100")
})
}

export const down = async (knex) => {}

This file was deleted.

0 comments on commit 78a9229

Please sign in to comment.