Skip to content

Commit

Permalink
chore: reset database sequence and permanent link for demo apps
Browse files Browse the repository at this point in the history
  • Loading branch information
icyleaf committed Oct 18, 2024
1 parent c327913 commit 27ee091
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 20 deletions.
43 changes: 36 additions & 7 deletions app/jobs/reset_for_demo_mode_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
class ResetForDemoModeJob < ApplicationJob
queue_as :schedule

discard_on GoodJob::Job::ActionForStateMismatchError

def perform
unless Setting.demo_mode
logger.warn("Zealot is not in demo mode, can not execute ResetForDemoModeJob.")
return
end
return unless demo_mode?

clean_app_data
init_demo_data
reset_jobs
# reset_jobs

# Clear cache
SolidCache::Entry.clear_delete
end

private

def clean_app_data
clear_all_data
reset_table_auto_sequences
end

def clear_all_data
# Application
Metadatum.destroy_all
DebugFile.destroy_all
Expand All @@ -26,20 +33,42 @@ def clean_app_data
Setting.destroy_all
AppleKey.destroy_all
User.destroy_all
end

# Cache in DB
SolidCache::Entry.clear_delete
def reset_table_auto_sequences
[
App, Scheme, Channel, Release, Device, Metadatum, DebugFile,
Setting, AppleKey, AppleTeam, User, WebHook, Backup
].each do |model|
sequence_name = ActiveRecord::Base.connection.execute(
"SELECT pg_get_serial_sequence('#{model.table_name}', '#{model.primary_key}')"
).first['pg_get_serial_sequence']

ActiveRecord::Base.connection.execute(
"SELECT setval('#{sequence_name}', 1, false)"
)
end
end

def reset_jobs
GoodJob::Job.all.each do |job|
next if job.job_class == self.class.name

job.destroy_job
end

GoodJob.cleanup_preserved_jobs(older_than: 1.second)
end

def init_demo_data
user = CreateAdminService.new.call
CreateSampleAppsService.new.call(user)
end

def demo_mode?
return true if Setting.demo_mode

logger.warn("Zealot is not in demo mode, can not execute ResetForDemoModeJob.")
false
end
end
64 changes: 51 additions & 13 deletions app/services/create_sample_apps_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'securerandom'

class CreateSampleAppsService # rubocop:disable Metrics/MethodLength,Metrics/ClassLength
class CreateSampleAppsService # rubocop:disable Metrics/ClassLength
RELEASE_COUNT = 3

def call(user)
Expand Down Expand Up @@ -656,15 +656,51 @@ def create_android_teardown(user) # rubocop:disable Metrics/MethodLength
metadata.save!(validate: false)
end

def stardford_app(user)
def stardford_app(user) # rubocop:disable Metrics/MethodLength
app_name = I18n.t('demo.app_name1')
app_bundle_id = 'com.zealot.app-demo'
channels = %i[Android iOS]
schemes = [
I18n.t('settings.preset_schemes.beta'),
I18n.t('settings.preset_schemes.adhoc'),
I18n.t('settings.preset_schemes.production'),
{
name: I18n.t('settings.preset_schemes.beta'),
channels: [
{
name: 'Android',
slug: 'appBetaAndroid'
},
{
name: 'iOS',
slug: 'appBetaiPhone'
}
]
},
{
name: I18n.t('settings.preset_schemes.adhoc'),
channels: [
{
name: 'Android',
slug: 'appAdhocAndroid'
},
{
name: 'iOS',
slug: 'appAdhociPhone'
}
]
},
{
name: I18n.t('settings.preset_schemes.production'),
channels: [
{
name: 'Android',
slug: 'appProductionAndroid'
},
{
name: 'iOS',
slug: 'appProductioniPhone'
}
]
}
]

changelog = [
{
author: I18n.t('settings.preset_role.developer'),
Expand All @@ -688,11 +724,11 @@ def stardford_app(user)

app = create_app(app_name, user)

schemes.each do |scheme_name|
scheme = app.schemes.find_or_create_by name: scheme_name
channels.each do |channel_name|
channel = scheme.channels.find_or_create_by name: channel_name,
device_type: channel_name.downcase.to_sym
schemes.each do |scheme_data|
scheme = app.schemes.find_or_create_by name: scheme_data[:name]
scheme_data[:channels].each do |channel_data|
channel_data[:device_type] = channel_data[:name].downcase.to_sym
channel = scheme.channels.find_or_create_by channel_data
bundle_id = generate_bundle_id app_bundle_id, channel
release_type = case scheme.name
when I18n.t('settings.preset_schemes.beta')
Expand Down Expand Up @@ -723,16 +759,18 @@ def android_channels_app(user)
app_name = I18n.t('demo.app_name2')
app_bundle_id = 'com.zealot.android.app-demo'
schemes = [ I18n.t('settings.preset_schemes.production') ]
channels = I18n.t('demo.android_channels').values
channels = I18n.t('demo.android_channels', locale: :en).values
changelog = "bump 0.1.0\nfix: xxx\nfeat: xxx done"

app = create_app(app_name, user)

schemes.each do |scheme_name|
scheme = app.schemes.find_or_create_by name: scheme_name
channels.each_with_index do |channel_name, i|
slug = CGI.escape("android#{channel_name.capitalize}")
channel = scheme.channels.find_or_create_by name: channel_name,
device_type: :android
device_type: :android,
slug: slug
generate_release(
channel,
app_bundle_id,
Expand Down

0 comments on commit 27ee091

Please sign in to comment.