mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-23 02:35:17 +01:00
Merge pull request #3536 from ywk253100/171102_fail_earlier
Fail earlier when found database schema dismatch
This commit is contained in:
commit
8dfe5f0bfc
@ -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');
|
||||
|
@ -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');
|
||||
|
@ -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
34
src/common/dao/version.go
Normal 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
|
||||
}
|
28
src/common/dao/version_test.go
Normal file
28
src/common/dao/version_test.go
Normal 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)
|
||||
}
|
20
src/common/models/version.go
Normal file
20
src/common/models/version.go
Normal 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)"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user