MCPcopy Create free account
hub / github.com/outerbase/studio / generatePostgresSchemaChange

Function generatePostgresSchemaChange

src/drivers/postgres/generate-schema.ts:163–250  ·  view source on GitHub ↗
(
  driver: BaseDriver,
  change: DatabaseTableSchemaChange
)

Source from the content-addressed store, hash-verified

161
162//https://www.postgresql.org/docs/current/sql-createtable.html
163export function generatePostgresSchemaChange(
164 driver: BaseDriver,
165 change: DatabaseTableSchemaChange
166): string[] {
167 const isCreateScript = !change.name.old;
168
169 const lines = [];
170
171 for (const col of change.columns) {
172 if (col.new === null) lines.push(`DROP COLUMN ${col.old?.name}`);
173 else if (col.old === null) {
174 if (isCreateScript) {
175 lines.push(generateCreateColumn(driver, col.new));
176 } else {
177 lines.push("ADD " + generateCreateColumn(driver, col.new));
178 }
179 } else {
180 // check if there is rename
181 if (col.new.name !== col.old.name) {
182 lines.push(
183 `RENAME COLUMN ${driver.escapeId(col.old.name)} TO ${driver.escapeId(
184 col.new.name
185 )}`
186 );
187 }
188
189 // check if there is any changed except name
190 if (!isEqual(omit(col.old, ["name"]), omit(col.new, ["name"]))) {
191 lines.push(
192 `ALTER COLUMN ${generateCreateColumn(
193 driver,
194 {
195 name: col.new.name,
196 type: col.new.type,
197 pk: col.new.pk,
198 constraint: {},
199 },
200 true
201 )}`
202 );
203 if (col.new.constraint) {
204 for (const s of generateConstraintModifyScript(
205 driver,
206 {
207 ...col.new.constraint,
208 primaryColumns: [col.new.name],
209 uniqueColumns: [col.new.name],
210 foreignKey: {
211 ...col.new.constraint.foreignKey,
212 columns: [col.new.name],
213 },
214 },
215 change.name.old ?? ""
216 )) {
217 lines.push(s);
218 }
219 }
220 }

Callers 1

Calls 4

generateCreateColumnFunction · 0.70
generateConstraintScriptFunction · 0.70
escapeIdMethod · 0.45

Tested by

no test coverage detected