| 1 | package idear_test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "path/filepath" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/carlosframework/rastrillo/db" |
| 11 | "github.com/carlosframework/rastrillo/migrate" |
| 12 | "github.com/carlosframework/rastrillo/sessions" |
| 13 | |
| 14 | "amadan.net/rastrillo/idear" |
| 15 | ) |
| 16 | |
| 17 | // openDB is a fresh, on-disk (t.TempDir) rastrillo db, matching the |
| 18 | // shape schema_test needs to apply migrations against — a real |
| 19 | // database, not an in-memory stand-in, per the brief. |
| 20 | func openDB(t *testing.T) *db.DB { |
| 21 | t.Helper() |
| 22 | d, err := db.Open(filepath.Join(t.TempDir(), "idear.db"), nil) |
| 23 | if err != nil { |
| 24 | t.Fatalf("db.Open: %v", err) |
| 25 | } |
| 26 | t.Cleanup(func() { d.Close() }) |
| 27 | return d |
| 28 | } |
| 29 | |
| 30 | // tableExists reports whether name is a table in sqlite_master. |
| 31 | func tableExists(t *testing.T, sqlDB *sql.DB, name string) bool { |
| 32 | t.Helper() |
| 33 | var got string |
| 34 | err := sqlDB.QueryRow( |
| 35 | `SELECT name FROM sqlite_master WHERE type = 'table' AND name = ?`, name, |
| 36 | ).Scan(&got) |
| 37 | if err == sql.ErrNoRows { |
| 38 | return false |
| 39 | } |
| 40 | if err != nil { |
| 41 | t.Fatalf("querying sqlite_master for %s: %v", name, err) |
| 42 | } |
| 43 | return got == name |
| 44 | } |
| 45 | |
| 46 | // TestSchema_Apply checks that merging idear.Schema in after |
| 47 | // sessions.Schema — the documented BootSchema order — and applying it |
| 48 | // to a fresh database creates both idear tables. |
| 49 | func TestSchema_Apply(t *testing.T) { |
| 50 | d := openDB(t) |
| 51 | full := migrate.Merge(sessions.Schema, idear.Schema) |
| 52 | if _, err := migrate.Apply(context.Background(), d, full); err != nil { |
| 53 | t.Fatalf("migrate.Apply: %v", err) |
| 54 | } |
| 55 | |
| 56 | sqlDB := d.Writer() |
| 57 | if !tableExists(t, sqlDB, "idear_members") { |
| 58 | t.Errorf("idear_members table was not created") |
| 59 | } |
| 60 | if !tableExists(t, sqlDB, "idear_invitations") { |
| 61 | t.Errorf("idear_invitations table was not created") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // TestSchema_Apply_Twice checks that a second Apply is a no-op: the |
| 66 | // ledger records what already ran and refuses to re-run it. |
| 67 | func TestSchema_Apply_Twice(t *testing.T) { |
| 68 | d := openDB(t) |
| 69 | full := migrate.Merge(sessions.Schema, idear.Schema) |
| 70 | |
| 71 | first, err := migrate.Apply(context.Background(), d, full) |
| 72 | if err != nil { |
| 73 | t.Fatalf("first migrate.Apply: %v", err) |
| 74 | } |
| 75 | if len(first.Applied) == 0 { |
| 76 | t.Fatalf("first migrate.Apply applied nothing; test cannot tell a no-op second run apart from a broken first one") |
| 77 | } |
| 78 | |
| 79 | second, err := migrate.Apply(context.Background(), d, full) |
| 80 | if err != nil { |
| 81 | t.Fatalf("second migrate.Apply: %v", err) |
| 82 | } |
| 83 | if len(second.Applied) != 0 { |
| 84 | t.Errorf("second migrate.Apply applied %v, want none (ledger should have skipped every migration)", second.Applied) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // frozenIdearChecksum is migrate.Checksum of idear's shipped |
| 89 | // migrations/0001_init.sql, recorded the day it shipped. |
| 90 | // |
| 91 | // ==> THIS CONSTANT MAY NEVER BE UPDATED. <== |
| 92 | // |
| 93 | // It was re-recorded ONCE, on 2026-08-24, during task 3 and before |
| 94 | // idear had a v0.1.0 or a single consumer, because the migration as |
| 95 | // task 2 wrote it did not work: the timestamp columns were declared |
| 96 | // TEXT, and modernc.org/sqlite only decodes a text timestamp back into |
| 97 | // a time.Time when the column's DECLARED type is DATE, DATETIME or |
| 98 | // TIMESTAMP (rows.go, ColumnTypeDatabaseTypeName). Against TEXT |
| 99 | // columns every GORM read of a Member or an Invitation failed with |
| 100 | // "unsupported Scan, storing driver.Value type string into type |
| 101 | // *time.Time" — the tables could be written and never read. DATETIME |
| 102 | // is also what `rastrillo migration generate` emits for a GORM model's |
| 103 | // time.Time (cmd/rastrillo/new.go), so this is the framework's own |
| 104 | // spelling and not a local invention. |
| 105 | // |
| 106 | // That re-recording is the only one there will be. The rule this |
| 107 | // comment states is about migrations some app has in its ledger; on |
| 108 | // 2026-08-24 no ledger anywhere held this one. From the first tag |
| 109 | // onwards the sentence above is literal, and a failure here means an |
| 110 | // edit to revert — not a constant to refresh. |
| 111 | // |
| 112 | // A failure here does not mean the constant is stale — it means an |
| 113 | // edit to a shipped migration file changed its checksum, and that |
| 114 | // edit must be reverted. Every app that has applied this migration has |
| 115 | // this checksum in its ledger; migrate.Apply compares the two on every |
| 116 | // boot and refuses to start if they no longer match. The only |
| 117 | // legitimate way to change a shipped migration's effect is a new |
| 118 | // migration file beside it, with a new ID and a new entry here. See |
| 119 | // rastrillo's own migrate/frozen_checksums_test.go, which this test is |
| 120 | // deliberately shaped after. |
| 121 | const frozenIdearChecksum = "78c47fc344a9c69a564ff91205713baef3f298f6e008d827ca763668ab51941c" |
| 122 | |
| 123 | func TestSchema_FrozenChecksum(t *testing.T) { |
| 124 | for _, m := range idear.Schema.All() { |
| 125 | if m.ID != "idear/0001_init" { |
| 126 | t.Fatalf("unexpected migration id %q; if idear has grown a second migration, "+ |
| 127 | "this test must be extended, not just re-pointed at the new one", m.ID) |
| 128 | } |
| 129 | if got := migrate.Checksum(m.SQL); got != frozenIdearChecksum { |
| 130 | t.Errorf("%s: checksum is now %s, was %s.\n"+ |
| 131 | "A shipped migration file was edited. Revert the edit — do NOT update the "+ |
| 132 | "constant. Every deployed app has the old checksum in its ledger and will "+ |
| 133 | "refuse to boot with \"applied with different SQL\". To change what the schema "+ |
| 134 | "becomes, add a new migration file instead.", m.ID, got, frozenIdearChecksum) |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // TestSchema_RoundTripsAModel writes a Member through GORM and reads |
| 140 | // it back. |
| 141 | // |
| 142 | // It exists because TestSchema_Apply does not, and the difference is |
| 143 | // the whole of the defect this migration was corrected for. Apply |
| 144 | // proves the CREATE TABLE parses. It cannot prove the DECLARED COLUMN |
| 145 | // TYPES are usable, and they were not: with the timestamp columns |
| 146 | // declared TEXT, modernc.org/sqlite handed every stored timestamp back |
| 147 | // as a raw string, because it converts one to a time.Time only when |
| 148 | // the column's declared type is DATE, DATETIME or TIMESTAMP. Every |
| 149 | // read of a Member or an Invitation failed with |
| 150 | // |
| 151 | // sql: Scan error on column index 5, name "created_at": |
| 152 | // unsupported Scan, storing driver.Value type string into type *time.Time |
| 153 | // |
| 154 | // and the tables could be written and never read. A schema test that |
| 155 | // only parses the DDL is exactly the shape that let that through, so |
| 156 | // this one exercises a value instead: the assertion is that CreatedAt |
| 157 | // comes back as a real instant, which is only possible if the column |
| 158 | // type is right. |
| 159 | // |
| 160 | // Reverting migrations/0001_init.sql to TEXT must fail HERE, by name, |
| 161 | // and not only as collateral damage in thirty unrelated tests. |
| 162 | func TestSchema_RoundTripsAModel(t *testing.T) { |
| 163 | d := openDB(t) |
| 164 | if _, err := migrate.Apply(context.Background(), d, |
| 165 | migrate.Merge(sessions.Schema, idear.Schema)); err != nil { |
| 166 | t.Fatalf("migrate.Apply: %v", err) |
| 167 | } |
| 168 | |
| 169 | m := &idear.Member{Subject: "s1", Email: "a@example.test", Name: "A", Role: idear.RoleOwner} |
| 170 | if err := d.G.Create(m).Error; err != nil { |
| 171 | t.Fatalf("writing a Member: %v", err) |
| 172 | } |
| 173 | if m.ID == 0 { |
| 174 | t.Fatal("Create did not assign an id") |
| 175 | } |
| 176 | |
| 177 | var got idear.Member |
| 178 | if err := d.G.Where("id = ?", m.ID).Take(&got).Error; err != nil { |
| 179 | t.Fatalf("reading the Member back: %v\n"+ |
| 180 | "An \"unsupported Scan ... into type *time.Time\" here means a timestamp column "+ |
| 181 | "is declared TEXT again; it must be DATETIME.", err) |
| 182 | } |
| 183 | if got.CreatedAt.IsZero() { |
| 184 | t.Error("CreatedAt came back as the zero time; the column is not carrying an instant") |
| 185 | } |
| 186 | if got.UpdatedAt.IsZero() { |
| 187 | t.Error("UpdatedAt came back as the zero time") |
| 188 | } |
| 189 | if got.DeactivatedAt != nil { |
| 190 | t.Errorf("DeactivatedAt = %v, want nil — a NULL timestamp must scan as a nil *time.Time", got.DeactivatedAt) |
| 191 | } |
| 192 | if got.Subject != m.Subject || got.Role != idear.RoleOwner { |
| 193 | t.Errorf("read back %+v, want subject %q at role owner", got, m.Subject) |
| 194 | } |
| 195 | |
| 196 | // The nullable timestamp must round-trip when it is SET, too — a |
| 197 | // deactivated member is read on every request the middleware |
| 198 | // gates. |
| 199 | when := time.Now().UTC().Truncate(time.Millisecond) |
| 200 | if err := d.G.Model(&idear.Member{}).Where("id = ?", m.ID). |
| 201 | Update("deactivated_at", when).Error; err != nil { |
| 202 | t.Fatalf("deactivating: %v", err) |
| 203 | } |
| 204 | var off idear.Member |
| 205 | if err := d.G.Where("id = ?", m.ID).Take(&off).Error; err != nil { |
| 206 | t.Fatalf("reading the deactivated Member back: %v", err) |
| 207 | } |
| 208 | if off.DeactivatedAt == nil || !off.DeactivatedAt.Equal(when) { |
| 209 | t.Errorf("DeactivatedAt = %v, want %v", off.DeactivatedAt, when) |
| 210 | } |
| 211 | if off.Active() { |
| 212 | t.Error("the member reads as active after being deactivated") |
| 213 | } |
| 214 | } |
| 215 | |