@@ -189,17 +189,30 @@ func (r *branchResource) Create(ctx context.Context, req resource.CreateRequest,
189189 return
190190 }
191191
192- // After branch is ready, check if we need to promote it to a production branch
193- if ! data .Production .IsNull () && data .Production .ValueBool () {
194- res , err := r .client .PromoteBranch (ctx , org .ValueString (), database .ValueString (), name .ValueString ())
195- if err != nil {
196- resp .Diagnostics .AddError (
197- "Unable to promote branch during creation" ,
198- fmt .Sprintf ("Branch %s could not be promoted to production: %s" , name .ValueString (), err ),
199- )
200- return
192+ // After the branch is ready, check if we need to promote or demote it
193+ if ! data .Production .IsNull () {
194+ if ! branch .Production && data .Production .ValueBool () {
195+ res , err := r .client .PromoteBranch (ctx , org .ValueString (), database .ValueString (), name .ValueString ())
196+ if err != nil {
197+ resp .Diagnostics .AddError (
198+ "Unable to promote branch during creation" ,
199+ fmt .Sprintf ("Branch %s could not be promoted to production: %s" , name .ValueString (), err ),
200+ )
201+ return
202+ }
203+ branch = res .Branch
204+ }
205+ if branch .Production && ! data .Production .ValueBool () {
206+ res , err := r .client .DemoteBranch (ctx , org .ValueString (), database .ValueString (), name .ValueString ())
207+ if err != nil {
208+ resp .Diagnostics .AddError (
209+ "Unable to demote branch during creation" ,
210+ fmt .Sprintf ("Branch %s could not be demoted to production: %s" , name .ValueString (), err ),
211+ )
212+ return
213+ }
214+ branch = res .Branch
201215 }
202- branch = res .Branch
203216 }
204217
205218 data = branchResourceFromClient (ctx , & branch , data .Organization , data .Database , data .SeedData , resp .Diagnostics )
0 commit comments