method name. Each runs once, in order, after dbDelta. * * @var array */ private const MIGRATIONS = array( 1 => 'migrate_1_initial', ); /** * Fully qualified table name. * * @param string $name One of calculators, leads, lead_activity, pricing_rules. */ public function table( string $name ): string { global $wpdb; return $wpdb->prefix . 'thinroof_' . $name; } /** * All table names owned by the Free plugin. * * @return string[] */ public function tables(): array { return array( $this->table( 'calculators' ), $this->table( 'leads' ), $this->table( 'lead_activity' ), $this->table( 'pricing_rules' ), ); } /** * Run upgrades if the stored version is behind. Cheap when up to date. */ public function maybe_upgrade(): void { $installed = (int) get_option( self::OPTION_VERSION, 0 ); if ( $installed >= self::VERSION ) { return; } $this->create_tables(); foreach ( self::MIGRATIONS as $version => $method ) { if ( $version <= $installed ) { continue; } $this->{$method}(); update_option( self::OPTION_VERSION, $version, true ); } } /** * SQL for every table (dbDelta format: one column per line, two spaces after PRIMARY KEY). * * @return string[] */ public function schema(): array { global $wpdb; $collate = $wpdb->get_charset_collate(); $calculators = $this->table( 'calculators' ); $leads = $this->table( 'leads' ); $activity = $this->table( 'lead_activity' ); $pricing = $this->table( 'pricing_rules' ); return array( "CREATE TABLE {$calculators} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, title varchar(191) NOT NULL DEFAULT '', status varchar(20) NOT NULL DEFAULT 'active', config longtext NULL, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', updated_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), KEY status (status) ) {$collate};", "CREATE TABLE {$leads} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, calculator_id bigint(20) unsigned NOT NULL DEFAULT 0, status varchar(32) NOT NULL DEFAULT 'new', full_name varchar(191) NOT NULL DEFAULT '', email varchar(191) NOT NULL DEFAULT '', phone varchar(32) NOT NULL DEFAULT '', zip varchar(10) NOT NULL DEFAULT '', property_type varchar(40) NOT NULL DEFAULT '', project_type varchar(40) NOT NULL DEFAULT '', material varchar(60) NOT NULL DEFAULT '', pitch varchar(40) NOT NULL DEFAULT '', stories tinyint(3) unsigned NOT NULL DEFAULT 1, tear_off tinyint(1) NOT NULL DEFAULT 0, input_area int(10) unsigned NOT NULL DEFAULT 0, roof_area int(10) unsigned NOT NULL DEFAULT 0, budget varchar(40) NOT NULL DEFAULT '', timeline varchar(40) NOT NULL DEFAULT '', notes text NULL, estimate_total decimal(12,2) NOT NULL DEFAULT 0.00, estimate_low decimal(12,2) NOT NULL DEFAULT 0.00, estimate_high decimal(12,2) NOT NULL DEFAULT 0.00, estimate_data longtext NULL, marketing_consent tinyint(1) NOT NULL DEFAULT 0, consent_text text NULL, consent_at datetime NULL DEFAULT NULL, source_url varchar(500) NOT NULL DEFAULT '', created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', updated_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), KEY status_created (status,created_at), KEY created_at (created_at), KEY calculator_id (calculator_id), KEY email (email), KEY zip (zip) ) {$collate};", "CREATE TABLE {$activity} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, lead_id bigint(20) unsigned NOT NULL DEFAULT 0, type varchar(32) NOT NULL DEFAULT '', message text NULL, user_id bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), KEY lead_created (lead_id,created_at) ) {$collate};", "CREATE TABLE {$pricing} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, calculator_id bigint(20) unsigned NOT NULL DEFAULT 0, material_key varchar(60) NOT NULL DEFAULT '', label varchar(191) NOT NULL DEFAULT '', material_cost decimal(10,2) NOT NULL DEFAULT 0.00, labor_cost decimal(10,2) NOT NULL DEFAULT 0.00, waste_pct decimal(5,2) NULL DEFAULT NULL, is_active tinyint(1) NOT NULL DEFAULT 1, sort_order int(11) NOT NULL DEFAULT 0, updated_at datetime NOT NULL DEFAULT '0000-00-00 00:00:00', PRIMARY KEY (id), UNIQUE KEY calc_material (calculator_id,material_key) ) {$collate};", ); } /** * Create or update tables with dbDelta. */ public function create_tables(): void { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; foreach ( $this->schema() as $sql ) { dbDelta( $sql ); } } /** * Migration 1: seed example pricing rows when the table is empty. */ private function migrate_1_initial(): void { ( new Pricing_Repository( $this ) )->seed_defaults(); } }