diff --git a/make/common/db/registry.sql b/make/common/db/registry.sql index 9b764e901..e2d364ea9 100644 --- a/make/common/db/registry.sql +++ b/make/common/db/registry.sql @@ -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'); diff --git a/make/common/db/registry_sqlite.sql b/make/common/db/registry_sqlite.sql index 7ccd6bc12..a846e124a 100644 --- a/make/common/db/registry_sqlite.sql +++ b/make/common/db/registry_sqlite.sql @@ -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'); diff --git a/src/common/dao/base.go b/src/common/dao/base.go index 86f107a1e..1da44e0f2 100644 --- a/src/common/dao/base.go +++ b/src/common/dao/base.go @@ -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 } diff --git a/src/common/dao/version.go b/src/common/dao/version.go new file mode 100644 index 000000000..c3dc8fe9e --- /dev/null +++ b/src/common/dao/version.go @@ -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 +} diff --git a/src/common/dao/version_test.go b/src/common/dao/version_test.go new file mode 100644 index 000000000..1ee432208 --- /dev/null +++ b/src/common/dao/version_test.go @@ -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) +} diff --git a/src/common/models/version.go b/src/common/models/version.go new file mode 100644 index 000000000..9fc892799 --- /dev/null +++ b/src/common/models/version.go @@ -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)"` +}