Skip to content

Instantly share code, notes, and snippets.

@garcia-pedro-hr
Created February 14, 2020 19:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garcia-pedro-hr/be1115a5bb4e1e377cf211ac1e9f2a5f to your computer and use it in GitHub Desktop.
Save garcia-pedro-hr/be1115a5bb4e1e377cf211ac1e9f2a5f to your computer and use it in GitHub Desktop.
Room Database Schema to DB
BEGIN TRANSACTION;
CREATE TABLE IF NOT EXISTS "room_master_table" (
"id" INTEGER,
"identity_hash" TEXT,
PRIMARY KEY("id")
);
CREATE TABLE IF NOT EXISTS "bar_table" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"parentFooId" INTEGER NOT NULL,
"barLongField" INTEGER NOT NULL,
FOREIGN KEY("parentFooId") REFERENCES "foo_table"("id") ON UPDATE NO ACTION ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS "foo_table" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"fooName" TEXT NOT NULL,
"nullableBooleanField" INTEGER
);
INSERT INTO "room_master_table" VALUES (42,'650b8756c4bb1f35699539b6d4f27454');
INSERT INTO "bar_table" VALUES (1,1,10);
INSERT INTO "bar_table" VALUES (2,1,20);
INSERT INTO "bar_table" VALUES (3,2,30);
INSERT INTO "foo_table" VALUES (1,'FooOne',1);
INSERT INTO "foo_table" VALUES (2,'FooTwo',NULL);
CREATE INDEX IF NOT EXISTS "index_bar_table_parentFooId" ON "bar_table" (
"parentFooId"
);
COMMIT;
CREATE TABLE IF NOT EXISTS foo_table (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
fooName TEXT NOT NULL,
nullableBooleanField INTEGER
);
CREATE TABLE IF NOT EXISTS bar_table (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
parentFooId INTEGER NOT NULL,
barLongField INTEGER NOT NULL,
FOREIGN KEY(parentFooId) REFERENCES foo_table(id) ON UPDATE NO ACTION ON DELETE CASCADE
);
CREATE INDEX IF NOT EXISTS index_bar_table_parentFooId ON bar_table (parentFooId);
CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY, identity_hash TEXT);
INSERT OR REPLACE INTO room_master_table (id, identity_hash)
VALUES (42, "650b8756c4bb1f35699539b6d4f27454");
INSERT INTO foo_table (id, fooName, nullableBooleanField)
VALUES
(1, "FooOne", 1),
(2, "FooTwo", NULL);
INSERT INTO bar_table (id, parentFooId, barLongField)
VALUES
(1, 1, 10),
(2, 1, 20),
(3, 2, 30);
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "650b8756c4bb1f35699539b6d4f27454",
"entities": [
{
"tableName": "foo_table",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `fooName` TEXT NOT NULL, `nullableBooleanField` INTEGER)",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "fooName",
"columnName": "fooName",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "nullableBooleanField",
"columnName": "nullableBooleanField",
"affinity": "INTEGER",
"notNull": false
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "bar_table",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `parentFooId` INTEGER NOT NULL, `barLongField` INTEGER NOT NULL, FOREIGN KEY(`parentFooId`) REFERENCES `foo_table`(`id`) ON UPDATE NO ACTION ON DELETE CASCADE )",
"fields": [
{
"fieldPath": "id",
"columnName": "id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "parentFooId",
"columnName": "parentFooId",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "barLongField",
"columnName": "barLongField",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"id"
],
"autoGenerate": true
},
"indices": [
{
"name": "index_bar_table_parentFooId",
"unique": false,
"columnNames": [
"parentFooId"
],
"createSql": "CREATE INDEX IF NOT EXISTS `index_bar_table_parentFooId` ON `${TABLE_NAME}` (`parentFooId`)"
}
],
"foreignKeys": [
{
"table": "foo_table",
"onDelete": "CASCADE",
"onUpdate": "NO ACTION",
"columns": [
"parentFooId"
],
"referencedColumns": [
"id"
]
}
]
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '650b8756c4bb1f35699539b6d4f27454')"
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment