Merge pull request #3536 from ywk253100/171102_fail_earlier

Fail earlier when found database schema dismatch
This commit is contained in:
Daniel Jiang 2017-11-07 15:01:14 +08:00 committed by GitHub
commit 8dfe5f0bfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 94 additions and 2 deletions

View File

@ -230,4 +230,4 @@ CREATE TABLE IF NOT EXISTS `alembic_version` (
`version_num` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
insert into alembic_version values ('1.2.0');
insert into alembic_version values ('1.3.0');

View File

@ -221,4 +221,4 @@ create table alembic_version (
version_num varchar(32) NOT NULL
);
insert into alembic_version values ('0.3.0');
insert into alembic_version values ('1.3.0');

View File

@ -71,6 +71,16 @@ func InitDatabase(database *models.Database) error {
if err := db.Register(); err != nil {
return err
}
version, err := GetSchemaVersion()
if err != nil {
return err
}
if version.Version != SchemaVersion {
return fmt.Errorf("unexpected database schema version, expected %s, got %s",
SchemaVersion, version.Version)
}
log.Info("initialize database completed")
return nil
}

34
src/common/dao/version.go Normal file
View File

@ -0,0 +1,34 @@
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dao
import (
"github.com/vmware/harbor/src/common/models"
)
const (
// SchemaVersion is the version of database schema
SchemaVersion = "1.3.0"
)
// GetSchemaVersion return the version of database schema
func GetSchemaVersion() (*models.SchemaVersion, error) {
version := &models.SchemaVersion{}
if err := GetOrmer().Raw("select version_num from alembic_version").
QueryRow(version); err != nil {
return nil, err
}
return version, nil
}

View File

@ -0,0 +1,28 @@
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dao
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetSchemaVersion(t *testing.T) {
version, err := GetSchemaVersion()
require.Nil(t, err)
assert.Equal(t, SchemaVersion, version.Version)
}

View File

@ -0,0 +1,20 @@
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
// SchemaVersion is the version of database schema
type SchemaVersion struct {
Version string `json:"version" orm:"column(version_num)"`
}