MCPcopy
hub / github.com/drizzle-team/drizzle-orm / generatePgSnapshot

Function generatePgSnapshot

drizzle-kit/src/serializer/pgSerializer.ts:101–913  ·  view source on GitHub ↗
(
	tables: AnyPgTable[],
	enums: PgEnum<any>[],
	schemas: PgSchema[],
	sequences: PgSequence[],
	roles: PgRole[],
	policies: PgPolicy[],
	views: PgView[],
	matViews: PgMaterializedView[],
	casing: CasingType | undefined,
	schemaFilter?: string[],
)

Source from the content-addressed store, hash-verified

99}
100
101export const generatePgSnapshot = (
102 tables: AnyPgTable[],
103 enums: PgEnum<any>[],
104 schemas: PgSchema[],
105 sequences: PgSequence[],
106 roles: PgRole[],
107 policies: PgPolicy[],
108 views: PgView[],
109 matViews: PgMaterializedView[],
110 casing: CasingType | undefined,
111 schemaFilter?: string[],
112): PgSchemaInternal => {
113 const dialect = new PgDialect({ casing });
114 const result: Record<string, Table> = {};
115 const resultViews: Record<string, View> = {};
116 const sequencesToReturn: Record<string, Sequence> = {};
117 const rolesToReturn: Record<string, Role> = {};
118 // this policies are a separate objects that were linked to a table outside of it
119 const policiesToReturn: Record<string, Policy> = {};
120
121 // This object stores unique names for indexes and will be used to detect if you have the same names for indexes
122 // within the same PostgreSQL schema
123
124 const indexesInSchema: Record<string, string[]> = {};
125
126 for (const table of tables) {
127 // This object stores unique names for checks and will be used to detect if you have the same names for checks
128 // within the same PostgreSQL table
129 const checksInTable: Record<string, string[]> = {};
130
131 const {
132 name: tableName,
133 columns,
134 indexes,
135 foreignKeys,
136 checks,
137 schema,
138 primaryKeys,
139 uniqueConstraints,
140 policies,
141 enableRLS,
142 } = getTableConfig(table);
143
144 if (schemaFilter && !schemaFilter.includes(schema ?? 'public')) {
145 continue;
146 }
147
148 const columnsObject: Record<string, Column> = {};
149 const indexesObject: Record<string, Index> = {};
150 const checksObject: Record<string, CheckConstraint> = {};
151 const foreignKeysObject: Record<string, ForeignKey> = {};
152 const primaryKeysObject: Record<string, PrimaryKey> = {};
153 const uniqueConstraintObject: Record<string, UniqueConstraint> = {};
154 const policiesObject: Record<string, Policy> = {};
155
156 columns.forEach((column) => {
157 const name = getColumnCasing(column, casing);
158 const notNull: boolean = column.notNull;

Callers 6

generateDrizzleJsonFunction · 0.90
diffTestSchemasPushFunction · 0.90
applyPgDiffsFunction · 0.90
diffTestSchemasFunction · 0.90
introspectPgToFileFunction · 0.90
serializePgFunction · 0.85

Calls 15

sqlToQueryMethod · 0.95
getTableConfigFunction · 0.90
getColumnCasingFunction · 0.90
isFunction · 0.90
sqlToStrFunction · 0.90
escapeSingleQuotesFunction · 0.90
isPgArrayTypeFunction · 0.90
uniqueKeyNameFunction · 0.90
getTableNameFunction · 0.90
getViewConfigFunction · 0.90
getEnumSchemaFunction · 0.85

Tested by

no test coverage detected