|
| 1 | +package testutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/swiftcarrot/dbx/schema" |
| 5 | +) |
| 6 | + |
| 7 | +// CreateUsersTableSchema creates a schema with a users table for testing. |
| 8 | +func CreateUsersTableSchema() *schema.Schema { |
| 9 | + s := schema.NewSchema() |
| 10 | + s.CreateTable("users", func(t *schema.Table) { |
| 11 | + t.Column("id", "serial", schema.NotNull) |
| 12 | + t.Column("username", "varchar(50)", schema.NotNull) |
| 13 | + t.Column("email", "varchar(100)", schema.NotNull) |
| 14 | + t.Column("created_at", "timestamp", schema.NotNull, schema.Default("CURRENT_TIMESTAMP")) |
| 15 | + t.SetPrimaryKey("users_pkey", []string{"id"}) |
| 16 | + t.Index("users_email_idx", []string{"email"}, schema.Unique) |
| 17 | + t.Index("users_username_idx", []string{"username"}) |
| 18 | + }) |
| 19 | + return s |
| 20 | +} |
| 21 | + |
| 22 | +// CreateUsersTableWithProfileSchema creates a schema with a users table that includes profile fields. |
| 23 | +func CreateUsersTableWithProfileSchema() *schema.Schema { |
| 24 | + s := CreateUsersTableSchema() |
| 25 | + table := s.Tables[0] // users table |
| 26 | + table.Column("bio", "text", schema.Nullable) |
| 27 | + table.Column("avatar_url", "varchar(255)", schema.Nullable) |
| 28 | + return s |
| 29 | +} |
| 30 | + |
| 31 | +// CreateUsersAndPostsSchema creates a schema with users and posts tables. |
| 32 | +func CreateUsersAndPostsSchema() *schema.Schema { |
| 33 | + s := CreateUsersTableSchema() |
| 34 | + s.CreateTable("posts", func(t *schema.Table) { |
| 35 | + t.Column("id", "serial", schema.NotNull) |
| 36 | + t.Column("user_id", "integer", schema.NotNull) |
| 37 | + t.Column("title", "varchar(200)", schema.NotNull) |
| 38 | + t.Column("content", "text", schema.Nullable) |
| 39 | + t.Column("published", "boolean", schema.NotNull, schema.Default("false")) |
| 40 | + t.Column("created_at", "timestamp", schema.NotNull, schema.Default("CURRENT_TIMESTAMP")) |
| 41 | + t.SetPrimaryKey("posts_pkey", []string{"id"}) |
| 42 | + t.Index("posts_user_id_idx", []string{"user_id"}) |
| 43 | + t.Index("posts_published_created_at_idx", []string{"published", "created_at"}) |
| 44 | + t.ForeignKey("fk_posts_user", []string{"user_id"}, "users", []string{"id"}, schema.OnDelete("CASCADE")) |
| 45 | + }) |
| 46 | + return s |
| 47 | +} |
| 48 | + |
| 49 | +// CreateFullSchema creates a schema with users, posts, and comments tables. |
| 50 | +func CreateFullSchema() *schema.Schema { |
| 51 | + s := CreateUsersAndPostsSchema() |
| 52 | + s.CreateTable("comments", func(t *schema.Table) { |
| 53 | + t.Column("post_id", "integer", schema.NotNull) |
| 54 | + t.Column("user_id", "integer", schema.NotNull) |
| 55 | + t.Column("content", "text", schema.NotNull) |
| 56 | + t.Column("created_at", "timestamp", schema.NotNull, schema.Default("CURRENT_TIMESTAMP")) |
| 57 | + t.SetPrimaryKey("comments_pkey", []string{"post_id", "user_id"}) |
| 58 | + t.ForeignKey("fk_comments_post", []string{"post_id"}, "posts", []string{"id"}, schema.OnDelete("CASCADE")) |
| 59 | + t.ForeignKey("fk_comments_user", []string{"user_id"}, "users", []string{"id"}, schema.OnDelete("CASCADE")) |
| 60 | + }) |
| 61 | + return s |
| 62 | +} |
| 63 | + |
| 64 | +// CreateUsersTableWithModifiedColumnSchema creates a users schema with modified columns. |
| 65 | +func CreateUsersTableWithModifiedColumnSchema() *schema.Schema { |
| 66 | + s := schema.NewSchema() |
| 67 | + s.CreateTable("users", func(t *schema.Table) { |
| 68 | + t.Column("id", "serial", schema.NotNull) |
| 69 | + t.Column("username", "varchar(50)", schema.Nullable) // Changed to nullable |
| 70 | + t.Column("email", "varchar(255)", schema.NotNull) // Changed length |
| 71 | + t.Column("created_at", "timestamp", schema.NotNull, schema.Default("CURRENT_TIMESTAMP")) |
| 72 | + t.SetPrimaryKey("users_pkey", []string{"id"}) |
| 73 | + t.Index("users_email_idx", []string{"email"}, schema.Unique) |
| 74 | + t.Index("users_username_idx", []string{"username"}) |
| 75 | + }) |
| 76 | + return s |
| 77 | +} |
0 commit comments