ORM and schema files

Point Ghostrow at SQL files, a Prisma schema, Django models, SQLAlchemy models or Laravel migrations with --schema, without a database connection.

--schema takes a file or a folder. Ghostrow reads the text of the files and never imports or runs your code, so it needs no database, no Python environment and no PHP.

ghostrow seed --schema schema.sql --dry-run
ghostrow seed --schema prisma/schema.prisma --dry-run
ghostrow seed --schema ./app/ --dry-run

Folders

Give --schema a folder and Ghostrow reads every .sql, .ddl, .prisma, .py and .php file in it, up to eight levels down, in name order. It skips hidden folders and node_modules, .git, .venv, venv, env, __pycache__, dist, build, vendor, .next and target. A file that is not a schema is ignored, so a Django project folder works as it is.

If several files describe the same table, Ghostrow merges them. Later files add columns and keys to the table. The run stops if no tables are found: No tables found in <path>. Supported: SQL DDL (CREATE TABLE), Prisma, Django, SQLAlchemy and Laravel migrations.

Supported formats

FormatHow Ghostrow recognises itWhat it reads
SQL DDL.sql or .ddl, or any file containing CREATE TABLEColumns and types, primary and foreign keys, UNIQUE, CHECK, enums (CREATE TYPE ... AS ENUM), ALTER TABLE ... ADD CONSTRAINT, CREATE UNIQUE INDEX. Views, functions, inserts and comments are skipped
Prisma.prisma, or a file with a datasource or generator blockModels, field types, @id, @unique, @map, @@map, @@id, @@unique, relations and enums
Django.py files that import django.db.modelsModel classes, field types, max_length, choices, ForeignKey, OneToOneField, ManyToManyField (as a join table), Meta.db_table, and abstract = True classes are skipped
SQLAlchemy.py files with __tablename__ or Table(...)Declarative models in both the 1.x Column style and the 2.0 Mapped and mapped_column style, plus Core Table(...) calls, with foreign keys
Laravel.php files with Schema::create or Schema::tableColumn methods such as string, integer, foreignId(...)->constrained() and timestamps and unique

Table names

The table name is what the database would have, so rules files and --table use it.

  • Prisma uses the model name (User) unless @@map("users") is set.
  • Django names a table <app>_<model> in lower case, for example shop_customer. The app is the name of the folder that holds models.py. Meta.db_table overrides it.
  • SQLAlchemy uses __tablename__ or the first argument of Table(...).
  • Laravel uses the name given to Schema::create.

Dialect

SQL files are read as Postgres, MySQL, SQLite or SQL Server, guessed from the script:

  • AUTOINCREMENT or WITHOUT ROWID means SQLite.
  • IDENTITY(, NVARCHAR, DATETIME2, UNIQUEIDENTIFIER or GO lines mean SQL Server. GO batch separators are handled.
  • Backticks, AUTO_INCREMENT or ENGINE= mean MySQL.
  • Anything else is Postgres.

The first file decides the dialect for a whole folder. Prisma uses the provider of its datasource. Laravel migrations are read as MySQL. The dialect sets how --format sql quotes names and writes values.

Example

A small Django app, django/shop/models.py:

class Customer(models.Model):
    email = models.EmailField(unique=True)
    name = models.CharField(max_length=80)

class Order(models.Model):
    customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
    total = models.DecimalField(max_digits=10, decimal_places=2)
    status = models.CharField(max_length=10, choices=[("new", "New"), ("paid", "Paid")])
ghostrow seed --schema django --dry-run
read schema   2 tables, 1 foreign key (django/, Free)
understood    shop_customer.id                 row id, primary key  (0.93, name, type)
              shop_customer.email              email, unique  (0.83, name, type)
              shop_customer.name               person's name  (0.92, name, type)
              shop_order.id                    row id, primary key  (0.93, name, type)
              shop_order.customer_id           reference  (1.00, foreign key to shop_customer)
              shop_order.total                 amount  (0.99, name, type)
              shop_order.status                status  (0.99, name, type)
rules         shop_order: status is one of new, paid  (declared enum)

The choices list became the allowed values for status.

Limits of reading text

Ghostrow reads what is written in the files. A model built in a loop, or a column added by a plain-SQL migration that is not in the files you pass, will be missing. If the picture looks incomplete, point Ghostrow at the real database with --db instead, or add the missing DDL to a .sql file in the same folder.

Next