@@ -168,12 +168,9 @@ func runInitCommand(cfg *Config) error {
168168 if err = ioutil .WriteFile (initFilePath + "/reset.sql" , []byte ("reset master;" ), 0644 ); err != nil {
169169 return fmt .Errorf ("failed to write reset.sql: %s" , err )
170170 }
171-
171+ hasInitialized , _ := checkIfPathExists ( path . Join ( dataPath , "mysql" ))
172172 // build init.sql.
173173 initSqlPath := path .Join (extraConfPath , "init.sql" )
174- if err = ioutil .WriteFile (initSqlPath , cfg .buildInitSql (), 0644 ); err != nil {
175- return fmt .Errorf ("failed to write init.sql: %s" , err )
176- }
177174
178175 // build extra.cnf.
179176 extraConfig , err := cfg .buildExtraConfig (initSqlPath )
@@ -183,7 +180,7 @@ func runInitCommand(cfg *Config) error {
183180
184181 // Notice: plugin.cnf cannot be copied to /etc/mysql/conf.d when initialized.
185182 // Check /var/lib/mysql/mysql exists. if exists it means that been initialized.
186- if exists , _ := checkIfPathExists ( path . Join ( dataPath , "mysql" )); exists || strings .HasPrefix (getEnvValue ("MYSQL_VERSION" ), "5" ) {
183+ if hasInitialized || strings .HasPrefix (getEnvValue ("MYSQL_VERSION" ), "5" ) {
187184 // Save plugin.cnf and extra.cnf to /etc/mysql/conf.d.
188185 saveCnfTo (extraConfPath , extraConfig )
189186 } else {
@@ -228,7 +225,7 @@ func runInitCommand(cfg *Config) error {
228225 // Check datadir is empty.
229226 // if /var/lib/mysql/mysql is empty, then run the restore.
230227 // otherwise , it must be has data, then do nothing.
231- if exists , _ := checkIfPathExists ( dataPath + "/mysql" ); ! exists {
228+ if ! hasInitialized {
232229 if len (cfg .XRestoreFrom ) != 0 {
233230 var err_f error
234231 if cfg .CloneFlag {
@@ -241,10 +238,14 @@ func runInitCommand(cfg *Config) error {
241238 return fmt .Errorf ("failed to restore from %s: %s" , cfg .XRestoreFrom , err )
242239 }
243240 }
244-
241+ // Check has initialized again.
242+ hasInitialized , _ = checkIfPathExists (path .Join (dataPath , "mysql" ))
245243 }
246244 }
247-
245+ // Build init.sql after restore
246+ if err = ioutil .WriteFile (initSqlPath , cfg .buildInitSql (hasInitialized ), 0644 ); err != nil {
247+ return fmt .Errorf ("failed to write init.sql: %s" , err )
248+ }
248249 // build xenon.json.
249250 xenonFilePath := path .Join (xenonPath , "xenon.json" )
250251 if err = ioutil .WriteFile (xenonFilePath , cfg .buildXenonConf (), 0644 ); err != nil {
0 commit comments