Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

Add timezone_name, timezone_offset to db table

天舟c82b76de2cb8
diff --git a/api/database.go b/api/database.go
index f503687a22..d812c94ed5 100644
--- a/api/database.go
+++ b/api/database.go
@@ -55,6 +55,8 @@ type Database struct {
Collation string `jsonapi:"attr,collation"`
SyncStatus SyncStatus `jsonapi:"attr,syncStatus"`
LastSuccessfulSyncTs int64 `jsonapi:"attr,lastSuccessfulSyncTs"`
+ TimezoneName string `jsonapi:"attr,timezoneName"`
+ TimezoneOffset int `jsonapi:"attr,timezoneOffset"`
}
type DatabaseCreate struct {
@@ -67,10 +69,12 @@ type DatabaseCreate struct {
InstanceId int `jsonapi:"attr,instanceId"`
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- CharacterSet string `jsonapi:"attr,characterSet"`
- Collation string `jsonapi:"attr,collation"`
- IssueId int `jsonapi:"attr,issueId"`
+ Name string `jsonapi:"attr,name"`
+ CharacterSet string `jsonapi:"attr,characterSet"`
+ Collation string `jsonapi:"attr,collation"`
+ IssueId int `jsonapi:"attr,issueId"`
+ TimezoneName string
+ TimezoneOffset int
}
type DatabaseFind struct {
diff --git a/frontend/src/components/DatabaseOverviewPanel.vue b/frontend/src/components/DatabaseOverviewPanel.vue
index 8e5cad413e..bb13673140 100644
--- a/frontend/src/components/DatabaseOverviewPanel.vue
+++ b/frontend/src/components/DatabaseOverviewPanel.vue
@@ -2,7 +2,7 @@
<div class="mt-6">
<div class="max-w-6xl mx-auto px-6 space-y-6 divide-y divide-block-border">
<!-- Description list -->
- <dl class="grid grid-cols-1 gap-x-4 gap-y-4 sm:grid-cols-2">
+ <dl class="grid grid-cols-1 gap-x-4 gap-y-4 sm:grid-cols-3">
<div class="col-span-1 col-start-1">
<dt class="text-sm font-medium text-control-light">Character set</dt>
<dd class="mt-1 text-sm text-main">
@@ -17,6 +17,13 @@
</dd>
</div>
+ <div class="col-span-1">
+ <dt class="text-sm font-medium text-control-light">Timezone</dt>
+ <dd class="mt-1 text-sm text-main">
+ {{ timezoneString(database.timezoneName, database.timezoneOffset) }}
+ </dd>
+ </div>
+
<div class="col-span-1 col-start-1">
<dt class="text-sm font-medium text-control-light">Sync status</dt>
<dd class="mt-1 text-sm text-main">
@@ -193,7 +200,7 @@ import DataSourceTable from "../components/DataSourceTable.vue";
import DataSourceConnectionPanel from "../components/DataSourceConnectionPanel.vue";
import TableTable from "../components/TableTable.vue";
import MigrationHistoryTable from "../components/MigrationHistoryTable.vue";
-import { idFromSlug, instanceSlug, isDBAOrOwner } from "../utils";
+import { timezoneString, instanceSlug, isDBAOrOwner } from "../utils";
import {
Database,
DataSource,
@@ -368,6 +375,7 @@ export default {
};
return {
+ timezoneString,
state,
tableList,
attentionTitle,
diff --git a/frontend/src/types/common.ts b/frontend/src/types/common.ts
index 3474769064..096411db35 100644
--- a/frontend/src/types/common.ts
+++ b/frontend/src/types/common.ts
@@ -207,6 +207,8 @@ export const unknown = (
collation: "",
syncStatus: "NOT_FOUND",
lastSuccessfulSyncTs: 0,
+ timezoneName: "UTC",
+ timezoneOffset: 0,
};
const UNKNOWN_DATA_SOURCE: DataSource = {
@@ -483,6 +485,8 @@ export const empty = (
collation: "",
syncStatus: "NOT_FOUND",
lastSuccessfulSyncTs: 0,
+ timezoneName: "UTC",
+ timezoneOffset: 0,
};
const EMPTY_DATA_SOURCE: DataSource = {
diff --git a/frontend/src/types/database.ts b/frontend/src/types/database.ts
index 8b06b598cf..4226064df8 100644
--- a/frontend/src/types/database.ts
+++ b/frontend/src/types/database.ts
@@ -32,6 +32,8 @@ export type Database = {
name: string;
characterSet: string;
collation: string;
+ timezoneName: string;
+ timezoneOffset: number;
};
export type DatabaseCreate = {
diff --git a/frontend/src/utils/util.ts b/frontend/src/utils/util.ts
index f0e9dfebb9..651f9e530c 100644
--- a/frontend/src/utils/util.ts
+++ b/frontend/src/utils/util.ts
@@ -38,6 +38,18 @@ export function secondsToString(second: number): string {
return moment.duration(second).humanize();
}
+export function timezoneString(zoneName: string, offset: number): string {
+ var sign = "+";
+ if (offset < 0) {
+ sign = "-";
+ }
+ const hour = Math.abs(offset) / 3600;
+ const minutes = Math.abs(offset) & (3600 / 60);
+ return `${zoneName}${sign}${String(hour).padStart(2, "0")}:${String(
+ minutes
+ ).padStart(2, "0")}`;
+}
+
export function urlfy(str: string): string {
let result = str.trim();
if (result.search(/^http[s]?\:\/\//) == -1) {
diff --git a/server/database.go b/server/database.go
index eb9775ce80..9b8140184a 100644
--- a/server/database.go
+++ b/server/database.go
@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
+ "time"
"github.com/bytebase/bytebase"
"github.com/bytebase/bytebase/api"
@@ -20,7 +21,10 @@ func (s *Server) registerDatabaseRoutes(g *echo.Group) {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create database request").SetInternal(err)
}
+ z, offset := time.Now().Zone()
databaseCreate.CreatorId = c.Get(GetPrincipalIdContextKey()).(int)
+ databaseCreate.TimezoneName = z
+ databaseCreate.TimezoneOffset = offset
database, err := s.DatabaseService.CreateDatabase(context.Background(), databaseCreate)
if err != nil {
diff --git a/server/sql.go b/server/sql.go
index ba6af6a406..56b3750905 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -366,13 +366,16 @@ func (s *Server) SyncSchema(instance *api.Instance) (rs *api.SqlResultSet) {
}
} else {
// Case 2
+ z, offset := time.Now().Zone()
databaseCreate := &api.DatabaseCreate{
- CreatorId: api.SYSTEM_BOT_ID,
- ProjectId: api.DEFAULT_PROJECT_ID,
- InstanceId: instance.ID,
- Name: schema.Name,
- CharacterSet: schema.CharacterSet,
- Collation: schema.Collation,
+ CreatorId: api.SYSTEM_BOT_ID,
+ ProjectId: api.DEFAULT_PROJECT_ID,
+ InstanceId: instance.ID,
+ Name: schema.Name,
+ CharacterSet: schema.CharacterSet,
+ Collation: schema.Collation,
+ TimezoneName: z,
+ TimezoneOffset: offset,
}
database, err := s.DatabaseService.CreateDatabase(context.Background(), databaseCreate)
if err != nil {
diff --git a/server/task.go b/server/task.go
index 857db52eae..71b1d3e614 100644
--- a/server/task.go
+++ b/server/task.go
@@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"strconv"
+ "time"
"github.com/bytebase/bytebase"
"github.com/bytebase/bytebase/api"
@@ -250,13 +251,17 @@ func (s *Server) ChangeTaskStatusWithPatch(ctx context.Context, task *api.Task,
if err := json.Unmarshal([]byte(updatedTask.Payload), payload); err != nil {
return nil, fmt.Errorf("invalid create database task payload: %w", err)
}
+
+ z, offset := time.Now().Zone()
databaseCreate := &api.DatabaseCreate{
- CreatorId: taskStatusPatch.UpdaterId,
- ProjectId: issue.ProjectId,
- InstanceId: task.InstanceId,
- Name: payload.DatabaseName,
- CharacterSet: payload.CharacterSet,
- Collation: payload.Collation,
+ CreatorId: taskStatusPatch.UpdaterId,
+ ProjectId: issue.ProjectId,
+ InstanceId: task.InstanceId,
+ Name: payload.DatabaseName,
+ CharacterSet: payload.CharacterSet,
+ Collation: payload.Collation,
+ TimezoneName: z,
+ TimezoneOffset: offset,
}
_, err := s.DatabaseService.CreateDatabase(ctx, databaseCreate)
if err != nil {
diff --git a/store/database.go b/store/database.go
index 6b874b1fe0..5456ef4534 100644
--- a/store/database.go
+++ b/store/database.go
@@ -164,10 +164,12 @@ func (s *DatabaseService) createDatabase(ctx context.Context, tx *sql.Tx, create
character_set,
collation,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
- VALUES (?, ?, ?, ?, ?, ?, ?, 'OK', (strftime('%s', 'now')))
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, project_id, name, character_set, collation, sync_status, last_successful_sync_ts
+ VALUES (?, ?, ?, ?, ?, ?, ?, 'OK', (strftime('%s', 'now')), ?, ?)
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, project_id, name, character_set, collation, sync_status, last_successful_sync_ts, timezone_name, timezone_offset
`,
create.CreatorId,
create.CreatorId,
@@ -176,6 +178,8 @@ func (s *DatabaseService) createDatabase(ctx context.Context, tx *sql.Tx, create
create.Name,
create.CharacterSet,
create.Collation,
+ create.TimezoneName,
+ create.TimezoneOffset,
)
if err != nil {
@@ -198,6 +202,8 @@ func (s *DatabaseService) createDatabase(ctx context.Context, tx *sql.Tx, create
&database.Collation,
&database.SyncStatus,
&database.LastSuccessfulSyncTs,
+ &database.TimezoneName,
+ &database.TimezoneOffset,
); err != nil {
return nil, FormatError(err)
}
@@ -237,7 +243,9 @@ func (s *DatabaseService) findDatabaseList(ctx context.Context, tx *Tx, find *ap
character_set,
collation,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
FROM db
WHERE `+strings.Join(where, " AND "),
args...,
@@ -264,6 +272,8 @@ func (s *DatabaseService) findDatabaseList(ctx context.Context, tx *Tx, find *ap
&database.Collation,
&database.SyncStatus,
&database.LastSuccessfulSyncTs,
+ &database.TimezoneName,
+ &database.TimezoneOffset,
); err != nil {
return nil, FormatError(err)
}
@@ -298,7 +308,7 @@ func (s *DatabaseService) patchDatabase(ctx context.Context, tx *Tx, patch *api.
UPDATE db
SET `+strings.Join(set, ", ")+`
WHERE id = ?
- RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, project_id, name, character_set, collation, sync_status, last_successful_sync_ts
+ RETURNING id, creator_id, created_ts, updater_id, updated_ts, instance_id, project_id, name, character_set, collation, sync_status, last_successful_sync_ts, timezone_name, timezone_offset
`,
args...,
)
@@ -322,6 +332,8 @@ func (s *DatabaseService) patchDatabase(ctx context.Context, tx *Tx, patch *api.
&database.Collation,
&database.SyncStatus,
&database.LastSuccessfulSyncTs,
+ &database.TimezoneName,
+ &database.TimezoneOffset,
); err != nil {
return nil, FormatError(err)
}
diff --git a/store/instance.go b/store/instance.go
index a5db59e54c..f7fb19f238 100644
--- a/store/instance.go
+++ b/store/instance.go
@@ -45,12 +45,14 @@ func (s *InstanceService) CreateInstance(ctx context.Context, create *api.Instan
// Create * database
databaseCreate := &api.DatabaseCreate{
- CreatorId: create.CreatorId,
- ProjectId: api.DEFAULT_PROJECT_ID,
- InstanceId: instance.ID,
- Name: api.ALL_DATABASE_NAME,
- CharacterSet: api.DEFAULT_CHARACTER_SET_NAME,
- Collation: api.DEFAULT_COLLATION_NAME,
+ CreatorId: create.CreatorId,
+ ProjectId: api.DEFAULT_PROJECT_ID,
+ InstanceId: instance.ID,
+ Name: api.ALL_DATABASE_NAME,
+ CharacterSet: api.DEFAULT_CHARACTER_SET_NAME,
+ Collation: api.DEFAULT_COLLATION_NAME,
+ TimezoneName: "UTC",
+ TimezoneOffset: 0,
}
allDatabase, err := s.databaseService.CreateDatabaseTx(ctx, tx.Tx, databaseCreate)
if err != nil {
diff --git a/store/migration/10001__init_schema.sql b/store/migration/10001__init_schema.sql
index 8153fae8fc..c89bed9ac1 100644
--- a/store/migration/10001__init_schema.sql
+++ b/store/migration/10001__init_schema.sql
@@ -328,6 +328,9 @@ END;
-- db stores the databases for a particular instance
-- data is synced periodically from the instance
+-- db table also stores the timezone info, this is used for features like backup and maintenance window
+-- where only hour of day, day of week instead of absolute timestamp are specified. We need timezone
+-- info to calculate the exact time and avoid time drift due to daylight saving change.
CREATE TABLE db (
id INTEGER PRIMARY KEY AUTOINCREMENT,
row_status TEXT NOT NULL CHECK (
@@ -344,6 +347,10 @@ CREATE TABLE db (
name TEXT NOT NULL,
character_set TEXT NOT NULL,
`collation` TEXT NOT NULL,
+ -- abbreviated name of the timezone
+ timezone_name TEXT NOT NULL,
+ -- timezone offset in seconds east of UTC
+ timezone_offset INTEGER NOT NULL,
UNIQUE(instance_id, name)
);
diff --git a/store/seed/release/10001__init_seed.sql b/store/seed/release/10001__init_seed.sql
index 2b6ee849bf..489ba17387 100644
--- a/store/seed/release/10001__init_seed.sql
+++ b/store/seed/release/10001__init_seed.sql
@@ -78,7 +78,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -91,6 +93,8 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
+ 0,
+ 'UTC',
0
);
@@ -156,7 +160,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -169,6 +175,8 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
+ 0,
+ 'UTC',
0
);
diff --git a/store/seed/test/10070__db.sql b/store/seed/test/10070__db.sql
index 17b989ecda..79d9da58c5 100644
--- a/store/seed/test/10070__db.sql
+++ b/store/seed/test/10070__db.sql
@@ -10,7 +10,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -23,6 +25,8 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
+ 0,
+ 'UTC',
0
);
@@ -37,7 +41,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -50,7 +56,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 1624558090
+ 1624558090,
+ 'CST',
+ 28800
);
INSERT INTO
@@ -64,7 +72,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -77,7 +87,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
INSERT INTO
@@ -91,7 +103,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -104,7 +118,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
-- Database for integration environment instance 6002
@@ -119,7 +135,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -132,6 +150,8 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
+ 0,
+ 'UTC',
0
);
@@ -146,7 +166,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -159,7 +181,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
INSERT INTO
@@ -173,7 +197,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -186,7 +212,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
INSERT INTO
@@ -200,7 +228,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -213,7 +243,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
-- Database for staging environment instance 6003
@@ -228,7 +260,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -241,6 +275,8 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
+ 0,
+ 'UTC',
0
);
@@ -255,7 +291,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -268,7 +306,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
INSERT INTO
@@ -282,7 +322,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -295,7 +337,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
INSERT INTO
@@ -309,7 +353,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -322,7 +368,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
-- Database for prod environment instance 6004
@@ -337,7 +385,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -350,6 +400,8 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
+ 0,
+ 'UTC',
0
);
@@ -364,7 +416,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -377,7 +431,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
INSERT INTO
@@ -391,7 +447,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -404,7 +462,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
INSERT INTO
@@ -418,7 +478,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -431,7 +493,9 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
- 0
+ 0,
+ 'CST',
+ 28800
);
-- Database for prod environment instance 6005
@@ -446,7 +510,9 @@ INSERT INTO
character_set,
`collation`,
sync_status,
- last_successful_sync_ts
+ last_successful_sync_ts,
+ timezone_name,
+ timezone_offset
)
VALUES
(
@@ -459,5 +525,7 @@ VALUES
'utf8mb4',
'utf8mb4_general_ci',
'OK',
+ 0,
+ 'UTC',
0
);
\ No newline at end of file