Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Guide/authentication.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ To use the authentication module, your `users` table needs to have at least an `

```sql
CREATE TABLE users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
password_hash TEXT NOT NULL,
locked_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
Expand Down
2 changes: 1 addition & 1 deletion Guide/database-migrations.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ To generate our new posts table we copy the DDL statement from `Schema.sql` into

```sql
CREATE TABLE posts (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
body TEXT NOT NULL
);
Expand Down
12 changes: 6 additions & 6 deletions Guide/database.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ Database name: `app`.

Once you have created your project, the first step is to define a database schema. The database schema is a SQL file with a lot of `CREATE TABLE ...` statements. You can find it at `Application/Schema.sql`.

In a new project, this file will be empty. The [`uuid-ossp`](https://www.postgresql.org/docs/current/uuid-ossp.html) extension is automatically enabled for the database by IHP.
In a new project, this file will be empty. PostgreSQL 18+ provides native `uuidv7()` and `uuidv4()` functions for generating UUIDs without requiring any extensions.

To define your database schema add your `CREATE TABLE ...` statements to the `Schema.sql`. For a users table this can look like this:

```sql
CREATE TABLE users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL
);
Expand Down Expand Up @@ -120,7 +120,7 @@ For every table in the `Schema.sql` a corresponding data structure will be gener

```sql
CREATE TABLE users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
firstname TEXT NOT NULL,
lastname TEXT NOT NULL
);
Expand Down Expand Up @@ -190,7 +190,7 @@ Sometimes you have an optional id field, like e.g. when having a database schema

```sql
CREATE TABLE tasks (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
description TEXT,
assigned_user_id UUID
);
Expand Down Expand Up @@ -690,7 +690,7 @@ You can use the enum as a field type for another record. E.g. we can have `posts

```sql
CREATE TABLE posts (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
body TEXT NOT NULL,
color colors
);
Expand Down Expand Up @@ -847,7 +847,7 @@ It's possible to use the UI to set the unique constraint on a column. However, s

```sql
CREATE TABLE users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
username TEXT NOT NULL,
UNIQUE (email, username)
Expand Down
6 changes: 3 additions & 3 deletions Guide/file-storage.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ In this example we assume the following data schema:

```sql
CREATE TABLE companies (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
logo_url TEXT DEFAULT NULL
);
Expand Down Expand Up @@ -498,7 +498,7 @@ To solve both problems we can introduce a new model `UploadedFile`:

```sql
CREATE TABLE uploaded_files (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
signed_url TEXT NOT NULL,
signed_url_expired_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
path TEXT NOT NULL,
Expand All @@ -513,7 +513,7 @@ Companies will now reference this new record

```sql
CREATE TABLE companies (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
uploaded_file_id UUID NOT NULL
);
Expand Down
4 changes: 2 additions & 2 deletions Guide/oauth.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ For Google OAuth Login to work, we need to add a `google_user_id TEXT` column to
Open `Application/Schema.sql` and add `google_user_id TEXT` to the `CREATE TABLE users` statement:
```sql
CREATE TABLE users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
-- ... ,
google_user_id TEXT
);
Expand Down Expand Up @@ -296,7 +296,7 @@ For GitHub OAuth Login to work, we need to add a `github_user_id INT` column to
Open `Application/Schema.sql` and add `github_iser_id INT` to the `CREATE TABLE users` statement:
```sql
CREATE TABLE users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
-- ... ,
github_user_id INT DEFAULT NULL UNIQUE,
);
Expand Down
2 changes: 1 addition & 1 deletion Guide/realtime-spas.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ In this example we're going to build a simple todo list.

```sql
CREATE TABLE todos (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
is_completed BOOLEAN DEFAULT false NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
Expand Down
4 changes: 2 additions & 2 deletions Guide/relationships.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ The following sections assume the following database schema being given. It's th

```sql
CREATE TABLE posts (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
body TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
);

