a-bakos / gazebo-cms

0 stars 0 forks source link

Create db migrations files #2

Closed a-bakos closed 1 year ago

a-bakos commented 1 year ago

An example on how to create a migrations file:

sqlx migrate add -r create_gb_posts_table

Inside the files that get created we can add some SQL command

*In `_up.sql`**

CREATE TABLE IF NOT EXISTS gb_posts (
    id serial PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL
)

*In `_down.sql`**

DROP TABLE IF EXISTS gb_posts;

Then to add default "posts", create a new pair of files:

sqlx migrate add -r create_gb_posts_add_default_posts_table

*In `_up.sql`**

INSERT INTO gb_posts (title, content)
SELECT 'Hello, Gazebo!', 'First demo post for your Gazebo CMS.'
UNION ALL
SELECT 'A CMS experiment project', 'Second demo post for your Gazebo CMS.'
UNION ALL
SELECT 'Inspired by WordPress', 'Third demo post for your Gazebo CMS.'
WHERE NOT EXISTS (SELECT id FROM gb_posts WHERE title IN ('Hello, Gazebo!', 'A CMS experiment project', 'Inspired by WordPress'))
RETURNING id;