|
| 1 | +import type { Model } from '@zenstackhq/language/ast'; |
1 | 2 | import { ZModelCodeGenerator } from '@zenstackhq/sdk'; |
2 | 3 | import fs from 'node:fs'; |
3 | 4 | import path from 'node:path'; |
4 | 5 | import { execPackage } from '../utils/exec-utils'; |
5 | 6 | import { generateTempPrismaSchema, getSchemaFile, handleSubProcessError, loadSchemaDocumentWithServices } from './action-utils'; |
6 | 7 | import { syncEnums, syncRelation, syncTable, type Relation } from './pull'; |
7 | 8 | import { providers } from './pull/provider'; |
8 | | -import { getDatasource, getDbName } from './pull/utils'; |
| 9 | +import { getDatasource } from './pull/utils'; |
9 | 10 |
|
10 | 11 | type PushOptions = { |
11 | 12 | schema?: string; |
@@ -84,31 +85,35 @@ async function runPull(options: PullOptions) { |
84 | 85 |
|
85 | 86 | const { enums, tables } = await provider.introspect(datasource.url) |
86 | 87 |
|
87 | | - syncEnums({ dbEnums: enums, model, services }) |
| 88 | + const newModel: Model = { |
| 89 | + $type: 'Model', |
| 90 | + $container: undefined, |
| 91 | + $containerProperty: undefined, |
| 92 | + $containerIndex: undefined, |
| 93 | + declarations: [...model.declarations.filter(d => ["DataSource"].includes(d.$type))], |
| 94 | + imports: [], |
| 95 | + }; |
| 96 | + |
| 97 | + |
| 98 | + syncEnums({ dbEnums: enums, model: newModel, services }) |
88 | 99 |
|
89 | | - const resolveRelations: Relation[] = [] |
90 | | - for (const table of tables) { |
91 | | - const relations = syncTable({ table, model, provider, services }) |
92 | | - resolveRelations.push(...relations) |
93 | | - } |
94 | 100 |
|
95 | | - for (const relation of resolveRelations) { |
96 | | - syncRelation({ model, relation, services }); |
| 101 | + |
| 102 | + const resolvedRelations: Relation[] = [] |
| 103 | + for (const table of tables) { |
| 104 | + const relations = syncTable({ table, model: newModel, provider, services }) |
| 105 | + resolvedRelations.push(...relations) |
97 | 106 | } |
98 | 107 |
|
99 | | - for (const d of model.declarations) { |
100 | | - if (d.$type !== 'DataModel') continue |
101 | | - const found = tables.find((t) => getDbName(d) === t.name) |
102 | | - if (!found) { |
103 | | - delete (d.$container as any)[d.$containerProperty!][d.$containerIndex!] |
104 | | - } |
| 108 | + for (const relation of resolvedRelations) { |
| 109 | + syncRelation({ model: newModel, relation, services }); |
105 | 110 | } |
106 | 111 |
|
107 | | - model.declarations = model.declarations.filter((d) => d !== undefined) |
| 112 | + //TODO: diff models and apply changes only |
108 | 113 |
|
109 | 114 | const generator = await new ZModelCodeGenerator(); |
110 | 115 |
|
111 | | - const zmodelSchema = await generator.generate(model) |
| 116 | + const zmodelSchema = await generator.generate(newModel) |
112 | 117 |
|
113 | 118 | console.log(options.out ? `Writing to ${options.out}` : schemaFile); |
114 | 119 |
|
|
0 commit comments