Skip to content

Commit

Permalink
Create custom DC mysql tables for new database (#1312)
Browse files Browse the repository at this point in the history
  • Loading branch information
shifucun authored Jan 9, 2024
1 parent fafe01c commit 533eb4b
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
8 changes: 8 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/datacommonsorg/mixer/internal/server"
"github.com/datacommonsorg/mixer/internal/server/cache"
"github.com/datacommonsorg/mixer/internal/server/healthcheck"
"github.com/datacommonsorg/mixer/internal/sqldb"
"github.com/datacommonsorg/mixer/internal/sqldb/cloudsql"
"github.com/datacommonsorg/mixer/internal/sqldb/sqlite"
"github.com/datacommonsorg/mixer/internal/store"
Expand Down Expand Up @@ -203,6 +204,13 @@ func main() {
}
}

// Create tables for new database.
if *useSQLite || *useCloudSQL {
if err := sqldb.CreateTables(sqlClient); err != nil {
log.Fatalf("Can not create tables in database: %v", err)
}
}

// Store
if len(tables) == 0 && *remoteMixerDomain == "" && sqlClient == nil {
log.Fatal("No bigtables or remote mixer domain or sql database are provided")
Expand Down
50 changes: 50 additions & 0 deletions internal/sqldb/createtables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 Google LLC
//
// 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
//
// https://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 sqldb

import (
"database/sql"
)

// CreateTables creates tables in MySQL database if they are not present.
func CreateTables(sqlClient *sql.DB) error {
tripleStatement := `
CREATE TABLE IF NOT EXISTS triples (
subject_id TEXT,
predicate TEXT,
object_id TEXT,
object_value TEXT
);
`
_, err := sqlClient.Exec(tripleStatement)
if err != nil {
return err
}

observationStatement := `
CREATE TABLE IF NOT EXISTS observations (
entity TEXT,
variable TEXT,
date TEXT,
value TEXT,
provenance TEXT
);
`
_, err = sqlClient.Exec(observationStatement)
if err != nil {
return err
}
return nil
}
36 changes: 1 addition & 35 deletions internal/sqldb/sqlite/sqlite.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Google LLC
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -20,36 +20,6 @@ import (
"path/filepath"
)

func createTables(sqlClient *sql.DB) error {
tripleStatement := `
CREATE TABLE IF NOT EXISTS triples (
subject_id TEXT,
predicate TEXT,
object_id TEXT,
object_value TEXT
);
`
_, err := sqlClient.Exec(tripleStatement)
if err != nil {
return err
}

observationStatement := `
CREATE TABLE IF NOT EXISTS observations (
entity TEXT,
variable TEXT,
date TEXT,
value TEXT,
provenance TEXT
);
`
_, err = sqlClient.Exec(observationStatement)
if err != nil {
return err
}
return nil
}

func ConnectDB(dbPath string) (*sql.DB, error) {
// Create all intermediate directories.
if err := os.MkdirAll(filepath.Dir(dbPath), 0755); err != nil {
Expand All @@ -74,9 +44,5 @@ func ConnectDB(dbPath string) (*sql.DB, error) {
if err != nil {
return nil, err
}
err = createTables(sqlClient)
if err != nil {
return nil, err
}
return sqlClient, err
}

0 comments on commit 533eb4b

Please sign in to comment.