This repository has been archived on 2024-09-20. You can view files and clone it, but cannot push or open issues or pull requests.
musicapp/migrations.moon
2019-06-27 19:33:10 -07:00

63 lines
1.9 KiB
Plaintext

import create_table, rename_column, add_column, types from require "lapis.db.schema"
import trim from require "lapis.util"
import process_tags from require "helpers"
import Tracks, Tags from require "models"
{
[1]: =>
create_table "tracks", {
{ "id", types.serial primary_key: true }
{ "artist", types.text null: true }
{ "name", types.text null: true }
{ "url", types.text null: true }
{ "status", types.integer default: 1 }
}
[2]: =>
rename_column "tracks", "name", "title"
rename_column "tracks", "url", "link"
for column in *{
{ "art_url", types.text null: true }
{ "file_url", types.text null: true }
{ "file_size", types.integer null: true }
{ "album", types.text null: true }
{ "genre", types.text null: true }
{ "tags", types.text default: " untagged " }
{ "quality", types.integer default: 1 }
{ "mood", types.text null: true }
}
add_column "tracks", unpack column
[3]: =>
return true -- formerly a bad migration
[4]: =>
return true -- formerly a bad migration
[5]: =>
create_table "tags", {
{ "id", types.serial primary_key: true }
{ "name", types.text unique: true }
{ "count", types.integer default: -1 }
}
tags = {}
for track in *Tracks\select "*"
for name in track.tags\gmatch "%S+"
if tags[name]
tags[name] += 1
else
tags[name] = 1
for name, count in pairs tags
Tags\create { :name, :count }
[6]: =>
return true -- formerly a bad migration
[7]: =>
-- WARNING: removes all tags!
for track in *Tracks\select "*"
tags = ""
for field in *{"artist", "mood", "link", "genre"}
if track[field] and #track[field] > 0
tags ..= " #{field}:#{track[field]\gsub "%s+", "_"}"
if #tags > 0
track.tags = process_tags "untagged" .. tags
else
track.tags = " untagged "
track\update "tags"
}