class CreateSimplecrmSchema < ActiveRecord::Migration[8.1] def change create_table :users do |t| t.string :email, null: false t.string :password_digest, null: false t.string :first_name, null: false t.string :last_name, null: false t.string :role, null: false, default: "user" t.boolean :active, null: false, default: true t.string :password_reset_token t.datetime :password_reset_sent_at t.bigint :created_by_id t.bigint :updated_by_id t.timestamps end add_index :users, :email, unique: true add_index :users, :password_reset_token, unique: true add_index :users, :role create_table :products do |t| t.string :name, null: false t.string :code, null: false t.boolean :active, null: false, default: true t.integer :position, null: false, default: 0 t.bigint :created_by_id t.bigint :updated_by_id t.timestamps end add_index :products, :code, unique: true add_index :products, :position create_table :organizations do |t| t.string :name, null: false t.string :legal_name t.string :organization_type, null: false, default: "altro" t.string :sport t.string :country, null: false, default: "Italia" t.string :region t.string :province t.string :city t.string :address t.string :website t.string :phone t.string :email t.string :vat_number t.text :notes t.string :status, null: false, default: "prospect" t.string :lead_source t.references :assigned_user, foreign_key: { to_table: :users } t.bigint :created_by_id t.bigint :updated_by_id t.timestamps end add_index :organizations, :status add_index :organizations, :lead_source add_index :organizations, :organization_type add_index :organizations, :email add_index :organizations, :name add_index :organizations, :city add_index :organizations, :website add_index :organizations, :sport add_index :organizations, :country add_index :organizations, :region create_table :contacts do |t| t.references :organization, null: false, foreign_key: true t.string :first_name, null: false t.string :last_name, null: false t.string :role t.string :email t.string :phone t.string :mobile t.string :preferred_contact_method, default: "email" t.text :notes t.boolean :primary_contact, null: false, default: false t.bigint :created_by_id t.bigint :updated_by_id t.timestamps end add_index :contacts, :email add_index :contacts, :phone add_index :contacts, :primary_contact add_index :contacts, [:last_name, :first_name] create_table :opportunities do |t| t.references :organization, null: false, foreign_key: true t.string :name, null: false t.string :pipeline_stage, null: false, default: "to_contact" t.decimal :estimated_value, precision: 12, scale: 2 t.integer :probability, default: 0 t.date :expected_close_date t.string :product t.references :assigned_user, foreign_key: { to_table: :users } t.string :lost_reason t.text :notes t.datetime :won_at t.datetime :lost_at t.datetime :stage_changed_at t.datetime :first_contacted_at t.bigint :created_by_id t.bigint :updated_by_id t.timestamps end add_index :opportunities, :pipeline_stage add_index :opportunities, :lost_reason add_index :opportunities, :product add_index :opportunities, :expected_close_date add_index :opportunities, :stage_changed_at create_table :activities do |t| t.string :activity_type, null: false t.string :subject, null: false t.text :description t.datetime :happened_at, null: false t.references :user, null: false, foreign_key: true t.references :organization, null: false, foreign_key: true t.references :contact, foreign_key: true t.references :opportunity, foreign_key: true t.bigint :created_by_id t.bigint :updated_by_id t.timestamps end add_index :activities, :activity_type add_index :activities, :happened_at add_index :activities, [:organization_id, :happened_at] create_table :tasks do |t| t.string :title, null: false t.text :description t.references :organization, null: false, foreign_key: true t.references :contact, foreign_key: true t.references :opportunity, foreign_key: true t.references :assigned_user, foreign_key: { to_table: :users } t.datetime :due_at, null: false t.datetime :completed_at t.string :priority, null: false, default: "normal" t.string :task_type, null: false, default: "generic" t.string :status, null: false, default: "pending" t.bigint :created_by_id t.bigint :updated_by_id t.timestamps end add_index :tasks, :due_at add_index :tasks, :status add_index :tasks, :priority add_index :tasks, :task_type add_index :tasks, [:status, :due_at] create_table :sales_goals do |t| t.string :name, null: false t.string :metric, null: false t.decimal :target_value, precision: 12, scale: 2, null: false t.date :start_date, null: false t.date :end_date, null: false t.boolean :active, null: false, default: true t.bigint :created_by_id t.bigint :updated_by_id t.timestamps end add_index :sales_goals, :metric add_index :sales_goals, [:start_date, :end_date] add_index :sales_goals, :active end end