Commit a6a3d36f authored by Alper Akgun's avatar Alper Akgun

Collect the date a SaaS trial starts on

Adds a trial_starts_on column to gitlab_subscriptions (and the
gitlab_subscription_histories  tables) and sets it to the start_date
whenever a new trial is created or updated.
parent 35bf352a
---
title: Collect the date a SaaS trial starts on
merge_request: 20384
author:
type: added
# frozen_string_literal: true
class AddTrialStartsOnToGitlabSubscriptions < ActiveRecord::Migration[5.2]
DOWNTIME = false
def change
add_column :gitlab_subscriptions, :trial_starts_on, :date, null: true
add_column :gitlab_subscription_histories, :trial_starts_on, :date, null: true
end
end
...@@ -1849,6 +1849,7 @@ ActiveRecord::Schema.define(version: 2019_12_06_122926) do ...@@ -1849,6 +1849,7 @@ ActiveRecord::Schema.define(version: 2019_12_06_122926) do
t.integer "change_type", limit: 2 t.integer "change_type", limit: 2
t.bigint "gitlab_subscription_id", null: false t.bigint "gitlab_subscription_id", null: false
t.datetime_with_timezone "created_at" t.datetime_with_timezone "created_at"
t.date "trial_starts_on"
t.index ["gitlab_subscription_id"], name: "index_gitlab_subscription_histories_on_gitlab_subscription_id" t.index ["gitlab_subscription_id"], name: "index_gitlab_subscription_histories_on_gitlab_subscription_id"
end end
...@@ -1863,6 +1864,7 @@ ActiveRecord::Schema.define(version: 2019_12_06_122926) do ...@@ -1863,6 +1864,7 @@ ActiveRecord::Schema.define(version: 2019_12_06_122926) do
t.integer "max_seats_used", default: 0 t.integer "max_seats_used", default: 0
t.integer "seats", default: 0 t.integer "seats", default: 0
t.boolean "trial", default: false t.boolean "trial", default: false
t.date "trial_starts_on"
t.index ["hosted_plan_id"], name: "index_gitlab_subscriptions_on_hosted_plan_id" t.index ["hosted_plan_id"], name: "index_gitlab_subscriptions_on_hosted_plan_id"
t.index ["namespace_id"], name: "index_gitlab_subscriptions_on_namespace_id", unique: true t.index ["namespace_id"], name: "index_gitlab_subscriptions_on_namespace_id", unique: true
end end
......
...@@ -64,7 +64,7 @@ module EE ...@@ -64,7 +64,7 @@ module EE
validate :validate_plan_name validate :validate_plan_name
validate :validate_shared_runner_minutes_support validate :validate_shared_runner_minutes_support
delegate :trial?, :trial_ends_on, :upgradable?, to: :gitlab_subscription, allow_nil: true delegate :trial?, :trial_ends_on, :trial_starts_on, :upgradable?, to: :gitlab_subscription, allow_nil: true
before_create :sync_membership_lock_with_parent before_create :sync_membership_lock_with_parent
......
...@@ -42,6 +42,7 @@ module EE ...@@ -42,6 +42,7 @@ module EE
optional :end_date, type: Date, desc: 'The date when subscription expires' optional :end_date, type: Date, desc: 'The date when subscription expires'
optional :trial, type: Grape::API::Boolean, desc: 'Wether the subscription is trial' optional :trial, type: Grape::API::Boolean, desc: 'Wether the subscription is trial'
optional :trial_ends_on, type: Date, desc: 'The date when the trial expires' optional :trial_ends_on, type: Date, desc: 'The date when the trial expires'
optional :trial_starts_on, type: Date, desc: 'The date when the trial starts'
end end
end end
...@@ -82,6 +83,7 @@ module EE ...@@ -82,6 +83,7 @@ module EE
namespace = find_namespace!(params[:id]) namespace = find_namespace!(params[:id])
subscription_params = declared_params(include_missing: false) subscription_params = declared_params(include_missing: false)
subscription_params[:trial_starts_on] ||= subscription_params[:start_date] if subscription_params[:trial]
subscription = namespace.create_gitlab_subscription(subscription_params) subscription = namespace.create_gitlab_subscription(subscription_params)
if subscription.persisted? if subscription.persisted?
present subscription, with: ::EE::API::Entities::GitlabSubscription present subscription, with: ::EE::API::Entities::GitlabSubscription
...@@ -118,7 +120,10 @@ module EE ...@@ -118,7 +120,10 @@ module EE
not_found!('GitlabSubscription') unless subscription not_found!('GitlabSubscription') unless subscription
bad_request!("Invalid trial expiration date") if trial_ends_on&.past? bad_request!("Invalid trial expiration date") if trial_ends_on&.past?
if subscription.update(declared_params(include_missing: false)) subscription_params = declared_params(include_missing: false)
subscription_params[:trial_starts_on] ||= subscription_params[:start_date] if subscription_params[:trial]
if subscription.update(subscription_params)
present subscription, with: ::EE::API::Entities::GitlabSubscription present subscription, with: ::EE::API::Entities::GitlabSubscription
else else
render_validation_error!(subscription) render_validation_error!(subscription)
......
...@@ -220,6 +220,13 @@ describe API::Namespaces do ...@@ -220,6 +220,13 @@ describe API::Namespaces do
expect(group1.gitlab_subscription).to be_present expect(group1.gitlab_subscription).to be_present
end end
it 'sets the trial_starts_on to the start_date' do
do_post(admin, params.merge(trial: true))
expect(group1.gitlab_subscription.trial_starts_on).to be_present
expect(group1.gitlab_subscription.trial_starts_on.strftime('%d/%m/%Y')).to eq(params[:start_date])
end
it 'creates a subscription using full_path when the namespace path contains dots' do it 'creates a subscription using full_path when the namespace path contains dots' do
post api("/namespaces/#{group1.full_path}/gitlab_subscription", admin), params: params post api("/namespaces/#{group1.full_path}/gitlab_subscription", admin), params: params
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment