(schemaSql: string)
| 152 | } |
| 153 | |
| 154 | function emitSchemaFile(schemaSql: string): string { |
| 155 | const parsed = parseSchema(schemaSql); |
| 156 | |
| 157 | const versionStmt = parsed.statements.find(s => |
| 158 | /INSERT\s+IGNORE\s+INTO\s+_meta.+schema_version/i.test(s) |
| 159 | ); |
| 160 | const versionMatch = versionStmt |
| 161 | ? /VALUES\s*\(\s*'schema_version'\s*,\s*'([^']+)'/i.exec(versionStmt) |
| 162 | : null; |
| 163 | const schemaVersion = versionMatch ? versionMatch[1] : 'unknown'; |
| 164 | |
| 165 | const sections: string[] = []; |
| 166 | sections.push(HEADER_LINES.join('\n')); |
| 167 | sections.push(`import { z } from 'zod';`); |
| 168 | sections.push(`export const COMMONS_SCHEMA_VERSION = ${JSON.stringify(schemaVersion)} as const;`); |
| 169 | |
| 170 | for (const table of parsed.tables) { |
| 171 | sections.push(renderRowSchema(table)); |
| 172 | sections.push(renderTableMeta(table)); |
| 173 | } |
| 174 | |
| 175 | sections.push( |
| 176 | [ |
| 177 | '/**', |
| 178 | ' * The wasteland commons DDL split into individual statements, ready to', |
| 179 | " * feed to DoltHub's write API one at a time. Order is preserved from", |
| 180 | ' * the source file. Statements have no trailing `;` so callers can append', |
| 181 | ' * one (or not) as the API expects.', |
| 182 | ' */', |
| 183 | renderSchemaStatements(parsed.statements.map(renderCreateTableStatement)), |
| 184 | ].join('\n') |
| 185 | ); |
| 186 | |
| 187 | return sections.join('\n\n') + '\n'; |
| 188 | } |
| 189 | |
| 190 | // --------------------------------------------------------------------------- |
| 191 | // DML helpers |
no test coverage detected