CREATE TABLE comments (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
post_id UUID NOT NULL,
author TEXT NOT NULL,
body TEXT NOT NULL,
Expand Down
8 changes: 4 additions & 4 deletions Guide/stripe.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Open the `Application/Schema.sql` and add this:

```sql
CREATE TABLE plans (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
name TEXT NOT NULL,
stripe_price_id TEXT NOT NULL
);
Expand All @@ -109,7 +109,7 @@ Open `Application/Schema.sql` and add this `subscriptions` table:

```sql
CREATE TABLE subscriptions (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
user_id UUID NOT NULL,
starts_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
ends_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
Expand All @@ -127,7 +127,7 @@ Additionally you also need to add the following fields to your `users` table:

```sql
CREATE TABLE users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
-- ...,

stripe_customer_id TEXT DEFAULT NULL,
Expand Down Expand Up @@ -415,7 +415,7 @@ Open `Application/Schema.sql` and add a `invoices` table:

```sql
CREATE TABLE invoices (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
subscription_id UUID NOT NULL,
stripe_invoice_id TEXT NOT NULL,
invoice_url TEXT NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion Guide/validation.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ Here's an excerpt from the `Schema.sql`:
CREATE TYPE comment_moderation AS ENUM ('comment_moderation_pending', 'comment_moderation_approved', 'comment_moderation_rejected');

CREATE TABLE comments (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
post_id UUID NOT NULL,
body TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL
Expand Down
2 changes: 1 addition & 1 deletion Guide/your-first-project.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Open the `Application/Schema.sql` in your code editor to see the SQL queries whi

```sql
CREATE TABLE posts (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
body TEXT NOT NULL
);
Expand Down
6 changes: 3 additions & 3 deletions ihp-datasync-typescript/Test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ tests = do
it "should generate a valid typescript definition file" do
let schema = parseSqlStatements $ cs [plain|
CREATE TABLE users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
password_hash TEXT NOT NULL,
locked_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
Expand All @@ -28,7 +28,7 @@ tests = do
);
CREATE TYPE colors AS ENUM ('red', 'blue');
CREATE TABLE tasks (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,
user_id UUID NOT NULL,
Expand Down Expand Up @@ -414,7 +414,7 @@ tests = do
let table = CreateTable
{ name = "tasks"
, columns =
[ Column { name = "id", columnType = PUUID, defaultValue = Just (CallExpression "uuid_generate_v4" []), notNull = True, isUnique = False, generator = Nothing }
[ Column { name = "id", columnType = PUUID, defaultValue = Just (CallExpression "uuidv7" []), notNull = True, isUnique = False, generator = Nothing }
, Column { name = "title", columnType = PText, defaultValue = Just (TextExpression "untitled"), notNull = True, isUnique = False, generator = Nothing }
]
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
Expand Down
2 changes: 1 addition & 1 deletion ihp-datasync/IHP/DataSync/ChangeNotifications.hs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ createNotificationFunction table = [i|
AND n.nspname = 'public'
) THEN
CREATE UNLOGGED TABLE large_pg_notifications (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
payload TEXT DEFAULT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
);
Expand Down
4 changes: 2 additions & 2 deletions ihp-graphql/Test/GraphQL/SchemaCompilerSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ tests = do
let sqlSchema = parseSqlStatements [trimming|
-- Your database schema. Use the Schema Designer at http://localhost:8001/ to add some tables.
CREATE TABLE users (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
email TEXT NOT NULL,
password_hash TEXT NOT NULL,
locked_at TIMESTAMP WITH TIME ZONE DEFAULT NULL,
failed_login_attempts INT DEFAULT 0 NOT NULL
);
CREATE TABLE tasks (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,
id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,
title TEXT NOT NULL,
body TEXT NOT NULL,
user_id UUID NOT NULL
Expand Down
2 changes: 1 addition & 1 deletion ihp-ide/IHP/IDE/CodeGen/JobGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ buildPlan' config =
schemaSql =
""
<> "CREATE TABLE " <> tableName <> " (\n"
<> " id UUID DEFAULT uuid_generate_v4() PRIMARY KEY NOT NULL,\n"
<> " id UUID DEFAULT uuidv7() PRIMARY KEY NOT NULL,\n"
<> " created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,\n"
<> " updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL,\n"
<> " status JOB_STATUS DEFAULT 'job_status_not_started' NOT NULL,\n"
Expand Down
8 changes: 6 additions & 2 deletions ihp-ide/IHP/IDE/Data/View/Layout.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ isBoolField fieldName tableCols = case (find (\c -> c.columnName == (cs fieldNam

isSqlFunction :: Text -> Bool
isSqlFunction text = text `elem`
[ "uuid_generate_v4()"
[ "uuidv7()"
, "uuidv4()"
, "uuid_generate_v4()"
, "NOW()"
, "NULL"]

isSqlFunction_ :: ByteString -> Bool
isSqlFunction_ text = text `elem`
[ "uuid_generate_v4()"
[ "uuidv7()"
, "uuidv4()"
, "uuid_generate_v4()"
, "NOW()"
, "NULL"]

Expand Down
2 changes: 1 addition & 1 deletion ihp-ide/IHP/IDE/SchemaDesigner/SchemaOperations.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ addTable tableName list = list <> [StatementCreateTable CreateTable
[Column
{ name = "id"
, columnType = PUUID
, defaultValue = Just (CallExpression "uuid_generate_v4" [])
, defaultValue = Just (CallExpression "uuidv7" [])
, notNull = True
, isUnique = False
, generator = Nothing
Expand Down
4 changes: 2 additions & 2 deletions ihp-ide/Test/IDE/CodeGeneration/ControllerGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ tests = do
let schema = [
StatementCreateTable (table "pages") {
columns = [
(col "id" PUUID) { defaultValue = Just (CallExpression "uuid_generate_v4" []), notNull = True }
(col "id" PUUID) { defaultValue = Just (CallExpression "uuidv7" []), notNull = True }
]
, primaryKeyConstraint = PrimaryKeyConstraint ["id"]
},
StatementCreateTable (table "people") {
columns = [
(col "id" PUUID) { defaultValue = Just (CallExpression "uuid_generate_v4" []), notNull = True }
(col "id" PUUID) { defaultValue = Just (CallExpression "uuidv7" []), notNull = True }
, (col "name" PText) { notNull = True }
, (col "email" PText) { notNull = True }
]
Expand Down
Loading