Skip to content

Commit

Permalink
refactor(db): rename chart_views#slug to chart_views#name
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Nov 30, 2024
1 parent a3480a4 commit 3847643
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 25 deletions.
2 changes: 1 addition & 1 deletion adminShared/AdminTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface ApiChartViewOverview {
id: number
slug: string
name: string
parent: {
id: number
title: string
Expand Down
14 changes: 8 additions & 6 deletions adminSiteClient/ChartEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface Log {

export interface ChartViewMinimalInformation {
id: number
slug: string
name: string
title: string
}

Expand Down Expand Up @@ -213,18 +213,20 @@ export class ChartEditor extends AbstractChartEditor<ChartEditorManager> {
delete chartJson.isPublished
delete chartJson.slug

const suggestedSlug = grapher.title ? slugify(grapher.title) : undefined
const suggestedName = grapher.title ? slugify(grapher.title) : undefined

const slug = prompt(
"Please enter a slug for the narrative view. Note that the slug cannot be changed later.",
suggestedSlug
const name = prompt(
"Please enter a programmatic name for the narrative view. Note that this name cannot be changed later.",
suggestedName
)

if (name === null) return

// Need to open intermediary tab before AJAX to avoid popup blockers
const w = window.open("/", "_blank") as Window

const body = {
slug,
name,
parentChartId: grapher.id,
config: chartJson,
}
Expand Down
4 changes: 2 additions & 2 deletions adminSiteClient/ChartViewEditorPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ChartViewEditorPage
static contextType = AdminAppContext
context!: AdminAppContextType

idAndSlug: { id: number; slug: string } | undefined = undefined
idAndName: { id: number; name: string } | undefined = undefined

patchConfig: GrapherInterface = {}
fullConfig: GrapherInterface = {}
Expand All @@ -31,7 +31,7 @@ export class ChartViewEditorPage
`/api/chartViews/${this.chartViewId}`
)

this.idAndSlug = { id: data.id, slug: data.slug }
this.idAndName = { id: data.id, name: data.name }
this.fullConfig = data.configFull
this.patchConfig = data.configPatch
this.parentChartId = data.parentChartId
Expand Down
10 changes: 5 additions & 5 deletions adminSiteClient/ChartViewIndexPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ function createColumns(
),
},
{
title: "Slug",
dataIndex: "slug",
key: "slug",
title: "Name",
dataIndex: "name",
key: "name",
width: 150,
render: (slug) => highlightFn(slug),
render: (name) => highlightFn(name),
},
{
title: "Title",
Expand Down Expand Up @@ -124,7 +124,7 @@ export function ChartViewIndexPage() {
(chartView: ApiChartViewOverview) => [
`${chartView.id}`,
chartView.title,
chartView.slug,
chartView.name,
chartView.parent.title,
]
)
Expand Down
20 changes: 10 additions & 10 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ const getReferencesByChartId = async (
const chartViewsPromise = db.knexRaw<ChartViewMinimalInformation>(
knex,
`-- sql
SELECT cv.id, cv.slug, cc.full ->> "$.title" AS title
SELECT cv.id, cv.name, cc.full ->> "$.title" AS title
FROM chart_views cv
JOIN chart_configs cc ON cc.id = cv.chartConfigId
WHERE cv.parentChartId = ?`,
Expand Down Expand Up @@ -3312,7 +3312,7 @@ const createPatchConfigAndFullConfigForChartView = async (
}

getRouteWithROTransaction(apiRouter, "/chartViews", async (req, res, trx) => {
type ChartViewRow = Pick<DbPlainChartView, "id" | "slug" | "updatedAt"> & {
type ChartViewRow = Pick<DbPlainChartView, "id" | "name" | "updatedAt"> & {
lastEditedByUser: string
chartConfigId: string
title: string
Expand All @@ -3325,7 +3325,7 @@ getRouteWithROTransaction(apiRouter, "/chartViews", async (req, res, trx) => {
`-- sql
SELECT
cv.id,
cv.slug,
cv.name,
cv.updatedAt,
cv.lastEditedByUserId,
cv.chartConfigId,
Expand All @@ -3344,7 +3344,7 @@ getRouteWithROTransaction(apiRouter, "/chartViews", async (req, res, trx) => {

const chartViews: ApiChartViewOverview[] = rows.map((row) => ({
id: row.id,
slug: row.slug,
name: row.name,
updatedAt: row.updatedAt?.toISOString() ?? null,
lastEditedByUser: row.lastEditedByUser,
chartConfigId: row.chartConfigId,
Expand All @@ -3366,7 +3366,7 @@ getRouteWithROTransaction(

type ChartViewRow = Pick<
DbPlainChartView,
"id" | "slug" | "updatedAt"
"id" | "name" | "updatedAt"
> & {
lastEditedByUser: string
chartConfigId: string
Expand All @@ -3381,7 +3381,7 @@ getRouteWithROTransaction(
`-- sql
SELECT
cv.id,
cv.slug,
cv.name,
cv.updatedAt,
cv.lastEditedByUserId,
u.fullName as lastEditedByUser,
Expand Down Expand Up @@ -3416,12 +3416,12 @@ getRouteWithROTransaction(
)

postRouteWithRWTransaction(apiRouter, "/chartViews", async (req, res, trx) => {
const { slug, parentChartId } = req.body as Pick<
const { name, parentChartId } = req.body as Pick<
DbPlainChartView,
"slug" | "parentChartId"
"name" | "parentChartId"
>
const rawConfig = req.body.config as GrapherInterface
if (!slug || !parentChartId || !rawConfig) {
if (!name || !parentChartId || !rawConfig) {
throw new JsonError("Invalid request", 400)
}

Expand All @@ -3441,7 +3441,7 @@ postRouteWithRWTransaction(apiRouter, "/chartViews", async (req, res, trx) => {

// insert into chart_views
const insertRow: DbInsertChartView = {
slug,
name,
parentChartId,
lastEditedByUserId: res.locals.user.id,
chartConfigId: chartConfigId,
Expand Down
19 changes: 19 additions & 0 deletions db/migration/1733004083678-RenameChartViewsSlug.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class RenameChartViewsSlug1733004083678 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`-- sql
ALTER TABLE chart_views RENAME COLUMN slug TO name, RENAME INDEX slug TO name;
`
)
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`-- sql
ALTER TABLE chart_views RENAME COLUMN name TO slug, RENAME INDEX name TO slug;
`
)
}
}
2 changes: 1 addition & 1 deletion packages/@ourworldindata/types/src/dbTypes/ChartViews.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const ChartViewsTableName = "chart_views"
export interface DbInsertChartView {
id?: number
slug: string
name: string
chartConfigId: string
parentChartId: number
createdAt?: Date | null
Expand Down

0 comments on commit 3847643

Please sign in to comment.