Files
eminuxCRM/db/migrate/20260816120000_add_projects.rb
eminuxandCursor c4e5f289cf
CI / scan_ruby (push) Failing after 11m20s
CI / scan_js (push) Successful in 10m35s
CI / lint (push) Has been cancelled
Commit iniziale di eminuxCRM: CRM Rails con pipeline, campagne email e Docker.
Include autenticazione, progetti isolati, mail marketing HTML con SMTP, test A/B e editor WYSIWYG.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-17 23:01:48 +02:00

76 lines
2.9 KiB
Ruby

class AddProjects < ActiveRecord::Migration[8.1]
def up
create_table :projects do |t|
t.string :name, null: false
t.string :code, null: false
t.text :description
t.boolean :active, null: false, default: true
t.integer :position, null: false, default: 0
t.string :color, default: "#0f172a"
t.bigint :created_by_id
t.bigint :updated_by_id
t.timestamps
end
add_index :projects, :code, unique: true
add_index :projects, :active
add_index :projects, :position
create_table :user_projects do |t|
t.references :user, null: false, foreign_key: true
t.references :project, null: false, foreign_key: true
t.boolean :enabled, null: false, default: true
t.timestamps
end
add_index :user_projects, [:user_id, :project_id], unique: true
add_index :user_projects, :enabled
create_table :organization_projects do |t|
t.references :organization, null: false, foreign_key: true
t.references :project, null: false, foreign_key: true
t.timestamps
end
add_index :organization_projects, [:organization_id, :project_id], unique: true, name: "idx_org_projects_unique"
add_reference :opportunities, :project, foreign_key: true
add_reference :sales_goals, :project, foreign_key: true
say_with_time "Backfill default projects" do
now = quote(Time.current)
execute <<~SQL.squish
INSERT INTO projects (name, code, description, active, position, color, created_at, updated_at) VALUES
('MatchLiveTV', 'matchlivetv', 'Streaming e diritti sportivi', TRUE, 0, '#0f172a', #{now}, #{now}),
('RiskMeter', 'riskmeter', 'Risk management', TRUE, 1, '#0369a1', #{now}, #{now}),
('Cardoo', 'cardoo', 'Cardoo', TRUE, 2, '#047857', #{now}, #{now})
SQL
project_id = select_value("SELECT id FROM projects WHERE code = 'matchlivetv'")
execute <<~SQL.squish
INSERT INTO organization_projects (organization_id, project_id, created_at, updated_at)
SELECT id, #{project_id}, #{now}, #{now} FROM organizations
ON CONFLICT (organization_id, project_id) DO NOTHING
SQL
execute "UPDATE opportunities SET project_id = #{project_id} WHERE project_id IS NULL"
execute "UPDATE sales_goals SET project_id = #{project_id} WHERE project_id IS NULL"
execute <<~SQL.squish
INSERT INTO user_projects (user_id, project_id, enabled, created_at, updated_at)
SELECT u.id, p.id, TRUE, #{now}, #{now}
FROM users u
CROSS JOIN projects p
WHERE u.role = 'user'
ON CONFLICT (user_id, project_id) DO NOTHING
SQL
end
end
def down
remove_reference :sales_goals, :project, foreign_key: true
remove_reference :opportunities, :project, foreign_key: true
drop_table :organization_projects
drop_table :user_projects
drop_table :projects
end
end