72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
|
package migrations
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"time"
|
||
|
|
||
|
"git.handmade.network/hmn/hmn/src/migration/types"
|
||
|
"git.handmade.network/hmn/hmn/src/oops"
|
||
|
"github.com/jackc/pgx/v4"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
registerMigration(AddEducationResources{})
|
||
|
}
|
||
|
|
||
|
type AddEducationResources struct{}
|
||
|
|
||
|
func (m AddEducationResources) Version() types.MigrationVersion {
|
||
|
return types.MigrationVersion(time.Date(2022, 6, 23, 3, 27, 52, 0, time.UTC))
|
||
|
}
|
||
|
|
||
|
func (m AddEducationResources) Name() string {
|
||
|
return "AddEducationResources"
|
||
|
}
|
||
|
|
||
|
func (m AddEducationResources) Description() string {
|
||
|
return "Adds the tables needed for the 2022 education initiative"
|
||
|
}
|
||
|
|
||
|
func (m AddEducationResources) Up(ctx context.Context, tx pgx.Tx) error {
|
||
|
_, err := tx.Exec(ctx,
|
||
|
`
|
||
|
CREATE TABLE education_article_version (
|
||
|
id SERIAL NOT NULL PRIMARY KEY
|
||
|
);
|
||
|
|
||
|
CREATE TABLE education_article (
|
||
|
id SERIAL NOT NULL PRIMARY KEY,
|
||
|
title VARCHAR(255) NOT NULL,
|
||
|
slug VARCHAR(255) NOT NULL,
|
||
|
description TEXT NOT NULL,
|
||
|
type INT NOT NULL,
|
||
|
current_version INT NOT NULL REFERENCES education_article_version (id)
|
||
|
);
|
||
|
|
||
|
ALTER TABLE education_article_version
|
||
|
ADD article_id INT NOT NULL REFERENCES education_article (id) ON DELETE CASCADE,
|
||
|
ADD date TIMESTAMP WITH TIME ZONE NOT NULL,
|
||
|
ADD content_raw TEXT NOT NULL,
|
||
|
ADD content_html TEXT NOT NULL,
|
||
|
ADD editor_id INT REFERENCES hmn_user (id) ON DELETE SET NULL;
|
||
|
`,
|
||
|
)
|
||
|
if err != nil {
|
||
|
return oops.New(err, "failed to create education tables")
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (m AddEducationResources) Down(ctx context.Context, tx pgx.Tx) error {
|
||
|
_, err := tx.Exec(ctx, `
|
||
|
DROP TABLE education_article CASCADE;
|
||
|
DROP TABLE education_article_version CASCADE;
|
||
|
`)
|
||
|
if err != nil {
|
||
|
return oops.New(err, "failed to delete education tables")
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|