UNPKG

@magic-mustard/sqlsync

Version:

SQLSync simplifies database schema evolution by allowing a declarative approach to table management

74 lines (73 loc) 3.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CreateTablesProcessor = void 0; const namespace_1 = require("../namespace"); const namespace_2 = require("./namespace"); const logger_1 = require("../../utils/logger"); class CreateTablesProcessor extends namespace_1.Parser.AbstractProcessor { constructor(file, migrations, state, contentBetweenFlags) { super(file, migrations, state, contentBetweenFlags); } process() { // Extract create table statements from the file content const createTableStatements = this.getCreateTableStatements(); // Process each statement with validations this.processCreateTableStatements(createTableStatements); } getCreateTableStatements() { // Extract CREATE TABLE statements directly from file content const content = this.file.contents; const createTableRegex = /CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?\w+\s*\.\s*\w+\s*\([^;]*\);/gi; const matches = content.match(createTableRegex) || []; logger_1.logger.debug(`Extracted CREATE TABLE statements: ${matches.length} found`); return matches; } hasCreateTableStatements() { try { const statements = this.getCreateTableStatements(); return statements.length > 0; } catch (error) { return false; } } processCreateTableStatements(statements) { // Get the current state for tables const state = this.state.getNamespaceState(namespace_2.Tables.CreateTableStateIndex); for (const statement of statements) { // Extract schema name from CREATE TABLE statement const schemaMatch = statement.match(/CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?(?:(\w+)\.)?(\w+)/i); if (!schemaMatch) { logger_1.logger.error(`Failed to extract schema from CREATE TABLE statement in file: ${this.file.path}`); continue; } const schema = schemaMatch[1]; if (!schema) { throw new Error(`Invalid CREATE TABLE statement in file ${this.file.path}: Schema must be specified (e.g., public.users).`); } const tableName = schemaMatch[2]; const schemaQualifiedTableName = `${schema}.${tableName}`; // Check if the table already exists in the state if (state[namespace_2.Tables.CreateTableStateIndex] && state[namespace_2.Tables.CreateTableStateIndex][schema] && state[namespace_2.Tables.CreateTableStateIndex][schema].includes(schemaQualifiedTableName)) { //logger.info(`CREATE TABLE statement for ${schemaQualifiedTableName} already exists in state. It will not be added to the migration. (file: ${this.file.path})`); continue; } // If new, add to state if (!state[namespace_2.Tables.CreateTableStateIndex]) { state[namespace_2.Tables.CreateTableStateIndex] = {}; } if (!state[namespace_2.Tables.CreateTableStateIndex][schema]) { state[namespace_2.Tables.CreateTableStateIndex][schema] = []; } state[namespace_2.Tables.CreateTableStateIndex][schema].push(schemaQualifiedTableName); this.state.updateNamespaceState(namespace_2.Tables.CreateTableStateIndex, state); // Append to migration this.migrations.append({ token: statement, filePath: this.file.path }); logger_1.logger.success(`Added CREATE TABLE statement for ${schemaQualifiedTableName} to migration. (file: ${this.file.path})`); } } } exports.CreateTablesProcessor = CreateTablesProcessor;