fix: make migration robust and ensure CI/CD applies kustomization
- Migration now creates default 'General' department for businesses that have products but no departments, preventing NOT NULL constraint violations - CI/CD deploy-dev now uses 'kubectl apply -k' instead of 'kubectl set image' to apply full kustomization including deployment patch - This ensures 'migrate:fresh --seed --force' runs on every dev deployment, enforcing the CI/CD policy to always run seeders Fixes issues where: 1. Migration failed on dev because no departments existed 2. Deployment patches weren't being applied (seeders not running) 3. Products created without departments causing picking ticket failures 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -165,7 +165,7 @@ steps:
|
||||
|
||||
# Auto-deploy to dev.cannabrands.app (develop branch only)
|
||||
deploy-dev:
|
||||
image: bitnami/kubectl:latest
|
||||
image: line/kubectl-kustomize:latest
|
||||
environment:
|
||||
KUBECONFIG_CONTENT:
|
||||
from_secret: kubeconfig_dev
|
||||
@@ -177,12 +177,14 @@ steps:
|
||||
- mkdir -p ~/.kube
|
||||
- echo "$KUBECONFIG_CONTENT" | tr -d '[:space:]' | base64 -d > ~/.kube/config
|
||||
- chmod 600 ~/.kube/config
|
||||
# Update deployment to use new SHA-tagged image (both app and init containers)
|
||||
# Update image tags in kustomization for dev environment
|
||||
- cd k8s/overlays/dev
|
||||
- |
|
||||
kubectl set image deployment/cannabrands-hub \
|
||||
app=code.cannabrands.app/cannabrands/hub:dev-${CI_COMMIT_SHA:0:7} \
|
||||
migrate=code.cannabrands.app/cannabrands/hub:dev-${CI_COMMIT_SHA:0:7} \
|
||||
-n cannabrands-dev
|
||||
kustomize edit set image \
|
||||
code.cannabrands.app/cannabrands/hub:dev=code.cannabrands.app/cannabrands/hub:dev-${CI_COMMIT_SHA:0:7}
|
||||
# Apply full kustomization (includes deployment patch with migrate:fresh --seed)
|
||||
- kubectl apply -k . --prune -l app=cannabrands-hub
|
||||
- cd ../../..
|
||||
# Wait for rollout to complete (timeout 5 minutes)
|
||||
- kubectl rollout status deployment/cannabrands-hub -n cannabrands-dev --timeout=300s
|
||||
# Verify deployment health
|
||||
@@ -195,6 +197,9 @@ steps:
|
||||
echo "Image deployed:"
|
||||
kubectl get deployment cannabrands-hub -n cannabrands-dev -o jsonpath='{.spec.template.spec.containers[0].image}'
|
||||
echo ""
|
||||
echo "Init container command:"
|
||||
kubectl get deployment cannabrands-hub -n cannabrands-dev -o jsonpath='{.spec.template.spec.initContainers[0].args[0]}' | grep -o "migrate[^\"]*"
|
||||
echo ""
|
||||
when:
|
||||
branch: develop
|
||||
event: push
|
||||
|
||||
@@ -12,7 +12,27 @@ return new class extends Migration
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// First, assign a default department to all products without one
|
||||
// Step 1: Create default departments for businesses that have products but no departments
|
||||
// This ensures every business with products has at least one department
|
||||
DB::statement("
|
||||
INSERT INTO departments (business_id, name, description, is_active, sort_order, created_at, updated_at)
|
||||
SELECT DISTINCT
|
||||
b.business_id,
|
||||
'General',
|
||||
'Default department for products',
|
||||
true,
|
||||
0,
|
||||
NOW(),
|
||||
NOW()
|
||||
FROM brands b
|
||||
INNER JOIN products p ON p.brand_id = b.id
|
||||
WHERE p.department_id IS NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM departments d WHERE d.business_id = b.business_id
|
||||
)
|
||||
");
|
||||
|
||||
// Step 2: Assign default department to products that don't have one
|
||||
// Get the first department for each business and assign it to products
|
||||
DB::statement('
|
||||
UPDATE products p
|
||||
@@ -27,7 +47,7 @@ return new class extends Migration
|
||||
WHERE p.department_id IS NULL
|
||||
');
|
||||
|
||||
// Now make the column NOT NULL
|
||||
// Step 3: Make the column NOT NULL
|
||||
Schema::table('products', function (Blueprint $table) {
|
||||
$table->foreignId('department_id')->nullable(false)->change();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user