Skip to content

Commit

Permalink
update database docs for index_name override on create_index
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Oct 8, 2024
1 parent 951e3fe commit 925c3c0
Showing 1 changed file with 40 additions and 10 deletions.
50 changes: 40 additions & 10 deletions docs/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -922,42 +922,72 @@ DROP TABLE IF EXISTS "users";
table, the rest of the arguments are the ordered columns that make up the
index. Optionally the last argument can be a Lua table of options.

There are two options `unique: BOOL`, `where: clause_string`.
$options_table{
{
name = "unique",
description = [[
A boolean value indicating whether the index should be unique.
]]
}, {
name = "where",
description = [[
A clause string that specifies conditions for the index. This string will be used literally in the query and is used to create partial indexes.
]],
example = dual_code{[[
create_index("example_table", "example_column", { where = "some_column is not null" })
]]}
}, {
name = "index_name",
description = [[
A string value that overrides the default generated index name.
]]
}
}

`create_index` will also check if the index exists before attempting to create
it. If the index exists then nothing will happen.

Here are some example indexes:

```lua
$dual_code{
lua=[[
local create_index = schema.create_index

create_index("users", "created_at")
create_index("users", "username", { unique = true })

create_index("posts", "category", "title")
create_index("uploads", "name", { where = "not deleted" })
```

```moon
]],
moon=[[
import create_index from schema

create_index "users", "created_at"
create_index "users", "username", unique: true

create_index "posts", "category", "title"
create_index "uploads", "name", where: "not deleted"
```
]]}

This will generate the following SQL:

```sql
CREATE INDEX ON "users" (created_at);
CREATE UNIQUE INDEX ON "users" (username);
CREATE INDEX ON "posts" (category, title);
CREATE INDEX ON "uploads" (name) WHERE not deleted;
CREATE INDEX "users_created_at_idx" ON "users" (created_at);
CREATE UNIQUE INDEX "users_username_idx" ON "users" (username);
CREATE INDEX "posts_category_title_idx" ON "posts" (category, title);
CREATE INDEX "uploads_name_idx" ON "uploads" (name) WHERE not deleted;
```

The index name is generated by concatenating the table name and the column
names, separated by underscores, followed by the suffix "_idx". For example, an
index on the "users" table with columns "username" and "email" would have a
default index name of "users_username_email_idx". Special characters in column
names are replaced with underscores to ensure the index name is valid.

To override the default index name, you can use the `index_name` option when
creating an index.


#### `drop_index(table_name, col1, col2...)`

Drops an index from a table. It calculates the name of the index from the table
Expand Down

0 comments on commit 925c3c0

Please sign in to comment.