diff --git a/internal/cli/discourse_reset.go b/internal/cli/discourse_reset.go index 7790a82..63a8f41 100644 --- a/internal/cli/discourse_reset.go +++ b/internal/cli/discourse_reset.go @@ -62,6 +62,18 @@ func buildAssetsClobberCommands() []string { ) } +// buildSchemaCacheClearCommands generates commands to remove any cached schema +// YAML files after a branch switch. The cache is tied to a specific schema +// version; leaving it in place after switching branches causes Rails to +// misread column types (e.g. boolean instead of integer defaults), which +// crashes boot before assets:clobber or migrations can run. +func buildSchemaCacheClearCommands() []string { + return []string{ + "echo 'Clearing stale schema cache...'", + "find . -name 'schema_cache.yml*' -delete 2>/dev/null || true", + } +} + // buildDatabaseDropCreateMigrateCommands generates commands to drop, create, and migrate databases. // When skipDBReset is true, db:drop and db:create are omitted, as well as user seeding. // Only migrations and later steps run. @@ -168,6 +180,9 @@ func buildDiscourseResetScript(checkoutCmds []string, opts discourseResetScriptO // Post-checkout steps lines = append(lines, buildPostCheckoutCommands()...) + // Schema cache must be cleared before Rails boots on the new branch + lines = append(lines, buildSchemaCacheClearCommands()...) + // Rails asset cleanup lines = append(lines, buildAssetsClobberCommands()...) diff --git a/internal/cli/new.go b/internal/cli/new.go index 6c4383a..8724162 100644 --- a/internal/cli/new.go +++ b/internal/cli/new.go @@ -536,13 +536,15 @@ git fetch origin "$_branch" --tags --prune --force git checkout -B "$_branch" "origin/$_branch" git reset --hard "origin/$_branch" %s -`, shellQuote(branchName), strings.Join(buildAssetsClobberCommands(), "\n")) +%s +`, shellQuote(branchName), strings.Join(buildSchemaCacheClearCommands(), "\n"), strings.Join(buildAssetsClobberCommands(), "\n")) return docker.ExecInteractive(name, workdir, envs, []string{"bash", "-lc", script}) } func checkoutBranch(cmd *cobra.Command, cfg config.Config, name, workdir, branchName string, envs docker.Envs, withoutTestDB bool) error { if branchName == "main" || branchName == "master" { fmt.Fprintf(cmd.OutOrStdout(), "Updating %s branch...\n", branchName) + schemaClearCmds := strings.Join(buildSchemaCacheClearCommands(), "\n") assetClobberCmds := strings.Join(buildAssetsClobberCommands(), "\n") script := fmt.Sprintf(` set -e @@ -552,7 +554,8 @@ git checkout %s > /tmp/dv-git-checkout.log 2>&1 echo "Pulling latest..." git pull > /tmp/dv-git-pull.log 2>&1 %s -`, branchName, branchName, assetClobberCmds) +%s +`, branchName, branchName, schemaClearCmds, assetClobberCmds) return docker.ExecInteractive(name, workdir, envs, []string{"bash", "-lc", script}) } checkoutCmds := buildBranchCheckoutCommands(branchName)