From 1b71895dba15cf53c6ffc786143f6bb60abaaa8d Mon Sep 17 00:00:00 2001
From: Brendan Dougherty <brendan.dougherty@shopify.com>
Date: Thu, 18 Jul 2024 10:36:58 -0400
Subject: [PATCH] Merge pull request #175 from
 Shopify/v19.0.4-shopify-2-candidate-backport-vtcombo-vschema

Backport: VTCombo: Ensure VSchema exists when creating keyspace (#16094)
(cherry picked from commit 4b24c58989675babc2056fe68c8f9f08b48d0a7d)
---
 go/test/endtoend/vtcombo/vttest_sample_test.go | 13 +++++++++++++
 go/vt/vtcombo/tablet_map.go                    |  5 +++++
 2 files changed, 18 insertions(+)

diff --git a/go/test/endtoend/vtcombo/vttest_sample_test.go b/go/test/endtoend/vtcombo/vttest_sample_test.go
index daeb5e8deb9..4895c1195b0 100644
--- a/go/test/endtoend/vtcombo/vttest_sample_test.go
+++ b/go/test/endtoend/vtcombo/vttest_sample_test.go
@@ -130,6 +130,8 @@ func TestStandalone(t *testing.T) {
 	tmp, _ := cmd.([]any)
 	require.Contains(t, tmp[0], "vtcombo")
 
+	assertVSchemaExists(t, grpcAddress)
+
 	ctx := context.Background()
 	conn, err := vtgateconn.Dial(ctx, grpcAddress)
 	require.NoError(t, err)
@@ -160,6 +162,17 @@ func TestStandalone(t *testing.T) {
 	assertTransactionalityAndRollbackObeyed(ctx, t, conn, idStart)
 }
 
+func assertVSchemaExists(t *testing.T, grpcAddress string) {
+	tmpCmd := exec.Command("vtctldclient", "--server", grpcAddress, "--compact", "GetVSchema", "routed")
+
+	log.Infof("Running vtctldclient with command: %v", tmpCmd.Args)
+
+	output, err := tmpCmd.CombinedOutput()
+	require.NoError(t, err, fmt.Sprintf("Output:\n%v", string(output)))
+
+	assert.Equal(t, "{}\n", string(output))
+}
+
 func assertInsertedRowsExist(ctx context.Context, t *testing.T, conn *vtgateconn.VTGateConn, idStart, rowCount int) {
 	cur := conn.Session(ks1+":-80@rdonly", nil)
 	bindVariables := map[string]*querypb.BindVariable{
diff --git a/go/vt/vtcombo/tablet_map.go b/go/vt/vtcombo/tablet_map.go
index 369a8138b5a..84ffa10fd3a 100644
--- a/go/vt/vtcombo/tablet_map.go
+++ b/go/vt/vtcombo/tablet_map.go
@@ -301,6 +301,11 @@ func CreateKs(
 		return 0, fmt.Errorf("CreateKeyspace(%v) failed: %v", keyspace, err)
 	}
 
+	// make sure a valid vschema has been loaded
+	if err := ts.EnsureVSchema(ctx, keyspace); err != nil {
+		return 0, fmt.Errorf("EnsureVSchema(%v) failed: %v", keyspace, err)
+	}
+
 	// iterate through the shards
 	for _, spb := range kpb.Shards {
 		shard := spb.Name