chore: apply schema changes before inserting into migrations table
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { eq, sql } from 'drizzle-orm';
|
||||
import { pushSchema } from 'drizzle-kit/utils';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import {
|
||||
jsonb,
|
||||
numeric,
|
||||
pgEnum,
|
||||
pgTable,
|
||||
varchar,
|
||||
jsonb,
|
||||
} from 'drizzle-orm/pg-core';
|
||||
import { pushSchema } from 'drizzle-kit/utils';
|
||||
import { SanitizedCollectionConfig } from 'payload/dist/collections/config/types';
|
||||
import { configToJSONSchema } from 'payload/dist/utilities/configToJSONSchema';
|
||||
import type { Init } from 'payload/dist/database/types';
|
||||
import { configToJSONSchema } from 'payload/dist/utilities/configToJSONSchema';
|
||||
import prompts from 'prompts';
|
||||
import { buildTable } from './schema/build';
|
||||
import type { GenericEnum, GenericRelation, GenericTable, PostgresAdapter } from './types';
|
||||
@@ -45,10 +45,8 @@ export const init: Init = async function init(
|
||||
// create global model
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== 'production') {
|
||||
// TODO:
|
||||
// Run migrate here????
|
||||
// We need to make sure the files in the `migrationsDir` are all executed
|
||||
// Only push schema if not in production
|
||||
if (process.env.NODE_ENV === 'production') return;
|
||||
|
||||
const schema: Record<string, GenericEnum | GenericTable | GenericRelation> = {};
|
||||
|
||||
@@ -72,7 +70,7 @@ export const init: Init = async function init(
|
||||
apply,
|
||||
} = await pushSchema(schema, this.db);
|
||||
|
||||
this.payload.logger.info({
|
||||
this.payload.logger.debug({
|
||||
msg: 'Schema push results',
|
||||
hasDataLoss,
|
||||
warnings,
|
||||
@@ -114,15 +112,7 @@ export const init: Init = async function init(
|
||||
|
||||
const jsonSchema = configToJSONSchema(this.payload.config);
|
||||
|
||||
// This should mirror the generated table definition from schema/build.ts
|
||||
await this.db.execute(sql`CREATE TABLE IF NOT EXISTS "payload_migrations" (
|
||||
id SERIAL PRIMARY KEY,
|
||||
name character varying,
|
||||
batch numeric,
|
||||
schema jsonb,
|
||||
created_at timestamp without time zone DEFAULT now() NOT NULL,
|
||||
updated_at timestamp without time zone DEFAULT now() NOT NULL
|
||||
);`);
|
||||
await apply();
|
||||
|
||||
const devPush = await this.db.select().from(migrationsSchema).where(eq(migrationsSchema.batch, '-1'));
|
||||
|
||||
@@ -137,7 +127,4 @@ export const init: Init = async function init(
|
||||
schema: JSON.stringify(jsonSchema),
|
||||
}).where(eq(migrationsSchema.batch, '-1'));
|
||||
}
|
||||
|
||||
await apply();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user